美文网首页
编译安装nginx

编译安装nginx

作者: 猫尾草 | 来源:发表于2019-09-25 20:57 被阅读0次

1. 编译安装nginx

  因为官方版的nginx是不带lua等第三方模块的,这意味着使用yum install -y nginx安装的nginx是不带这些模块的。所以要使用这些模块时必须编译安装nginx。nginx的编译安装教程在网上很容易找。
第一步:安装依赖
yum install gc gcc gcc-c++ zlib-devel openssl-devel pcre-devel
第二步:下载nginx安装文件

// 1.16.0是nginx版本
wget http://nginx.org/download/nginx-1.16.0.tar.gz
// 解压
tar -zxvf nginx-1.16.0.tar.gz
// 进入nginx安装文件目录
cd nginx-1.16.0

第三步:安装nginx

// 这一步实际上我也不知道需要带哪些必要参数,安装哪些模块,后面会给出一个解决办法
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install

安装完成,nginx -v可以看到nginx版本信息,就安装成功了。

[root@work-env ~]# nginx -v
nginx version: nginx/1.16.0

2. 一个讨巧方法

  你可能本来电脑上就使用yum install -y nginx安装了nginx,没关系,这里也推荐你先用yum install -y nginx安装好nginx,再用相同版本编译安装一次。
第一步:
nginx -v查看版本信息,下载对应版本的nginx安装包
第二步:
nginx -V(注意这里是大写的V)查看yum安装的nginx的配置信息,一般如下:

--prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --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_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-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

这些信息就是yum帮你设置好的参数,包含了常用设置和常用模块。比如我们想添加lua模块,就在后面加上

--add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.11

其中/opt/ngx_devel_kit-0.3.0/opt/lua-nginx-module-0.10.11是你下载的lua模块的安装文件地址。
  这时使用./configure命令加上这段参数,就可以安装带lua模块的nginx。
  这样安装还有一个好处,yum安装过程中生成的systemct start nginx、systemctl status nginx等命令,在你本机重新编译安装后还存在。如果你自己重零开始编译安装,就需要编辑/usr/lib/systemd/system/nginx.service文件,向其中添加

[Unit]
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=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

才能使用systemctl命令操作nginx。

3. systemctl操作nginx得命令

systemctl is-enabled nginx.service #查询nginx是否开机启动
systemctl enable nginx.service #开机运行nginx
systemctl disable nginx.service #取消开机运行nginx
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重启nginx
systemctl reload nginx.service #重新加载nginx配置文件
systemctl status nginx.service #查询nginx运行状态
systemctl --failed #显示启动失败的服务

编译安装nginx的问题讲完了。

相关文章

网友评论

      本文标题:编译安装nginx

      本文链接:https://www.haomeiwen.com/subject/gnkmuctx.html