Nginx安装

431 阅读1分钟

yum安装

yum install yum-utils

设置nginx存储库

vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=tru

源码安装

下载官方源码包
# 官方地址:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.16.1.tar.gz -P /usr/local/src
tar -zxf /usr/local/src -C /tmp/ && cd /tmp/nginx-1.16.1
安装编译工具
yum install gcc pcre-devel zlib-devel 
编辑安装脚本
vim install.sh
#!/bin/bash
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/run/nginx.pid \

sh install.sh
make && make install

# 创建启动用户,-r是创建系统用户
groupadd -g 80 -r nginx
useradd -M -u 80 -g 80 -r nginx -s /sbin/nologin -c "Nginx Server" nginx

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
编辑自启动脚本
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/nginx/sbin/nginx -s stop
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl enable nginx.service
systemctl start nginx.service
动态安装模块
# 查看已安装的模块
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/share/nginx --sbin-path=/usr/sbin/nginx

cd /tmp/nginx-1.16.1
echo '--with-stream_geoip_module \' >> install.sh
sh install.sh

make
(切记:加载未安装的模块,一定不能make install否则会把之前的配置都搞没!)

# 将加载完的nginx命令覆盖原命令
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/nginx

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/share/nginx --sbin-path=/usr/sbin/nginx --with-stream_geoip_module