Build a Linux ECS at the front end, explain the configuration of nginx and deploy your own projects to the server

Dapeng_ yp 2021-09-15 11:06:43

build Linux Cloud server

Purchase and basic configuration

  • The cloud service
  • Register as an alicloud user ( You can register without Alipay account. ), Buy ECS Cloud server
    Purchase address
  • Purchase options
    1. Region and availability zone : Choose what you want
    2. Example specifications : Choose according to your family status
    3. Mirror image : Default choice CentOs that will do
    4. Network type : Default
    5. Public bandwidth : Choose according to your own economic conditions
    6. Quantity and duration of purchase : Choose... According to your needs
      I'm poor. I bought a new account 88 A novice version for a year Address
  • Check the products you buy
    Check the products you buy
  • Check strength ( Left menu bar - Strength and mirror image - example )
  • Several important places
    1. ip Address : Public network ip: Is the address used by the client to connect to the server ( host )
    2. Reset password : operation --> more --> password / Secret key --> Reset instance password
    3. remember :ip Address and your own reset password ( If you can't remember, come back and reset )
  • Tools
    free :putty finalshell git-bash cmder mac Command to end linux desktop
    charge :xshell SecureShell
    I used it here putty Tools to link
  • Link and login
    1. open putty Input :host Name( Public network ip)、port( Default 22 Just go ) Can be confirmed
      Click to connect , The first 1 You will see the prompt next time , choice yes Can
    2. Going in will let you enter login as: General input root that will do
    3. password: The password you set (linux Is implicit for passwords in , After input, no characters can be seen in the terminal )

Directory structure

Linux System ( With CentOs7 For example ) Main directory structure ,Linux The directory is a tree structure .

name explain
/ root directory
bin Common commands
sbin Programs that system administrators can run
boot linux The file directory required by the kernel and boot system programs
etc It stores various configuration files and subdirectories used in system management
home Used to store the user's home directory , except root Super administrator home directory is not here
mnt It is generally the mount directory used to store the mounted storage device
root Super users root Home directory
tmp Temporary file directory , Important files or directories cannot be stored here
usr Store the software installed by the user , The directory that takes up the most hard disk space
var A table of contents that changes frequently Such as log files

Basic commands

You can read this article : Develop common commands

name explain
cd route Enter the designated path
cd .. Go back to the next level
ls View a list of files or directories in a directory
ls -d View catalog information
ls -l Tabular form
ls -h The file size will be calculated by itself (k/m/g)
cat route View file contents
tail -f route View the latest changes in the file in real time
head -n route n Numbers View the top lines of the current file
unzip decompression file

Software installation

linux You also need to install software ( Services are also software ) , To install nginx,linux Open source operating system . There must be a dependency between software and software .

  1. Source code installation Download it in the official software Source code (c c++) Build and install Dependency needs to be resolved manually
  2. rpm install rpm Be similar to windows Installation package , Using it to install also requires manual resolution of dependencies
  3. yum install Be similar to npm, Automatically resolve dependencies required during installation

notes :yum Demand source station , The origin stations are all abroad , So use yum Install the software , You need to switch the mirror source . Now all virtual machines have helped you complete .

Linux The system starts

The boot process

Linux The startup process of the system is not as complicated as you think , The process can be divided into 5 Stages

  1. Kernel boot
  2. function init
  3. System initialization
  4. Set up the terminal
  5. User login system

Operation level

Be careful : In the cloud server , Don't set its run level .

Linux Allow for different occasions , Assign different boot programs , This is called " Operation level "(runlevel). in other words , Start up according to " Operation level ", Determine which programs to run .

  • Linux The system has 7 Operating levels (runlevel):
  1. Operation level 0: System shutdown status , The system default run level cannot be set to 0, Otherwise it will not start properly To turn it off
  2. Operation level 1: Single user working state ,root jurisdiction , For system maintenance , No remote login
  3. Operation level 2: Multi user status ( No, NFS)
  4. Operation level 3: Full multi-user state ( Yes NFS), Log in and enter the console command line mode Command line mode
  5. Operation level 4: The system is not in use , Retain
  6. Operation level 5:X11 Console , Log in and enter the figure GUI Pattern Graphic mode , Multiple users also have commands
  7. Operation level 6: The system shuts down and restarts normally , The default run level cannot be set to 6, Otherwise it will not start properly restart
  • stay centos7 After that, the run level was slightly modified
    init Level systemctl target
    • 0:
    • 1:
    • 2:
    • 3:
    • 4: nothing
    • 5:
    • 6: nothing
  • The way to view the default run level is

    systemctl get-default

  • How to set the default run level

    init 3

  • Set to multi-user command mode

    systemctl set-default

  • To turn it off

    init 0

  • restart

    init 6

rm -rf /usr/share/man/man8/nginx.8.gz

Nginx Detailed explanation

ginx Is an open source free lightweight Web The server / Reverse proxy server and email (IMAP/POP3) proxy server . It is characterized by less memory , Strong concurrency , Use nginx There are many website users , Such as Baidu 、 JD.COM 、 Sina 、 NetEase 、 tencent 、 Taobao, etc.
Official website
Chinese document

1、 install

In the installation nginx Let's take a look at linux Server version

cat /etc/redhat-release

Here mine linux yes 8.1 Version of (CentOs 8.x)

Mode one :yum install

  • The default port number is 80
  • Default profile name nginx.conf
  • nginx Default web In the path /usr/share/nginx/html ( Store our front-end projects )
  1. install :

    yum install nginx

  2. Enable and start Nginx service

    sudo systemctl enable nginx
    sudo systemctl start nginx

    Of course nginx By default, it will be installed to /usr/sbin/nginx Therefore, you can directly execute the following command to start


  3. To verify that the service is running , Check its status

    sudo systemctl status nginx

  4. View the running port in the current server

    netstat -tunpl

    -tunpl explain :

    • t: tcp
    • u: udp
    • n: Displays the port application name
    • p: pid
    • l: Only view running
  5. Extension method

    • Stop server

      Method 1 : sudo systemctl stop nginx
      Method 2 :/usr/sbin/nginx -s stop
      Method 3 : Kill process pkill nginx

    • restart

      /usr/sbin/nginx -s reload Will not stop the server , Only reload nginx Configuration makes the configuration effective Smooth restart
      /usr/sbin/nginx -s restart Stop service first , Restart the service

Mode two : Custom directory installation

  1. Install tools and Libraries
    Dependent software must be installed , Or the back nginx The installation will not work properly .

    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel

    explain :

    • PCRE It's a Perl library , Include perl Compatible regular expression library .nginx Of http Module USES pcre To parse regular expressions
    • zlib The library provides many ways to compress and decompress , nginx Use zlib Yes http The contents of the package go on gzip
  2. Create a folder

    cd /usr/local
    mkdir nginx
    cd nginx

  3. download tar package

    tar -xvf nginx-1.19.0.tar.gz

  4. install nginx

    Entry directory :cd nginx-1.19.0
    Carry out orders :./configure
    perform make command : make
    perform make install command :make install

  5. Check the installation
    cd Go to the installation directory just configured

    cd /usr/loca/nginx/
    ./sbin/nginx -t

    Information output under normal conditions :

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2、 start-up nginx

  • start-up nginx service


    notes : When starting, make sure 80 The port is not occupied

    netstat -tunpl

    other :

    /usr/local/nginx/sbin/nginx -s reload # Reload configuration file
    /usr/local/nginx/sbin/nginx -s stop # stop it Nginx
    /usr/local/nginx/sbin/nginx -t # Check whether the configuration file is written correctly

3、 solve 80 Port occupied

 solve Nginx Startup Report nginx: [emerg] bind() to failed (98: Address already in use)
  1. When this happens, it's usually 80 Port occupied , Use sudo fuser -k 80/tcp Order to shut down 80 Port can

    sudo fuser -k 80/tcp

  2. If you don't succeed, you can close the firewall first and then close 80 Give it a try

    Use command :systemctl status firewalld.service View firewall status
    Use command :systemctl stop firewalld.service Turn off the running firewall

4、 Completely uninstall and delete CentOS Upper Nginx

  1. First check to see if it is running nginx

    ps -ef | grep nginx

  2. If you stop before running nginx

    /usr/local/nginx/sbin/nginx -s stop

  3. see nginx The installation directory

    whereis nginx

  4. Delete related files

    rm -rf /usr/sbin/nginx
    rm -rf /etc/nginx
    rm -rf /etc/init.d/nginx

  5. Use yum uninstall nginx And related dependencies

    yum remove nginx

  6. Check for residual files , If so, perform steps 3 Continue to delete

    whereis nginx


After successful installation , Enter the directory after installation
cd /usr/local/nginx, See the following 4 There are three main directories

  • conf: The configuration file The configuration virtual host file is here
  • html: nginx Default web Root path location
  • logs: Log files
  • sbin: Binary program start-up / stop it / The overload service command is here

To configure nginx.conf

Profile syntax

http paragraph => server paragraph => location paragraph

  • http paragraph : Is to include all Server paragraph , the nginx All virtual host configurations are included
  • server paragraph : One server That is, a virtual host configuration , How many virtual hosts there are later depends on server paragraph
  • location paragraph : Every location Segment represents a module function configuration in the virtual host Domain name background address
  • notes :
    • The semicolon after the configuration cannot be less ,{} There should be a space between and configuration
    • nginx The end of each line in the configuration file should be written A semicolon ;
location /a {
root /a
location /b {
alias /a

Configure the virtual host

On a server , Sometimes multiple different domain names are bound , At this time, we need to configure nginx Let it support multi domain virtual hosts ( Or multi port web The server ).
step :

  1. stay nginx Add a new... In the configuration file server node
  2. stay server In nodes , Modify the corresponding site directory
  3. heavy load nginx service nginx -s reload
server {
listen 80; # port
server_name; # domain name
root /web/; # Website directory path
index index.html; # Default file
charset utf-8;
access_log logs/ main;
error_page 404 403 500 502 503 504 /50x.html;

Turn on gzip

The compressed file size has become smaller , The transmission is faster , Improve the access speed and concurrency of the website . At present, browsers on the market support GZIP Of .IE6 The following support is not good , There will be disorder .
Operation steps :

  1. Modify the configuration , Turn on gzip Compress
  2. heavy load nginx To configure
  3. Test for compression
## http Node Settings
# Turn on gzip
gzip on;
# Settings will be gzip Minimum length of compressed response
gzip_min_length 200;
# Compression level 1-9
gzip_comp_level 6;
# Compress the number of blocks in the buffer and the size of each block
gzip_buffers 32 4k; # | 16 8k;
# Set the minimum required to compress the response HTTP Request version
gzip_http_version 1.0;
gzip_types text/plain text/css text/xml application/xml application/json application/javascript;

Start publishing your own project to the server

One 、 Buy an alicloud server

As mentioned above, we should pay attention to the following points

  1. Record your own : Public network ip
  2. operation --> more --> password / Secret key --> Reset instance password
  3. operation --> more --> Network and security groups -> Security group configuration
    After entering , View the list of rules , Is there any 80 port , If not, you need to add a rule

Two 、putty Login server

  1. open putty Input :host Name( Public network ip)、port( Default 22 Just go ) Click ok
    Click to connect , The first 1 You will see the prompt next time , choice yes Can
  2. Going in will let you enter login as: General input root that will do
  3. password: The password you set (linux Is implicit for passwords in , After input, no characters can be seen in the terminal )

3、 ... and 、 Installation on server nginx

  1. There are already installation methods above , Follow the steps to complete the installation
  2. Directly through the domain name or ip Address access nginx The server ( Different versions nginx The display is different )
  3. nginx Under the html The file stores the files to be packaged and deployed at the front end

Four 、 Upload project

Here we use fileZlia Visual chemical tools to assist in completing the just uploaded work

  1. open fileZlia Tool connection linux The server
  2. Go in /usr/local/nginx/sbin/nginx/html Under the table of contents ,
  3. Will we vue The project is packaged dist Put it here
  4. Here if dist The meeting is slow , We can put a file
  5. decompression
    Enter the terminal to execute

    cd /usr/local/nginx/sbin/nginx/html
    unzip decompression

5、 ... and 、 To configure niginx Of nginx.conf

Go in /usr/local/nginx/sbin/nginx/conf Under the table of contents , find nginx.conf
To configure server term

 location / {
root html/dist;
index index.html index.htm;
Please bring the original link to reprint ,thank
Similar articles