1. Install dependency packages

[root@localhost ~]# yum -y install GCC pcl-devel zlib-devel openssl openssl-develCopy the code

2. Download and decompress the installation package

[root@localhost ~]# mkdir -p WWW /server/nginx # change directory [root@localhost ~]# CD / WWW /server/nginx # download [root@localhost nginx]# wget/ / nginx.org/download/nginx-1.21.0.tar.gz# unzip [root@localhost nginx]# tar -xvf nginx-1.21. 0.tar.gz
Copy the code

3. Install nginx

[root@localhost nginx]# CD/WWW /server/nginx # CD nginx-1.21. 0Run the [root@localhost nginx-] command1.21. 0]# ./configure --prefix=/usr/Run the make command [root@localhost nginx-]1.21. 0]# make # run make install [root@localhost nginx-1.21. 0]# make install
Copy the code

4. To configure nginx. Conf

[root @ # backup Nginx configuration file localhost conf] # cp/usr/local/Nginx/conf/Nginx. Conf/usr/local/Nginx/conf/Nginx. Conf. # bak open configuration file [root@localhost conf]# vi /usr/local/nginx/conf/nginx.confCopy the code

Change the port number to 8089, because apeache may occupy port 80, apeache port should not be changed, we choose to change nginx port.

Change localhost to your server’s IP address, or leave it unchanged.

5. Enable the firewall

I. Enable port 8089

[root@localhost conf]# firewall-cmd --zone=public --add-port=8089/tcp --permanent
Copy the code

Ii. Restart the firewall

[root@localhost conf]# firewall-cmd --reload
Copy the code

Iii. View opened ports

[root@localhost conf]# firewall-cmd --list-ports
Copy the code

6. Run the following command to go to the sbin directory of Nginx and start Nginx

[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
Copy the code

I. Common commands used after the installation is complete

Nginx./nginx -s stop Stop nginx./nginx -s reload nginx./nginx -v View the Nginx versionCopy the code

7. Check whether nginx is started successfully

[root@localhost sbin]# ps -ef | grep nginx
root       4338      1  0 16:48 ?        00:00:00 nginx: master process ./nginx
nobody     4339   4338  0 16:48 ?        00:00:00 nginx: worker process
root       4341   1549  0 16:48 pts/0    00:00:00 grep --color=auto nginx
Copy the code

8. After modifying the configuration file, restart the system by specifying the configuration file

[root @ # set the configuration file to restart the localhost sbin] # / usr/local/nginx/sbin/reload nginx - s - c/usr/local/nginx/conf/nginx. Conf # test for a configuration file is correct [root@localhost sbin]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Copy the code

Create a soft link to start nginx

# set up soft links [root @ localhost sbin] # ln -s/usr/local/nginx/sbin/nginx/usr/sbin / # start [root @ localhost sbin] # # nginx view nginx versions [root@localhost ~]# nginx -v nginx version: nginx/1.21. 0
Copy the code

10. Access the IP address and port number of the server

11. Configure nginx to start automatically upon startup

I. Create a nginx.service, press I to Enter editing mode, press Esc to modify the configuration file, Enter :wq, and press Enter to save the configuration

[root@localhost system]# CD /usr/lib/systemd/system/ # create file [root@localhost system]# touch nginx.service # edit file contents [root @ localhost system] # vi/usr/lib/systemd/system/nginx. Service # given executable permissions/root @ localhost system # chmod + x /usr/lib/systemd/system/nginx.serviceCopy the code

Ii. Edit the file

After=network.target remote-fs.target NSS -lookup. Target # description Service category [Service] # some specific running parameters of the Service set Type=forking # background running form PIDFile=/usr/Local/nginx/logs/nginx. Pid ExecStartPre = # of pid file path/usr/Local/nginx/sbin/nginx - t - c/usr/local/nginx/conf/nginx. Conf # start preparing ExecStart =/usr/Local/nginx/sbin/nginx - c/usr/local/nginx/conf/nginx. Conf # startup command ExecReload =/usr/Local /nginx/sbin/nginx -s reload # ExecStop=/usr/Local /nginx/sbin/nginx -s stop # ExecQuit=/usr/Local /nginx/sbin/nginx -s quit # PrivateTmp=true[Install] WantedBy=multi-user. TargetCopy the code

Iii. View the file content

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile= /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Copy the code

Iiii. Start the service

# Before starting the service, To start the service, run the systemctl daemon-reload command [root@localhost ~]# systemctl start nginx [root@localhost ~]# [root@localhost ~]# systemctl enable nginx # check nginx [root@localhost ~]# ps -ef | grep nginx root14869      1  0 17:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    14871  14869  0 17:37 ?        00:00:00 nginx: worker process
root      14893   1549  0 17:37 pts/0    00:00:00 grep --color=auto nginx
Copy the code

* Configure the startup mode of systemctl

Systemctl status nginx # status systemctl start nginx # start systemctl stop nginx # stop systemctl restart nginx # restartCopy the code

Iii. run the reboot command to restart the nginx and check whether the nginx automatically starts

[root@localhost ~]# reboot
Connection to 192.168. 0197. closed by remote host.
Connection to 192.168. 0197. closed.

C:\Users\beauty>ssh root@192.168. 0197.
root@192.168. 0197.'s password: Last login: Sat 2021 from Jun 5 16:09:35 192.168.0.194 / root @ localhost ~ # ps - ef | grep nginx root 1081 1 0 "? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1084 1081 0 17:39 ? 00:00:00 nginx: worker process root 1567 1548 0 17:40 pts/0 00:00:00 grep --color=auto nginxCopy the code