文章目录
1.nginx下载
1.1 文件下载
1.2命令行下载
我习惯将软件安装在/tools/xxx对应目录下,因此这次安装为/tools/nginx/
#我习惯将源码安装包放到这里面,并且安装到/tools/nginx里面
cd /tools/installbags/
wget http://nginx.org/download/nginx-1.22.0.tar.gz
2.nginx安装前置步骤
2.1安装依赖库
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.1安装PCRE库
#可以选择自己安装包放置的路径
cd /tools/installbags/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
#解压压缩包
tar zxvf pcre-8.35.tar.gz
#进入解压目录
cd pcre-8.35/
./configure
#安装
make && make install
#查看pcre版本
pcre-config --version
3.nginx安装
cd tools
#创建nginx安装目录
mkdir nginx
cd /tools/installbags/
#解压
tar zxvf nginx-1.22.0.tar.gz
#进入解压文件夹
cd nginx-1.22.0
#简易配置(2选1)
./configure --prefix=/tools/nginx
#指定配置(2选1)
./configure --prefix=/tools/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/tools/installbags/pcre-8.35
make && make install
4.nginx命令以及开机自启
4.1nginx常用命令
注意,我安装nginx的目录为/tools/nginx/
# 启动。默认监听80端口,启动成功之后可以浏览器访问ip:80,出现4.2中图表示centos服务器上nginx启动成功
/tools/nginx/sbin/nginx
# 重新载入配置文件
/tools/nginx/sbin/nginx -s reload
# 重启 Nginx
/tools/nginx/sbin/nginx -s reopen
# 强制停止 Nginx
/tools/nginx/sbin/nginx -s stop
# 优雅停止 Nginx,在退出前完成已经接受的连接请求
/tools/nginx/sbin/nginx -s quit
# 检查nginx.conf文件正确性
/tools/nginx/sbin/nginx -t
4.1.1nginx启动成功
4.1.2 nginx.conf检查
[root@VM-12-16-centos ~]# /tools/nginx/sbin/nginx -t
nginx: the configuration file /tools/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /tools/nginx/conf/nginx.conf test is successful
5.nginx设置自启
5.1创建nginx相关服务脚本
# 进入系统服务脚本目录
cd /usr/lib/systemd/system
# 创建ngxin服务相关脚本
touch nginx.service
5.2 内容如下
(注意我的目录是tools/nginx,根据需要进行更改)
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/tools/nginx/logs/nginx.pid
ExecStartPre=/tools/nginx/sbin/nginx -t -c /tools/nginx/conf/nginx.conf
ExecStart=/tools/nginx/sbin/nginx -c /tools/nginx/conf/nginx.conf
ExecReload=/tools/nginx/sbin/nginx -s reload
ExecStop=/tools/nginx/sbin/nginx -s stop
ExecQuit=/tools/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
5.3 重新加载系统服务
systemctl daemon-reload
5.4 启动服务
systemctl start nginx.service
5.5 开机启动
systemctl enable nginx.service
5.6 查看系统服务状态
systemctl status nginx.service