Nginx编译安装

1.准备编译安装的基础环境

shell> yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

2.安装Nginx

# 从官方下载源码包
shell> cd /usr/local/src/
shell> wget https://nginx.org/download/nginx-1.12.2.tar.gz
shell> tar xvf nginx-1.12.2.tar.gz
shell> cd nginx-1.12.2/
shell> groupadd nginx
shell> useradd -M -r -g nginx -s /sbin/nologin nginx
shell> ./configure --prefix=/data/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
shell> make & make install

# 验证版本及编译参数
shell> /data/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/data/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

# 启动
shell> /data/nginx/sbin/nginx

3.创建Nginx自启动脚本

# 创建 /usr/lib/systemd/system/nginx.service 文件
shell> vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
# 修改成编译安装目录
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
#KillSignal=SIGQUIT
#TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

4.验证Nginx自启动脚本

shell> /data/nginx/sbin/nginx -s stop
shell> systemctl daemon-reload
shell> systemctl start nginx
shell> systemctl enable nginx
shell> systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-08-12 11:10:30 CST; 22s ago
 Main PID: 11081 (nginx)
   CGroup: /system.slice/nginx.service
           ├─11081 nginx: master process /data/nginx/sbin/nginx
           └─11083 nginx: worker process

Aug 12 11:10:30 centos7 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 12 11:10:30 centos7 nginx[11077]: nginx: the configuration file /data/nginx/conf/nginx.c...s ok
Aug 12 11:10:30 centos7 nginx[11077]: nginx: configuration file /data/nginx/conf/nginx.conf ...sful
Aug 12 11:10:30 centos7 systemd[1]: Failed to read PID from file /data/nginx/logs/nginx.pid:...ment
Aug 12 11:10:30 centos7 systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
最后修改:2024 年 01 月 04 日
如果觉得我的文章对你有用,请随意赞赏