1. Nginx download
Open the official website, download the corresponding Linux version according to your needs, here we choose nginx-1.18.0 pgp stable version, CentOS-7 system to illustrate
Official website address: http://nginx.org/en/download.html
2. Nginx installation
(1) Xshell connects to the Linux server, the cd command creates a nginx directory in the /usr directory, and then executes the ls command to view the result of the creation, namely:
cd /usr
mkdir nginx
ls
(2) There are two installation methods:
①, wget command installation
②, upload the downloaded Nginx tar.gz compressed package to the Linux server through the Xftp tool, and then install it through the decompression command.
If it is installed through the wget command, select the version to be downloaded on the Nginx download page, copy the link address with the right mouse button, and then install it through the wget command in the newly created /usr/nginx directory, namely:
cd /usr/nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
Special note: Many times the wget command cannot be used on the CentOS system, and it always prompts: -bash: wget: command not found. If this error occurs, please refer to the blog: https://blog.csdn.net/weixin_43184774/article/ details/110191309
Here we use the Xftp tool to upload the downloaded Nginx tar.gz compressed package to the Linux server as an example.
(3) The cd command enters the newly created nginx directory, namely cd /usr/nginx, and then opens the Xftp tool above the Xshell
(4) Execute the following command to decompress the compressed file package of nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
(5) Execute the following command to enter the decompressed nginx folder
cd nginx-1.18.0
(6) Execute the following command to use the default configuration of nginx.
Note: Once this command is executed, the nginx file is moved to the /usr/local/ directory by default
./configure
(7) Execute the following commands to compile and install
make
make install
pay attention:
If the following error message appears when compiling and installing:
make: *** No rule to make target `build', needed by `default'. Stop.
solution:
①, install the following configuration
yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
②, reconfigure
./configure
③, compile
make && make install
(8) Execute the following command to find the installation path of Nginx
whereis nginx
(9) The cd command enters the /usr/local/nginx/sbin directory, you can see an executable file nginx, and then directly execute ./nginx
cd /usr/local/nginx/sbin
./nginx
(10) After running, directly access the server ip (if it is a local computer, enter localhost), you can see the welcome page of nginx, indicating that the installation is successful
3. Precautions for Nginx installation
Problem description: After Nginx is installed, the page cannot be accessed
solution:
(1) Execute the following command to check whether the installation is good, if it appears as shown in the figure below, it means that nginx has been started normally
ps -ef|grep nginx
(2) If you still cannot access the page at this time, you need to confirm whether port 80 is enabled in the security group policy of the server
(3) If port 80 is enabled and still cannot be accessed, enter the cd command into the /usr/local/nginx/sbin directory and execute the command: ./nginx -t to query where the nginx configuration file nginx.conf is, and this The statement can also verify whether the nginx.conf file is correct, the correct format will prompt: test is successful
cd /usr/local/nginx/sbin
./nginx
(4) Execute the following commands to edit the nginx.conf configuration file
vi /usr/local/nginx/conf/nginx.conf
(5) Edit the mapping path in the nginx.conf configuration file and modify this path to the path you actually store.
(6) Visit the server ip again (if it is a local computer, directly enter localhost), you can see the welcome page of nginx
4. Basic operation of Nginx
4.1 Nginx startup
Nginx startup code format: nginx installation directory address-c nginx configuration file address, namely:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4.2 Nginx stop
There are three ways to stop Nginx: calmly stop, fast stop, and forced stop
4.2.1 Calmly stop
(1) Execute the following command to view the process number
ps -ef|grep nginx
(2) Execute the following command: kill -QUIT port number, kill the process, for example, my port number is 26509
kill -QUIT 26509
4.2.2 Quick stop
(1) Execute the following command to view the process number
ps -ef|grep nginx
(2) Execute the command: kill -TERM port number, or kill -INT port number, kill the process, for example, my port is 26509
kill -TERM 26509,或kill -INT 26509
4.2.3 Forced stop
Execute the following command to forcibly stop the nginx service
pkill -9 nginx
4.3 Nginx restart
After Nginx is stopped, the cd command enters the sbin directory of nginx, enter the command: ./nginx to restart
cd /usr/local/nginx/sbin
./nginx
note:
Attentive students will find that sometimes after executing the ./nginx command in the /usr/local/nginx/sbin directory, the following error message is displayed
nginx:[emerg] bind() to 0.0.0.0:80 failed (98:Address already in use)
Solution:
Simple and crude method: Enter the cd command into the /usr/local/nginx/sbin directory and execute it. The /nginx -s reload command can restart the Nginx service.
cd /usr/local/nginx/sbin
./nginx -s reload