安装参考:https://blog.csdn.net/qq_33454884/article/details/89212702
【下载】
下载地址:http://nginx.org/en/download.html
【安装相关依赖与工具包】openssl、zlib、pcre
在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel
依赖包安装顺序依次为:openssl、zlib、pcre, 最后安装Nginx包
# 安装方式1:yum命令方式安装依赖
# yum install -y pcre pcre-devel
# yum -y install gcc gcc-c++ pcre pcre-devel make cmake zlib zlib-devel libtool openssl openssl-devel
# 安装方式2:压缩包解压安装方式
#1.安装pcre
# rpm -qa pcre -- 安装前检查是否安装过了,但是该方式只能检查到yum方式安装的软件
下载pcre:https://ftp.pcre.org/pub/pcre/
https://sourceforge.net/projects/pcre/files/pcre/
或者:wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz/download
# tar -zxvf pcre-8.39.tar.gz
# cd pcre-8.39
# ./configure
# make && make install
# pcre-config --version -- 检查安装结果
然后:安装openssl、安装zlib
【安装Nginx】
# 解压:tar -zxvf nginx-version.tar.gz
# tar -zxvf nginx-1.12.2.tar.gz
# 进入nginx-version目录
# cd nginx-1.12.2
# ./configure
# make && make install
注:如有必要,可以带上参数安装
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2
注意:"--with-xxx="的值是解压目录,而不是安装目录!
# 编译安装过程中创建如下目录和脚本位置
mkdir -p '/usr/local/nginx/conf'
mkdir -p '/usr/local/nginx/sbin'
mkdir -p '/usr/local/nginx/logs
# 启动脚本
/usr/local/nginx/sbin/nginx
# 进程文件
/usr/local/nginx/logs/nginx.pid
# 错误日志
/usr/local/nginx/logs/error.log
【启动nginx】
# /usr/local/nginx/sbin/nginx
# ps -ef|grep nginx
root 13732 1 0 12:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13733 13732 0 12:18 ? 00:00:00 nginx: worker process
root 13735 26107 0 12:18 pts/1 00:00:00 grep --color=auto nginx
# 启动成功nginx后有两个进程:master process 和 worker process
【配置nginx】
# cd /usr/local/nginx/conf/
# vim nginx.conf
# 根据需要配置自己的Nginx
【访问nginx】
# 默认端口是:80
# 在浏览器地址栏输入服务器Ip,页面显示:Welcome to nginx!,安装成功!
【防火墙端口开放信任设置】
# 查看防火墙firewall服务状态
# systemctl status firewalld
# 启动防火墙
# systemctl start firewalld
# -- 查看默认防火墙状态
# firewall-cmd --state
# -- 停止防火墙服务
# systemctl stop firewalld.service
# 添加80端口
# firewall-cmd --zone=public --add-port=80/tcp --permanent
# 移除端口
# firewall-cmd --zone=public --remove-port=80/tcp --permanent
# 重新载入防火墙设置
# firewall-cmd --reload
# firewall-cmd --list-port
# firewall-cmd --list-all -- 查看已经开放的端口号
# 重启防火墙服务
# systemctl restart firewalld.service
# 禁止firewall开机启动
# systemctl disable firewalld.service
防火墙相关操作参考:https://www.cnblogs.com/leoxuan/p/8275343.html
【nginx基本操作命令】
1.启动服务:nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
2.强制关闭服务:nginx -s stop
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
3.正常停止服务:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s quit
4.重启(重载)服务:nginx -s reload (重载服务配置文件,类似于重启,但服务不会中止)
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
5.使用命令帮助:nginx -h
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
6.验证配置文件:nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
7.使用配置文件启动服务:nginx -c "配置文件路径"
[root@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf