Linux 下编译安装 Nginx

文章
林里克斯

Linux 下编译安装 Nginx,适用生成环境。以及已有模块不满足使用环境再添加新模块

实验平台:CentOS 7.6

Nginx版本:nginx/1.16.1


一、安装环境


1.Nginx 官网

http://nginx.org/download/

2.创建 Nginx 用户

$ groupadd --system nginx
$ useradd --system -g nginx -d /usr/local/nginx/ -s /sbin/nologin -c "Nginx User" nginx

3.下载编译包

$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
$ tar xf nginx-1.16.1.tar.gz

二、编译安装


1.编译

$ ./configure --prefix=/data/nginx --sbin-path=/data/nginx/sbin/nginx --conf-path=/data/nginx/conf/nginx.conf --error-log-path=/data/nginx/logs/error.log --http-log-path=/data/nginx/logs/access.log --pid-path=/data/nginx/run/nginx.pid --lock-path=/data/nginx/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
checking for OS
 + Linux 3.10.0-957.27.2.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

2.缺少 gcc 编译环境

$ yum -y install gcc-c++

3.缺少 pcre

$ ./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

$ yum -y install pcre-devel

4.缺少 ssl 模块依赖

$ ./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

$ yum  -y install openssl-devel

5.安装完成

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/var/run/nginx/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

6.编译安装

$ make && make install

7.让 Nginx 读取指定路径的 .conf 文件

$ vim /usr/loacl/nginx/nginx.conf

include /usr/local/nginx/conf/conf.d/*.conf;

三、启动文件


1.编写启动文件 service

$ vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
$ chown -R nginx. /usr/local/nginx/
$ systemctl daemon-reload
$ systemctl start nginx
$ cp -rp /usr/local/nginx/sbin/nginx /usr/bin/

四、再添加编译模块


1.安装完成后再添加模块

  • eg: 添加 --with-http_v2_module --with-mail=dynamic

2.停止 nginx

$ systemctl stop nginx

3.查看已有模块

$ nginx -V

nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre

4.添加模块编译

$ ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_v2_module --with-mail=dynamic
$ make
#切记不要执行 make install 这步会覆盖原模块

5.备份原有 nginx 命令

$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
#因为新编译好的命令在 /root/nginx-1.16.1/objs

6.完善命令路径

$ cp -rp /root/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/
$ cp -rp /usr/local/nginx/sbin/nginx /usr/bin/

7.查看现有模块

$ nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_v2_module --with-mail=dynamic

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

648 0 2019-08-31


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-03-29

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 208710 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!