nginx安装

nginx安装

zlib1.3
openssl-1.1.1
pcre
pcre-devel

1. 前置安装

nginx安装依赖于g++、gcc、openssl-devel、pcre-devel和zlib-devel
实际环境存在gcc环境,仅安装openssl-devel、pcre-devel和zlib-devel
前置安装使用root用户操作

1.1 安装zlib

1
2
3
4
5
6
#下载zlib离线包
#解压
tar -xvf zlib-1.3.tar.gz
#编译
./configure
make && make install

1.2安装openssl-1.1.1

1
2
3
4
5
6
7
8
9
10
11
12
13
#下载openssl离线包(版本尽量为1.1.X)
#解压
tar -xvf openssl-1.1.1n.tar.gz
#编译
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make && make install

#配置环境变量
vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/openssl/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/openssl/bin:$PATH

source ~/.bashrc

1.3安装pcre依赖包

1
2
3
#安装pcre依赖包
rpm -ivh pcre-8.32-17.el7.x86_64.rpm --force
rpm -ivh pcre-devel-8.32-17.el7.x86_64.rpm

2.nginx安装

nginx使用一般用户安装

2.1 安装

1
2
3
4
5
6
7
8
9
10
11
12
#使用root用户创建文件夹,再切换至一般用户
mkdir /usr/local/nginx
chmod 777 /usr/local/nginx
su nginx
#使用nginx用户操作
#下载nginx安装包,并解压
tar -xvf nginx-1.24.0.tar.gz
#安装
./configure --prefix=/usr/local/nginx
make && make install

#/usr/local/nginx/conf目录下nginx.cof文件根据实际情况替换

2.2 操作命令

1
2
3
4
5
6
# 启动nginx
/usr/local/nginx/sbin/nginx
# 立即停止Nginx服务
/usr/local/nginx/sbin/nginx -s stop
# 完成当前任务后停止
/usr/local/nginx/sbin/nginx -s quit

2.3 Nginx配置开机启动

首先创建nginx.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vi 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

1
2
3
4
5
6
7
8
#将nginx.service移动到/usr/lib/systemd/system/目录下
cp nginx.service /usr/lib/systemd/system/
#重启配置服务
systemctl daemon-reload
#启动nginx服务
systemctl start nginx
#配置nginx开机自启
systemctl enable nginx

显示Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service配置成功

1
2
3
4
#重启服务
reboot
#验证
systemctl status nginx