centos安装nginx并配置自启动

140 阅读2分钟

nginx依赖包安装

yum install -y gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

nginx下载与安装

直接下载.tar.gz安装包,下载地址:nginx.org/en/download…把下载的包放到/tool/nginx目录,然后解压安装目录,make编译安装

mkdir -r /tool/nginx

cd /tool/nginx

wget 下载地址

tar -zxvf 压缩包名

//我下载的是1.20.0版本
cd /tool/nginx/nginx-1.20.0

./configure

make


make install

到此为止环境已经安装好,接下来启动nginx服务

cd /usr/local/nginx/sbin/

./nginx

//开放端口
firewall-cmd--add-port=80/tcp --permanent
firewall-cmd --add-port=80/udp --permanent

//重置防火墙
firewall-cmd --reload






image-20230721000235002转存失败,建议直接上传图片文件

设置自启动

cd /lib/systemd/system/

第二步:创建nginx.service文件,并编辑

vim nginx.service

//输入以下内容
[Unit]
Description=nginx
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

Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

:wq
//保存并退出

第三步:加入开机自启动

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         停止开机自启动