Nginx源码编译安装

官方源码下载地址

安装步骤

# 安装相关依赖包
[root@centos8 ~]#dnf -y install gcc make pcre-devel openssl-devel zlib-devel

# 创建nginx用户
[root@centos8 ~]#useradd -r -s /sbin/nologin nginx

# 解压
[root@centos8 ~]#tar xvf nginx-1.18.0.tar.gz

# 切换目录
[root@centos8 ~]#cd nginx-1.18.0/

# 编译安装
[root@centos8 nginx-1.18.0]#./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

[root@centos8 nginx-1.18.0]#make -j 2 && make install

# 查看安装目录
[root@centos8 nginx-1.18.0]#ll /apps/nginx/
total 0
drwxr-xr-x 2 root root 333 Nov 23 17:22 conf
drwxr-xr-x 2 root root  40 Nov 23 17:22 html
drwxr-xr-x 2 root root   6 Nov 23 17:22 logs
drwxr-xr-x 2 root root  19 Nov 23 17:22 sbin

# 修改权限
[root@centos8 nginx-1.18.0]#chown -R nginx.nginx /apps/nginx/

# 创建软连接
[root@centos8 nginx-1.18.0]#ln -s /apps/nginx/sbin/nginx /usr/sbin/

# 查看版本
[root@centos8 nginx-1.18.0]#nginx -v
nginx version: nginx/1.18.0

# 查看版本详细信息及编译参数
[root@centos8 nginx-1.18.0]#nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --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

# 启动nginx
[root@centos8 nginx-1.18.0]#nginx
[root@centos8 nginx-1.18.0]#ss -nlt
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
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                      [::]:22                     [::]:*        
         
# 访问nginx
[root@centos8 nginx-1.18.0]#curl 10.0.0.83
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
         35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

# 停止nginx
[root@centos8 nginx-1.18.0]#nginx -s stop
[root@centos8 nginx-1.18.0]#ss -nlt
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*        
LISTEN     0          128                      [::]:22                     [::]:*        

# 创建nginx的service文件,使用systemctl控制nginx服务
# 可以参考yum安装后生成的service文件进行修改
[root@centos8 nginx-1.18.0]#cat > /lib/systemd/system/nginx.service <<EOF
[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
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
EOF

# 创建pid目录
[root@centos8 nginx-1.18.0]#mkdir /apps/nginx/run/

# 修改配置文件,指定pid文件
[root@centos8 nginx-1.18.0]#vim /apps/nginx/conf/nginx.conf
pid  /apps/nginx/run/nginx.pid

# 重新加载配置
[root@centos8 nginx-1.18.0]#systemctl daemon-reload

# 使用systemctl启动nginx
[root@centos8 nginx-1.18.0]#systemctl enable --now nginx

# 查看nginx服务状态
[root@centos8 nginx-1.18.0]#systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabl>
   Active: active (running) since Mon 2020-11-23 17:46:35 CST; 19s ago
     Docs: http://nginx.org/en/docs/
  Process: 8784 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=ex>
 Main PID: 8785 (nginx)
    Tasks: 2 (limit: 11314)
   Memory: 2.1M
   CGroup: /system.slice/nginx.service
           ├─8785 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx>
           └─8786 nginx: worker process

Nov 23 17:46:35 centos8.wuvikr.3 systemd[1]: Starting nginx - high performance web serve>
Nov 23 17:46:35 centos8.wuvikr.3 systemd[1]: Started nginx - high performance web server.

# 查看端口
[root@centos8 nginx-1.18.0]#ss -nlt
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
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                      [::]:22                     [::]:*        

# 使用systemctl停止nginx
[root@centos8 nginx-1.18.0]#systemctl stop nginx
[root@centos8 nginx-1.18.0]#systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabl>
   Active: failed (Result: exit-code) since Mon 2020-11-23 18:04:27 CST; 11s ago
     Docs: http://nginx.org/en/docs/
  Process: 8878 ExecStop=/bin/kill -s TERM (code=exited, status=1/FAILURE)
  Process: 8784 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=ex>
 Main PID: 8785 (code=exited, status=0/SUCCESS)

Nov 23 17:46:35 centos8.wuvikr.3 systemd[1]: Starting nginx - high performance web serve>
Nov 23 17:46:35 centos8.wuvikr.3 systemd[1]: Started nginx - high performance web server.
Nov 23 18:04:27 centos8.wuvikr.3 systemd[1]: Stopping nginx - high performance web serve>
Nov 23 18:04:27 centos8.wuvikr.3 kill[8878]: kill: not enough arguments
Nov 23 18:04:27 centos8.wuvikr.3 systemd[1]: nginx.service: Control process exited, code>
Nov 23 18:04:27 centos8.wuvikr.3 systemd[1]: nginx.service: Failed with result 'exit-cod>
Nov 23 18:04:27 centos8.wuvikr.3 systemd[1]: Stopped nginx - high performance web server.
原文地址:https://www.cnblogs.com/wuvikr/p/14026002.html