Interview Collection 6--Project deployment (linux environment construction, springboot project deployment, vue project deployment, nginx load balancing)

Environmental preparation

In actual development, the project must be deployed in each Linux system server. During the learning process, you can install a
virtual and then configure the Linux operating system to simulate project deployment. Use cloud servers provided by various platforms
for project learning and deployment (such as Alibaba Cloud, Tencent Cloud, Huawei Cloud, etc.).

Here I am using centos7

Configure the yum source of centos

1. Back up the original image file, so that it can be restored after an error

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2. Open Ali's mirror path http://mirrors.aliyun.com, find centos and click Help to see the specific configuration

Download the new CentOS-Base.repo to /etc/yum.repos.d/

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3. Clear the original cache

yum clean all

4. Generate a new cache

yum makecache

5. Update the system

yum -y update

Install and configure JDK

https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html
insert image description here
You can choose to download the tar.gz compressed file under the Linux system

First use the yum source to download and install a file upload and download link tool:

yum install lrzsz

Enter the /usr/local/src directory. Just use the rz command

Of course, the follow-up will also involve mysql data, redis server, nginx server, etc., which can be downloaded in advance and uploaded to the server together
.

Note: All the source file installation packages we upload later are directly placed in the /user/local/src directory of the server, then
decompressed and moved to their respective folders under /usr/local/.

jdk installation

