6 Star 72 Fork 28

JustryDeng / notebook

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
[01]CentOS7安装Nginx及相关指令.md 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
JustryDeng 提交于 2024-01-25 17:45 . CentOS7安装Nginx及相关指令

CentOS7安装Nginx及相关指令

CentOS7安装Nginx

第一步:下载软件包

cd /opt/
wget http://nginx.org/download/nginx-1.22.0.tar.gz

第二步:安装依赖

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

第三步:安装Nginx

#解压
tar -zxvf nginx-1.22.0.tar.gz

#进入NG目录
cd ./nginx-1.22.0

# 如果./configure时准备启用--with-http_image_filter_module模块的话,需要先安装gd
yum -y install gd gd-devel
#配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --with-http_image_filter_module

#编译
make
make install

第四步(可选):修改nginx配置文件

注:/usr/local/nginx/是nginx的默认安装位置,我们修改配置文件都是去安装位置修改,而不是去下载位置修改

cd /usr/local/nginx/conf
vim nginx.conf

第五步:启动&停止

#启动
/usr/local/nginx/sbin/nginx

#查看nginx状态
ps aux | grep nginx

#重新加载配置
/usr/local/nginx/sbin/nginx -s reload

#停止
/usr/local/nginx/sbin/nginx -s stop

更多命令

  • 以特定目录下的配置文件启动:/usr/local/nginx/sbin/nginx -c /特定目录/nginx.conf
  • 重新加载配置:/usr/local/nginx/sbin/nginx -s reload,执行这个命令后,master进程会等待worker进程处理完当前请求,然后根据最新配置重新创建新的worker进程,完成Nginx配置的热更新
  • 立即停止服务:/usr/local/nginx/sbin/nginx -s stop
  • 从容停止服务:/usr/local/nginx/sbin/nginx -s quit,执行该命令后,Nginx在完成当前工作任务后再停止。
  • 检查配置文件是否正确:/usr/local/nginx/sbin/nginx -t
  • 查看正生效着的配置文件内容:/usr/local/nginx/sbin/nginx -T
  • 检查特定目录的配置文件是否正确:/usr/local/nginx/sbin/nginx -t -c /特定目录/nginx.conf
  • 查看版本信息:/usr/local/nginx/sbin/nginx -v
  • 查看版本、已启用模块等详细信息:/usr/local/nginx/sbin/nginx -V

注:/usr/local/nginx/即为nginx的安装目录

以systemd的方式启动停止NG

配置systemd方式管理NG

vim /etc/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动停止NG

#(启动nginx服务)
systemctl start nginx.service
#(停止nginx服务)
systemctl stop nginx.service
#(查看服务当前状态)
systemctl status nginx.service
#(设置开机自启动)
systemctl enable nginx.service
#(停止开机自启动)
systemctl disable nginx.service
#(重新启动服务)
systemctl restart nginx.service
#(查看所有已启动的服务)
systemctl list-units --type=service

相关资料

1
https://gitee.com/JustryDeng/notebook.git
git@gitee.com:JustryDeng/notebook.git
JustryDeng
notebook
notebook
master

搜索帮助