源码编译安装nginx


测试环境基于

[root@localhost ~]$cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

安装前准备

  • 创建nginx系统用户,在nginx.conf文件中通过user指定以nginx的身份运行worker进程
[root@localhost /data/nginx]$useradd -r nginx
[root@localhost /data/nginx]$tail -1 /etc/passwd
nginx:x:991:985::/home/nginx:/bin/bash
[root@localhost /data/nginx]$
  • 安装环境
[root@localhost ~]$yum groupinstall "Development Tools" -y
[root@localhost ~]$yum install pcre-devel zlib-devel openssl-devel libxslt-devel GeoIP-devel perl-ExtUtils-Embed expat-devel -y
#安装哪些依赖包取决于后面要安装哪些模块,不同的模块依赖不同的包,如果这里没有安装依赖包,后面./configure时会报错
  • 检查环境是否已安装成功
[root@localhost ~]$yum grouplist installed #查看已安装的包组
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.lzu.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
Installed Groups:
   Development Tools
Done
[root@localhost ~]$
[root@localhost ~]$yum list installed | grep -Ei "(pcre-devel)|(zlib-devel)|(openssl-devel)|(libxslt-devel)|(GeoIP-devel)|(perl-ExtUtils-Embed)|(expat-devel)"
GeoIP-devel.x86_64 1.5.0-13.el7 @base    
expat-devel.x86_64 2.1.0-10.el7_3 @base    
libxslt-devel.x86_64 1.1.28-5.el7 @base    
openssl-devel.x86_64 1:1.0.2k-16.el7_6.1 @updates 
pcre-devel.x86_64 8.32-17.el7 @base    
perl-ExtUtils-Embed.noarch 1.30-294.el7_6 @updates 
zlib-devel.x86_64 1.2.7-18.el7 @base    
[root@localhost ~]$

下载源码包

  • 官网下载稳定版本源码包: http://nginx.org/en/download.html
    • 解压包
[root@localhost /data/nginx]$tar -xf nginx-1.16.0.tar.gz 
[root@localhost /data/nginx]$ls
nginx-1.16.0 nginx-1.16.0.tar.gz
[root@localhost /data/nginx]$

编译安装

./configure --prefix=/data/app/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --user=nginx --group=nginx --with-file-aio --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_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-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module

  • 以上设定了安装目录、配置文件目录、log日志目录、nginx.pid目录、nginx.lock目录、指定用户和用户组和一些可能用到的模块。可通过./configure --help来查看可选参数。参数可参考yum安装的nginx -V的configure参数。
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/data/app/nginx"
  nginx binary file: "/data/app/nginx/sbin/nginx"
  nginx modules path: "/data/app/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/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"
[root@localhost /data/src/nginx/nginx-1.16.0]$
  • 编译安装
[root@localhost /data/nginx/nginx-1.16.0]$make -j 2 && make install
  • 安装完成后,可以通过nginx -V查看当前nginx编译安装时的configure参数

配置nginx语法着色

[root@localhost /data/nginx/nginx-1.16.0]$cp -r contrib/vim/* /usr/share/vim/vimfiles/
以上操作使打开nginx.conf等nginx配置文件时能语法着色

配置systemd 设置开机自启

  • 参考yum安装后的nginx.service配置systemd
[root@centos7 nginx]#cat nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

[root@localhost /data/src/nginx]$cp nginx.service /usr/lib/systemd/system
把nginx.service拷贝到/usr/lib/systemd/system/目录下

[root@localhost ~]$systemctl daemon-reload #重新加载下systemd配置文件
  • 配置nginx.service后可以通过systemctl命令管理nginx
[root@localhost /data/src/nginx]$systemctl start nginx.service 
[root@localhost /data/src/nginx]$systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost /data/src/nginx]$systemctl status nginx.service 
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-07-12 20:18:20 CST; 15s ago
     Docs: http://nginx.org/en/docs/
 Main PID: 31213 (nginx)
   CGroup: /system.slice/nginx.service
           ├─31213 nginx: master process /data/app/nginx/sbin/nginx -c /etc/nginx/nginx.conf
           └─31214 nginx: worker process

Jul 12 20:18:20 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Jul 12 20:18:20 localhost.localdomain systemd[1]: PID file /run/nginx.pid not readable (yet?) after start.
Jul 12 20:18:20 localhost.localdomain systemd[1]: Started nginx - high performance web server.
[root@localhost /data/src/nginx]$

导出nginx环境变量

[root@localhost ~]$cat /etc/profile.d/nginx.sh 
#
# 导出环境变量,使其能找到nginx命令
#

NGINX_DIR="/data/app/nginx" #nginx安装目录
PATH="$PATH:$NGINX_DIR/sbin"
export PATH
[root@localhost ~]$
[root@localhost ~]$source /etc/profile.d/nginx.sh
[root@localhost ~]$$echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data/app/nginx/sbin
  • 可通过-s 给nginx命令传信息,如: nginx -s start; nginx -s stop; nginx -s quit; nginx -s reload等
-s signal Send a signal to the master process. The argument signal can be one
                    of: stop, quit, reopen, reload. The following table shows the cor‐
                    responding system signals:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值