Nginx编译安装

  • Post author:
  • Post category:Nginx
  • Page Views 494 阅读

自动安装脚本:>>>Nginx自动编译安装脚本<<<

1.安装编译环境

yum install -y gcc gcc-c++

2.安装依赖库

pcre:nginx 的 rewrite 模块需要有pcre库的支持。
openssl:ssl功能需要有 openssl库的支持。
zlib:gzip模块需要有zlib库的支持。

yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
yum install -y libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed geoip-devel #根据编译报错得出

3.下载源码包

http://nginx.org/en/download.html

[root@LNMP ~]# cd /usr/local/src/
[root@LNMP src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

4.编译安装

[root@LNMP src]# tar xf nginx-1.16.1.tar.gz
[root@LNMP src]# cd nginx-1.16.1
[root@LNMP src]# useradd nginx -s /sbin/nologin -M
[root@LNMP nginx-1.16.1]#
./configure \
--prefix=/usr/local/nginx-1.16.1 \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module
[root@LNMP nginx-1.16.1]# make && make install

5.创建软连接

创建软链接的意义:当nginx需要升级时,nginx新版本编译完成后,删除旧的软链接,重新建立软链接到/usr/local/nginx

[root@LNMP nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1/ /usr/local/nginx

6.设置环境变量

[root@LNMP nginx-1.16.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile
[root@LNMP nginx-1.16.1]# source /etc/profile
[root@LNMP nginx-1.16.1]# echo $PATH
/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

7.检查与启动服务

[root@LNMP ~]# nginx -t
nginx: the configuration file /usr/local/nginx-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.16.1/conf/nginx.conf test is successful
[root@LNMP ~]# nginx

8.配置system服务

cat >/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

9.以服务的方式启动、

可能会遇到如下报错:

[root@LNMP ~]# systemctl stop nginx
[root@LNMP ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

解决办法:

[root@LNMP ~]# pkill nginx
[root@LNMP ~]# systemctl start nginx
[root@LNMP ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.


「 文章如果对你有帮助,请点个赞哦^^ 」 

0

这篇文章有一条评论

评论关闭。