Simple deployment from 0 to 1

Time:2021-2-27

For the current individual developers, it is very convenient to use the cloud server. After writing the project code, there will be an impulse to quickly deploy to the server. Today, let’s practice the simple 0-1 server deployment.

First of all, you need to have a cloud server. Take my Alibaba cloud server as an example. The system isCentOS 7.3

1. Remote connection

If you want to deploy on the server, you must first connect to the server, which can be controlled through alicloud’s official websiteBrowser remote connectionLog in to the server, but it’s troublesome. You need to log in again every once in a while. In addition, you can also use SSH to connect through account password or key, as follows:

#Connect through the account and password, generally root. You need to enter the password after the connection is successful
ssh [email protected]

#Connect through the key, and yourKEY is the local path of the key
ssh [email protected] -i yourKey

2. Nginx configuration

Today’s server deployment is basically inseparablenginx, easy to configure and use, very friendly for individual developers.

2.1 nginx installation

yum install -y nginx

After successful installation, you can use – V to view the version, here is 1.16.1

nginx -v

2.2 nginx command

Start nginx

have access toLinuxSystem tools forSystemdTo startnginx, can also be usednginxBuilt in command:

systemctl start nginx
#Or
nginx

#Set auto start
systemctl enable nginx

Stop nginx

When you want to stopnginxYou can use the stop command to:

systemctl stop nginx
#Or
nginx -s stop

Restart nginx

When you change the configuration of nginx, you often need to restart at this timenginxService configuration can take effect:

systemctl restart nginx
#Or
nginx -s reload

2.3 configuring nginx

nginxAfter installation, the default path is/etc/nginx/, if not found in this path, you can usenginx -tCommand to view the installation path:

nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

It’s verified herenginx.confnamelynginxThe default content is as follows:

#Some configurations have been omitted
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Here, we mainly focus on the configuration of the server. Take the server of HTTP protocol as an example to analyze it line by line

  • Listen: indicates to listen on port 80 of IPv4 & IPv6 and set this port as the default service
  • server_ Name: the name of the current service (IP or URL)
  • Root: represents the root path
  • Include: refers to the configuration under this path, which can be ignored here
  • Location: indicates the rules corresponding to the path after the domain name
  • error_ Page: indicates the redirection rule after an error occurs

Front end project

For the front-end project, it is essentially a bunch of static files after packaging, and the corresponding server is configured_ Name and root are OK. Here, the configuration domain name is the keywww.example.comAfter packaging, the file storage server directory under dist is/home/admin/wwwFor example:

#Some configurations have been omitted
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  www.example.com;
    root         /home/admin/www;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Server project

For server-side projects, a port will be monitored locally to run related servicesnginxConfigure the reverse proxy to enable the accessed service to reverse proxy to the corresponding port. Take port 3000 as an example

#Some configurations have been omitted
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  www.example.com;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
      proxy_pass http://127.0.0.1:3000;
    }
}

You need to restart after configuration changesnginxThe service will take effect

3. Upload files

There are many ways to upload files, which are mainly introduced herescpCommand to the remote server user[email protected]For example:

#The remote server's / remote/ index.html  Download the file to the local / local directory
scp [email protected]:/remote/index.html /local

#Download the entire remote directory of the remote server to the local / local directory
scp -r [email protected]:/remote/ /local

#Set local / local/ index.html  File, upload to the / remote directory of the remote server
scp /local/index.html [email protected]:/remote

#Upload the whole local / local directory to the / remote directory of the remote server
scp -r /local [email protected]:/remote

For the front-end project, if you want to upload all the files in the dist directory, but do not want to upload the dist directory, you can use it at this timewildcardTo upload all files:

#Upload all the files in the local / local directory to the / remote directory of the remote server
scp -r /local/* [email protected]:/remote

Before uploading, you need to ensure that the directory of the server has correct read and write permissions, otherwise it will appearSCP Permission deniedError, specific file permissions can be accessed throughls -lsee:

ls -l
# -rw-r--r-- 1 root root  2376 Feb 17 20:37 404.html
# drwxr-xr-x 2 root root  4096 Feb 17 00:43 about
# drwxr-xr-x 4 root root  4096 Feb 17 00:43 assets
# drwxr-xr-x 2 root root  4096 Feb 17 00:43 atlas
# -rw-r--r-- 1 root root 51022 Feb 17 20:37 head.png
# -rw-r--r-- 1 root root 13927 Feb 17 20:37 index.html
# drwxr-xr-x 8 root root  4096 Feb 17 00:43 posts

The second column represents the file permission, and the first letter is the file type,dRepresents a catalog file,-The following RWX represents the read / write / execute permissions, which are divided into three groups, corresponding to the owner / group / other groups respectively

If you don’t have permission, you can use the following command to set everyone to read, write and execute:

chmod 777 yourDir

4. HTTPS configuration

The following steps are required to configure your site as HTTPS:

  1. Apply for a certificate and download it. For example, in Alibaba cloud’sSSL certificateModule can apply for a free certificate, a period of one year
  2. Upload the certificate file to the corresponding directory of the server
  3. Change nginx configuration

ssl_certificateandssl_certificate_keyThe storage paths of certificate file and private key are shown as follows:

#Some configurations have been omitted
server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;
    server_name  www.example.com;
    root         /home/admin/www;

    ssl_certificate "/etc/pki/nginx/www.example.com.pem";
    ssl_certificate_key "/etc/pki/nginx/private/www.example.com.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}

When deploying an HTTPS site, a common requirement is to automatically jump the HTTP site to HTTPS,nginxThe configuration is as follows:

server {
    listen      80;
    server_name example.com www.example.com;
    return 301  https://$server_name$request_uri;
}

The above is the whole content of this article. If you have any mistakes, please correct them~