一、下载Nginx安装包
1.服务器联网的情况下,使用wget命令把Nginx安装包下载到/usr/local/目录中,如果没有wget命令,需要先安装:yum install -y wget
cd /usr/local
wget -c https://nginx.org/download/nginx-1.25.4.tar.gz
nginx官网:https://nginx.org/en/download.html
二、安装Nginx
1、安装Nginx相关依赖
#在线安装nginx所需要的依赖包
yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
2、配置Nginx安装项
找到Nginx的安装包进行解压。
2.1 解压安装包
#解压安装包
cd /usr/local/
tar -zxvf nginx-1.25.4.tar.gz
2.2 进入nginx目录
#进入nginx目录
cd /usr/local/nginx-1.25.4
2.3 执行配置脚本
#执行配置脚本 --prefix是指定安装目录
./configure --prefix=/usr/local/nginx --with-http_ssl_module
如果出现错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
# yum 安装prce library
yum -y install pcre
#安装openssl openssl-devel
yum -y install openssl openssl-devel
#安装gd-devel
yum -y install gd-devel
如果出行类似上面这样的输出,说明依赖项和配置项都正确了。
3、编译并安装Nginx
#对nginx编译和安装
cd /usr/local/nginx-1.25.4
make & make install
三、启动Nginx
1.启动脚本是在 /usr/local/nginx/sbin/nginx
#启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#停止
/usr/local/nginx/sbin/nginx -s stop
#重载
/usr/local/nginx/sbin/nginx -s reload
#杀掉nginx
/usr/local/nginx/sbin/nginx -s quit
#查询nginx是否启动
ps -ef | grep nginx
启动Nginx后,在浏览器中访问,如果出现这样的页面,说明Nginx安装成功。
四、设置Nginx开机自动启动
1.创建nginx.service文件
在/etc/systemd/system/路径下创建nginx.service文件。
2.编写nginx.service内容
在/etc/systemd/system/nginx.service文件中填写内容:
[Unit]
Description=nginx - high performance web server
After=nginx.service
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Execenable=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target
保存并退出。
3.设置开机自启动
# 设置开机启动
systemctl enable nginx
# 取消开机自启动
#systemctl disable nginx
# 查看服务当前状态
systemctl status nginx
# 启动nginx服务
systemctl start nginx
# 停止nginx服务
systemctl stop nginx
# 重启nginx服务
systemctl restart nginx
参考资料:
Linux系统下安装配置 Nginx 超详细图文教程_linux安装nginx-CSDN博客
3分钟教你搞定 nginx 编译安装报错:error: the HTTP rewrite module requires the PCRE library._./configure: error: the http rewrite module requir-CSDN博客
https://www.cnblogs.com/shineman-zhang/articles/17392268.html