通过编译源码包的形式安装,需要root权限,使用root用户进行命令操作
1、下载Nginx
官网:nginx,下载文件
2、上传Nginx文件到服务器
3、解压文件
进入/opt文件夹下
tar -zxvf nginx-1.24.0.tar.gz
查看解压后的文件
ls -la
4、安装编译环境和依赖
安装 gcc-c++ 编译器
yum -y install gcc-c++
yum install -y openssl openssl-devel
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
5、安装Nginx
配置安装目录,将安装到/opt/nginx 这个目录下
./configure --prefix=/opt/nginx
执行 make 和 make install 命令进行编译安装
make
make install
查看opt目录下多了nginx文件夹
6、开放端口 80
检查端口是否开放
firewall-cmd --zone=public --query-port=80/tcp
开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重新加载
firewall-cmd --reload
再检查端口是否开放
firewall-cmd --zone=public --query-port=80/tcp
7、启动 Nginx
查看 查看nginx 相关的进程
ps -ef | grep nginx
8、验证 Nginx
在其他终端,打开浏览器,输入ip,回车
9、设置 Nginx 服务开机启动
需要 root 权限,使用 root 用户进行命令操作
原理:利用 systemctl 管理服务
新建服务
在/usr/lib/systemd/system 目录下,新建 nginx.service 文件,配置内容
vim /usr/lib/systemd/system/nginx.service
配置内容如下:
[Unit]
Description=Nginx Server
After=nginx.service
[Service]
User=root
Group=root
Type=forking
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecStop=/opt/nginx/sbin/nginx -s quit
ExecReload=/opt/nginx/sbin/nginx -s reload
Restart=on-failure
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载systemctl
systemctl daemon-reload
启动Nginx
systemctl start nginx.service;ps -ef | grep nginx
设置 Nginx 开机启动
systemctl is-enabled nginx
重启计算机
reboot
验证Nginx 开机启动
systemctl status nginx.service;ps -ef | grep nginx