1. First check whether jdk is installed on the system (if it is installed, you need to uninstall it before using our own

java -version

2. Check out the installed java package

rpm -qa | grep java

3. Uninstall the jdk that comes with linux

rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.i686
rpm -e --nodeps java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.i686 tzdata-java-2013g-1.el6.noarch

If there is no built-in jdk, the above 2 and 3 steps are skipped directly.

4. Enter the /usr/local/src directory and unzip the jdk

cd  /usr/local/src

tar -zxvf jdk-8u202-linux-x64.tar.gz

5. Move the unzipped jdk to another folder and rename it

mv jdk1.8.0_202/ /usr/local/jdk1.8

6. Configure environment variables

First use vi /etc/profile to enter the configuration file.
Then, add the java configuration at the bottom of the file.

vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8
export PATH=$JAVA_HOME/bin:$PATH

insert image description here

Save and exit, then reflash the configuration file.

source /etc/profile

7. Check if jdk is installed successfully

java -version

MySql installation configuration

The glibc installation method is used here.

download

https://dev.mysql.com/downloads/mysql/
You can choose to use the compressed package to download and decompress, but additional configuration is required. You can also choose to install the configuration in other ways.
insert image description here

1. Uninstall the Maridb database that comes with Centos7

Be sure to uninstall it cleanly or you will get an error later

rpm -qa | grep mariadb
yum -y remove mari*
rm -rf /var/lib/mysql/*
rpm -qa | grep mariadb

2. Install the third-party dependent libraries required during the installation and use of mysql

yum search libaio # search for info
yum install libaio # install library

3. Create a Mysql administrative user

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

4. Unzip and install mysql
Note, enter the /usr/local/src directory and unzip the uploaded mysql.

tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

Then move and rename to /usr/local/mysql.

mv mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql

File structure description:
insert image description here
Go to the /usr/local/mysql directory, create the mysql-files file, and set the user and permissions.

cd /usr/local/mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files

6. Initialize the Mysql database

Go to the /usr/local/mysql directory and execute the following command to initialize the mysql data. The password for versions before 5.7 is empty by default. For version 5.7,
there will be an initialized password, which should be remembered.

bin/mysqld --initialize --user=mysql

insert image description here

wdKpZLg&x1Bd

Executing the above command will generate the default initialization password of the root user, and will automatically generate a data directory under the mysql directory for us
, so there is no need to create it manually.

7. Set up SSL secure encrypted connection

5.7 The database begins to access data to improve ssl security and confidentiality, and requires encryption and decryption for transmission.

bin/mysql_ssl_rsa_setup

8. Configure Mysql environment variables

Similar to the java environment variable configuration, add the msyql configuration to the end of the /etc/profile file, so that msyql can be used anywhere.

echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile

insert image description here

9. Start the msyql service in a safe way

Execute the command in the msyql installation directory.

bin/mysqld_safe --user=mysql &

Among them, & indicates that the msyql server is started in the background.
Out of service:

bin/mysqladmin shutdown -p

10. Other operations

Finally, we can also perform other configurations on the installed msyql. For example, modify the initialization password, modify the remote access to mysql
, etc.

Modify the root default password:

Enter the msyql installation directory and execute the following commands.

bin/mysqladmin -uroot password '123456' -p

insert image description here

Modify remote access to msyql:

First log in to msyql on the cloud server to view the user.
By default, there is a root super administrator user, but the host is localhost for local access. If you want remote access, there are two solutions
.
Option 1: Directly modify the host address of the current root user to %, that is, all addresses can be accessed

use mysql
update user set host ='%' where user = 'root';

But not recommended.
Option 2: Add a remote access user and set database operation permissions.

CREATE USER 'dyk'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.* TO 'dyk'@'%';
FLUSH PRIVILEGES;

Then make a remote connection, but there may be a connection failure error.
insert image description here
Of course, the above my mysql only needs to release the port on the Alibaba Cloud esc server. If it is your own virtual machine system, you need to instruct the operation development port number, or directly close the firewall.

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

Or just turn off the firewall

#查看防火墙状态: 
systemctl status firewalld.service

insert image description here

#执行关闭命令: 
systemctl stop firewalld.service

#再次执行查看防火墙命令:
systemctl status firewalld.service

insert image description here

#执行开机禁用防火墙自启命令  : 
systemctl disable firewalld.service

install redis

1. Install the C++ environment in the virtual machine

yum -y install gcc-c++

2. Unzip Redis

tar -zxvf redis-4.0.14.tar.gz

Then move and copy the same to the redis directory of usr/local

3. Compile and install

Enter the redis directory to compile and install
cd to the /usr/local/redis directory, enter the command make to execute the compilation command

cd /usr/local/redis/
make
make PREFIX=/usr/local/redis install

4. Modify the configuration file

Operate in the redis directory.

# 修改配置文件
vi redis.conf
# Redis后台启动
修改 daemonize 为 yes
# Redis服务器可以跨网络访问
修改 bind0.0.0.0
# 开启aof持久化,这个可以不做
appendonly yes

The content of the configuration file is a bit too much, you can use the /string carriage return operation to find the place to be modified, and n to find the next one.

5. Start Redis

Note development port 6379

bin/redis-server redis.conf

install nginx

1. Nginx download address: http://nginx.org/en/download.html

2. Upload the installation package to Linux

Similarly, upload to the usr/local/src directory

3. Unzip Nginx

tar -zxvf nginx-1.20.1.tar.gz 

4. Move the nginx decompressed package to /usr/local/nginx

mv nginx-1.20.1 /usr/local/nginx

5. Install Nginx dependency environment gcc

Nginx is developed in C/C++ language. It is recommended to run on Linux. To install Nginx, you need to compile the source code downloaded from the official website first. The compilation depends on
the gcc environment, so you need to install gcc. Always y (agree) (requires internet)

yum -y install gcc-c++

6. Connect to the network and install the Nginx dependent environment pcre/zlib/openssl. y indicates that if prompted during the installation process, y is selected by default

yum install -y pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel

7. Compile and install nginx

Enter the nginx installation directory.

./configure  #配置nginx(在nginx-1.16.1目录中执行这个配置文件)
make #编译nginx
make install #安装nginx

8. Go to the sbin directory and start

cd /usr/local/nginx/sbin #进入/usr/local/nginx/sbin这个目录
./nginx #启动Nginx

Note that if the following error occurs during the startup process: it means that the corresponding logs log directory and file are not generated during the installation and configuration. At this time, you
need to manually create and grant permissions.
insert image description here
Enter the nginx installation directory:

 cd /usr/local/nginx/
mkdir logs
chmod 700 logs

Then re-execute the seventh step above

9. Open port 80 for external access to Linux

If you are using your own virtual machine, you need to open the port through the command or close the firewall directly. If it is a cloud server development security group

10. Stop Nginx server

cd /usr/local/nginx/sbin #进入/usr/local/nginx/sbin这个目录
./nginx -s stop #停止Nginx

deploy

In enterprises, the Linux system is generally used as the Web application server, so we need to build the project running
environment in the Linux system. To build a running environment on a Linux system, you need to install jdk, myql, and tomcat related software. Since it is a srpingboot project, I don't need to use tomcat here, it has built-in tomcat

step

  1. Software installation (previously completed)
  2. import data to mysql
  3. Type the project into a jar package and upload it to the server

1. Linux system myql import database

2. Export the jar package and deploy it to the Alibaba Cloud server

Check the ip of the configuration file before packaging, etc.
insert image description here
Clean and then install

Project upload and start

Create a new directory myproject in /src/local/

mkdir myproject
cd myproject/
mv msbd-0.0.1-SNAPSHOT.jar msbd.jar

start command

java -jar msbd.jar

Note: Before starting the project, make sure that all configuration files of the project itself have been changed to the production environment. At the same time, the corresponding software
msyql and redis have been opened

The above method completes the deployment of the springboot project simply and quickly.
However, the project runs as the foreground process by default, and the project is closed when the window is closed. In addition, our project has only one instance, and the actual
application does not provide high availability, so it can be slightly improved on this basis.

Project Nginx deployment improvements

Next, we use the reverse proxy of nginx to implement multi-instance deployment of the project, and start the project as a background process at the same time.

nohup java -jar msbd.jar --server.port=9011 > logName.log 2>&1 &
nohup java -jar msbd.jar --server.port=9012 > logName2.log 2>&1 &

Command details:

nohup: Run the command without hanging up, and continue to run the corresponding process after exiting the account.

logName.log : Nohup redirects the output of the command to the specified "log file name. log" file
in the current directory, that is, the output content is not printed on the screen, but is output to the "log file name. log" file . If no filename is specified, nohup.out will be created in the current directory. If the nohup.out file in the current directory is not writable, the output will be redirected to the $HOME/nohup.out file. The command specified by the Command parameter cannot be invoked if no file can be created or opened for appending. 2>&1: 2 is standard error, 1 is standard output, this command is equivalent to redirecting standard error to standard output. Here & is equivalent to standard error and standard output, that is, output both standard error and standard output to the specified "log file name.log" file.
If you want to close it, for the process that has been executed in the background, you can use the ps command to view the corresponding pid, and simply sudo kill pid.

View the running process of the java project through the command:

ps -ef | grep java

insert image description here

Nginx performs load balancing configuration.

Create a new custom configuration folder myconfig in the /usr/local/nginx/conf directory, and introduce a load
balancing configuration file msdict.conf into this folder

 mkdir myconfig
touch msdict.conf

msdict.conf

# 代理多个服务
upstream msdict-server{
    
    
    server 127.0.0.1:9011;
    server 127.0.0.1:9012;
}
server {
    
    
    listen 9001;
    server_name 127.0.0.1;
# 代理跳向我们的后台接口服务
    location / {
    
    
    proxy_pass http://msdict-server;
    proxy_connect_timeout 600;
    proxy_read_timeout 5000;
    }
}

Introduce external configuration files in nginx's nginx.conf configuration file

 include myconfig/*.conf;

insert image description here

Finally, start nginx.

sbin/nginx -s reload

vue project deployment

1. Compile the vue project locally first. Before
compiling the vue project formally, uniformly modify all the configuration files, server addresses and other information that need to be modified:

insert image description here
insert image description here

Next, execute the instruction to compile.

npm run build

insert image description here
Among them, the index.html under dist is the default home page after the project is compiled.

Upload the dist directory to the server

Here, due to limitations, the project page is uploaded to the /usr/local/myproject/web/msweb directory
of the server. Here, we rename the dist directory to msweb after uploading
insert image description here

Next, restart Nginx.
insert image description here
insert image description here

nginx deployment vue project routing refresh 404

insert image description here

 location / {
    
    
            #root   html;
            #
           root /usr/local/myproject/web/msweb;
            index  index.html index.htm;
           try_files $uri $uri/ /index.html;
        }

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_44866153/article/details/122201752
Recommended