第五章 安装最新版 Nginx
本章编译安装 Nginx 服务器的 1.11.5 版本。
0. 初始化目录
sudo -u webhost mkdir -p $NGINX_TEMP/client-body
sudo -u webhost mkdir -p $NGINX_TEMP/proxy
sudo -u webhost mkdir -p $NGINX_TEMP/fastcgi
sudo -u webhost mkdir -p $NGINX_TEMP/scgi
sudo -u webhost mkdir -p $NGINX_TEMP/uwsgi
chmod -R 0775 $NGINX_TEMP
1. 下载源码和依赖包
本处使用 openssl-1.1.0c
,nginx-1.11.5
,zlib-1.2.8
,pcre-8.39
。
请自行下载对应的 .tgz
文件,并放到 /mnt/temp
目录下。
2. 编译 ZLib
cd /mnt/temp
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make -j 4
make install
3. 编译 PCRE
cd /mnt/temp
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure --enable-utf8 --enable-jit --enable-unicode-properties
make -j 4
make install
3.5. 使用 Google Performance Tools 提升性能
从 http://download.savannah.gnu.org/releases/libunwind/ 下载最新版 libunwind (此处为 1.1)
cd /mnt/temp
tar -zxvf libunwind-1.1.tar.gz
cd libunwind-1.1
./configure
make CFLAGS=-fPIC -j4
make CFLAGS=-fPIC install
从 https://github.com/gperftools/gperftools/releases 下载最新版 gperftools (此处为 2.5)
cd /mnt/temp
tar -zxvf gperftools-2.5.tar.gz
cd gperftools-2.5
./configure
make -j4
make install
4. 编译 Nginx
此处使用 OpenSSL v1.1.x 版本,以便开启 HTTP 2.0。 但是不对它进行编译和安装,所以对系统的 openssl 没有影响。
cd /mnt/temp
tar -zxvf openssl-1.1.0c.tar.gz
tar -zxvf nginx-1.11.5.tar.gz
cd nginx-1.11.5
./configure \
--conf-path=$NGINX_CONFIG/nginx.conf \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_random_index_module \
--with-google_perftools_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-pcre=/mnt/temp/pcre-8.39 \
--with-openssl=/mnt/temp/openssl-1.1.0c \
--with-zlib=/mnt/temp/zlib-1.2.8 \
--pid-path=$MCOS_PIDS/nginx.pid \
--lock-path=$MCOS_LOCKS/nginx.lock \
--user=webhost \
--group=staff \
--http-log-path=$MCOS_LOG/access.log \
--error-log-path=$MCOS_LOG/error.log \
--http-client-body-temp-path=$NGINX_TEMP/client-body \
--http-proxy-temp-path=$NGINX_TEMP/proxy \
--http-fastcgi-temp-path=$NGINX_TEMP/fastcgi \
--http-scgi-temp-path=$NGINX_TEMP/scgi \
--http-uwsgi-temp-path=$NGINX_TEMP/uwsgi
make -j 4
make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin
5. 配置 Nginx 为 Systemd 服务
创建文件 /usr/lib/systemd/system/nginx.service,内容如下:
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/www/pids/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /www/config/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /www/config/nginx/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存,即可使用下面的命令控制 Nginx:
systemctl start nginx
systemctl reload nginx
systemctl restart nginx
systemctl stop nginx
如果提示 nginx.service 不存在,先执行下面的清除 Systemd 缓存:
systemctl daemon-reload
comments powered by Disqus