1.安装nginx所需依赖
#确定下载路径,下载路径和安装路径不一样
cd /usr/local/software
#下载nginx,最新版的去官网找
wget http://nginx.org/download/nginx-1.20.2.tar.gz
#安装依赖
yum install -y gcc-c++ zlib zlib-devel pcre-devel openssl openssl-devel
#解压到local下面
tar -xvf nginx-1.20.2.tar.gz -C /usr/local
2.编译安装
#考虑到后续安装ssl证书 添加两个模块.没有指定路径,默认安装在/usr/local/nginx/
./configure --with-http_stub_status_module --with-http_ssl_module
#执行完configure会多出objs和Makefile
#编译
make
#安装
make install
3.常用命令
cd /usr/local/nginx/sbin/
#启动
./nginx
#强制停止Nginx服务
./nginx -s stop
#优雅地停止Nginx服务(即处理完所有请求后再停止服务)
./nginx -s quit
#重新加载Nginx配置文件,然后以优雅的方式重启Nginx
./nginx -s reload
#检测配置文件是否有语法错误,然后退出
./nginx -t
#显示版本信息并退出
./nginx -v
#显示版本和配置选项信息,然后退出
./nginx -V
4.校验安装成功
服务器内: curl http://127.0.0.1
浏览器打开http://服务器ip地址
默认端口是80,所以不用加端口。如果nginx正常运行,但是浏览器访问不到,可能是防火墙没有开放80端口
5.修改默认端口
# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
因为80端口有可能被其他程序占用
6.设置开机启动
cd /lib/systemd/system/
#创建启动脚本
vim nginx.service
复制以下内容到脚本
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#这些不需要粘贴过去,是脚本的说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
加入开机启动
systemctl enable nginx
常用命令
# systemctl start nginx.service 启动nginx服务
# systemctl stop nginx.service 停止服务
# systemctl restart nginx.service 重新启动服务
# systemctl list-units --type=service 查看所有已启动的服务
# systemctl status nginx.service 查看服务当前状态
# systemctl enable nginx.service 设置开机自启动
# systemctl disable nginx.service 停止开机自启动