1. 停止原有的web服务器:端口默认均是80
2. 添加普通用户账号运行nginx
useradd -M -s /sbin/nologin nginx
3. 解压并安装nginx
tar xf nginx-1.8.1.tar.gz
cd ngxin-1.8.1/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --sbin-path=/usr/sbin/
// --prefix=/usr/local/nginx 指定安装路径
// --with-http_stub_status_module 启用service status页,默认不启用
// --with-http_ssl_module 启用ssl模块,以支持https请求
// --with 开启某模块
// --without 不开启某模块
make && make install
// 退出nginx-1.18.0目录并删除解压目录
cd ../ && rm -rf nginx-1.18.0
排错:
\color{red}排错:
排错:
yum install gcc gcc-c++ -y
yum search pcre // 清楚安装痕迹
yum install pcre-devel -y
yum installl openssl-devel -y
4. 直接启动nginx服务
nginx
5. nginx命令常用选项
-v 查看版本号
-V 查看版本号及编译选项
-s 给主进程发送信号,可接 stop | quit | reload
-t 测试配置是否正确(-q 结合,正确无内容输出,直接退出)
-c 指定配置文件(默认配置文件为 " $安装目录/conf/nginx.conf ")
-T 打印配置文件内容
6. 查看起启动状态
netstat -tlanp | grep 80 // 或 “ lsof -i:80 ”
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5535/nginx
7. 测试主页是否可以访问
8. 配置nginx为系统服务,并开机自启
- 创建 nginx.service 文件:
vim /lib/systemd/system/nginx.service
[Unit] #服务说明
Description=nginx service #描述服务
After=network.target #描述服务类别
[Service] #服务运行参数设置
Type=forking #后台运行形式
ExecStart=/usr/sbin/nginx #服务运行启动命令
ExecReload=/usr/sbin/nginx -s reload #服务重启命令
ExecStop=/usr/sbin/nginx -s quit #服务停止命令
PrivateTmp=true #给服务分配独立的临时空间
[Install]
WantedBy=multi-user.target
- 关闭以命令方式启动的nginx,然后以systemd方式启动nginx服务
# 关闭nginx
nginx -s stop
# 重新加载服务
systemctl daemon-reload
# 开机自启nginx
systemctl enable nginx
# 启动nginx服务
systemctl start nginx
# 关闭nginx服务
systemctl stop nginx
# 重启nginx服务
systemctl restart nginx
# 查看nginx服务状态
systemctl status nginx