1. yum方式安装

1.1 安装依赖

yum install yum-utils

1.2 添加官方仓库

cat > /etc/yum.repos.d/nginx.repo << "EOF"
[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

[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
EOF

1.3 正式安装

yum install nginx

备注:如果想安装mainline版本,可执行yum-config-manager --enable nginx-mainline启用mainline repo。

2. 源码方式安装

2.1 安装依赖

yum -y install gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel gd gd-devel

2.2 正式安装

cd /usr/local/src/
wget https://nginx.org/download/nginx-1.14.0.tar.gz

# 添加运行nginx的用户
groupadd www
useradd -s /sbin/nologin -g www www

# 解压
tar zxf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
# 编译安装,编译参数根据个人需要进行配置
./configure --user=www --group=www \
--prefix=/usr/local/nginx \
--with-pcre \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module
make && make install

2.3 使用systemd管理nginx

cat > /usr/lib/systemd/system/nginx.service << "EOF"
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target 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
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
#PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

备注:使用yum方式安装的就无需手动创建了。

3. 服务管理

# 添加随机启动
systemctl enable nginx.service
# 启动服务
systemctl start nginx.service

4. 更多实用参考

nginx服务器安装及配置文件详解

nginx做负载均衡器以及proxy缓存配置

最后修改日期: 2021年5月28日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。