nginx compilation and installation
Official source code package download address
https://nginx.org/en/download.html
Example: Compile and install
#1. Install related packages [root@test1 ~]# yum -y install gcc pcre-devel openssl-devel zlib-devel #Source code installation requires preparing a standard compiler in advance [root@test1 ~]# useradd -s /sbin/nologin nginx [root@test1 ~]# cd /usr/local/src/ #2. Unzip the package [root@test1 src]# tar xf nginx-1.20.2.tar.gz [root@test1 src]# ls nginx-1.20.2 nginx-1.20.2.tar.gz #3.Configuration [root@test1 src]# cd nginx-1.20.2/ [root@test1 nginx-1.20.2]# ./configure --prefix=/apps/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module #4. Compile and install [root@test1 nginx-1.20.2]# make & amp; & amp; make install #Modify permissions [root@test1 nginx-1.20.2]# chown -R nginx.nginx /apps/nginx [root@test1 nginx-1.20.2]# ll /apps/nginx Total usage 0 drwxr-xr-x 2 nginx nginx 333 Nov 6 10:39 conf drwxr-xr-x 2 nginx nginx 40 Nov 6 10:39 html drwxr-xr-x 2 nginx nginx 6 Nov 6 10:39 logs drwxr-xr-x 2 nginx nginx 19 Nov 6 10:39 sbin conf: Saves all nginx configuration files, of which nginx.conf is the core and main configuration file of the nginx server. Other .conf are used to configure nginx-related functions. For example, the fastcgi function uses fastcgi.conf and There are two files, fastcgi_params. The configuration file generally has a template configuration file with .default as the suffix. You can copy it and remove the default suffix when using it. The web files of the nginx server are saved in the html directory, but they can be changed to other directories to save the web files. There is also a 50x web file that is the default error page prompt page. logs: Used to save access logs, error logs and other logs of the nginx server. The logs directory can be placed in other paths, such as /var/logs/nginx. sbin: Saves the nginx binary startup script and can accept different parameters to achieve different functions. [root@test1 nginx-1.20.2]# ls /apps/nginx/sbin/ nginx [root@test1 nginx-1.20.2]# ln -s /apps/nginx/sbin/nginx /usr/sbin/ #View compiled version [root@test1 nginx-1.20.2]# nginx -V nginx version: nginx/1.20.2 built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) built with OpenSSL 1.1.1k FIPS 25 Mar 2021 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module [root@test1 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@test1 ~]# nginx #Start nginx [root@test1 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128
Create nginx self-starting file
#Copy the service file generated by the yum installation of the same version of nginx [root@test1 ~]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/apps/nginx/run/nginx.pid #Specify the directory of the pid file, which is in the logs directory by default. Optional configuration ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID LimitNOFILE=100000 [Install] WantedBy=multi-user.target #Create the directory where pid is stored [root@test1 ~]# mkdir /apps/nginx/run/ #Modify configuration file [root@test1 ~]# vim /apps/nginx/conf/nginx.conf pid /apps/nginx/run/nginx.pid;
Verify autostart files
[root@test1 ~]# systemctl daemon-reload [root@test1 ~]# systemctl enable --now nginx Createdsymlink/etc/systemd/system/multiuser.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [root@test1 ~]# ll /apps/nginx/run Total usage 4 -rw-r--r-- 1 root root 5 Nov 6 11:44 nginx.pid
Problem description: Error when starting nginx
[root@test1 ~]# systemctl enable --now nginx Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details
Solution: According to the prompts, execute systemctl status nginx to check the situation. If you see the prompt Starting nginx… nginx already running. Just kill the process!
Check the nginx.pid file in the /apps/nginx/logs directory. This is the file used by nginx to specify the process. Deleting this file can also solve the problem.
[root@test1 ~]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor > Active: failed (Result: exit-code) since Mon 2023-11-06 10:46:33 CST; 1> Docs: http://nginx.org/en/docs/ Process: 8225 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx> November 06 10:46:31 test1 systemd[1]: Starting nginx - high performance web s> November 06 10:46:31 test1 nginx[8225]: nginx: [emerg] bind() to 0.0.0.0:80 fa> November 06 10:46:31 test1 nginx[8225]: nginx: [emerg] bind() to 0.0.0.0:80 fa> November 06 10:46:32 test1 nginx[8225]: nginx: [emerg] bind() to 0.0.0.0:80 fa> November 06 10:46:32 test1 nginx[8225]: nginx: [emerg] bind() to 0.0.0.0:80 fa> November 06 10:46:33 test1 nginx[8225]: nginx: [emerg] bind() to 0.0.0.0:80 fa> November 06 10:46:33 test1 nginx[8225]: nginx: [emerg] still could not bind() November 06 10:46:33 test1 systemd[1]: nginx.service: Control process exited, > November 06 10:46:33 test1 systemd[1]: nginx.service: Failed with result 'exit> November 06 10:46:33 test1 systemd[1]: Failed to start nginx - high performanc> lines 1-16/16 (END) [root@test1 ~]# cd /apps/nginx/logs [root@test1 logs]# ls access.log error.log nginx.pid [root@test1 logs]# cat nginx.pid 8183 [root@test1 logs]# kill 8183 [root@test1 ~]# systemctl daemon-reload [root@test1 ~]# systemctl enable --now nginx [root@test1 ~]# cat nginx.pid 8183 [root@test1 logs]# kill 8183 [root@test1 ~]# systemctl daemon-reload [root@test1 ~]# systemctl enable --now nginx