1,前置依赖包
1)gcc安装
yum -y install gcc
2)pcre、pcre-devel安装
yum install -y pcre pcre-devel
3)zlib安装
yum install -y zlib zlib-devel
4)安装openssl
yum install -y openssl openssl-devel
2,Nginx安装
1)下载Nginx安装包
wget http://nginx.org/download/nginx-1.14.1.tar.gz
2)解压安装包
tar -xf nginx-1.14.1.tar.gz
3)进入解压之后的文件夹,编译(通过--prefix指定安装目录)
./configure --prefix=/root/proxy/nginx
4)安装
make &&make install
3,测试Nginx
1)进入/root/proxy/nginx/conf目录,配置nginx.conf的监听端口及转发
2)启动nginx
/root/proxy/nginx/sbin/nginx -c /root/proxy/nginx/conf/nginx/conf
3)启动完成之后,浏览器访问监听的ip及端口即可看到nginx的欢迎页面
4,将Nginx添加到系统服务实现开机自启动
1)在/usr/local/下新建nginx文件夹( 这个文件夹的名称可以自定义)
mkdir /usr/local/nginx
2)将自定义路径安装的nginx目录下的 conf 和 sbin 目录移动到 /usr/local/nginx
cp -r /root/proxy/conf /usr/local/nginx
cp -r /root/proxy/sbin /usr/local/nginx
3)新建/usr/lib/systemd/system/nginx.service文件
vi /usr/lib/systemd/system/nginx.service
4)文件内添加如下内容,然后保存。
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
ExecStartPost=/bin/sleep 0.1
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
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000
[Install]
WantedBy=multi-user.target
4)更新系统服务
systemctl daemon-reload # 更新
systemctl enable nginx # 添加为开机自启动
systemctl start nginx # 启动nginx服务
systemctl status nginx # 查看服务启动状态
5,上述为服务器内部署单个nginx的步骤,如果要部署多个nginx,只需要从第2步开始再做一遍即可,注意安装目录,/usr/local下新建的文件夹,/usr/lib/systemd/system/新建的service文件名和service文件内配置的命令路径不一样即可。