Nginx编译安装

安装编译环境

这里以Centos为例

1
yum -y install gcc gcc-c++ automake autoconf libtool make

安装相关依赖库

pcre库为了rewrite,zlib库为了gzip压缩,将下面包全部解压缩

1
2
3
4
5
cd /opt
wget http://nginx.org/download/nginx-1.10.2.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz

编译安装

1
2
3
4
./configure --prefix=/opt/nginx --user=www --group=www --with-openssl=../openssl-1.0.2l \
--with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.11 --with-http_realip_module \
--with-http_ssl_module --with-http_stub_status_module
make && make install
  • 此处编译将openssl、pcre、zlib编译至nginx启动内核,从而不依赖系统openssl、pcre、zlib,加快nginx启动速度和方便移植到其他系统使用。

查看安装版本

1
2
3
4
5
6
7
8
# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.10.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2l 25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --user=www --group=www
--with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.11
--with-http_realip_module --with-http_ssl_module --with-http_stub_status_module

NGINX systemed service file

路径:/lib/systemd/system/nginx.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动NGINX

1
systemctl daemon-reload && systemctl enable --now nginx.service

  • nginx.conf配置内的pid指定位置必须和nginx.service文件内的路径保持一致。
-------------本文结束感谢您的阅读-------------