安装nginx
解压nginx,默认安装到/usr/local/nginx目录
tar -zxvf nginx-1.18.0.tar.gz
检测当前系统环境
cd /usr/local/nginx-1.18.0/
./configure
如果报错将以下的运行环境安装
yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
如果没有出现./configure: error提示,表示当前环境可以安装nginx,执行make和make install编译nginx
make
make install
没有出错的话,表示nginx已经成功安装完成,默认安装位置为/usr/local/nginx,解压的/usr/local/nginx-1.11.5/可以删除掉了。
如果出现cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,make install
配置开机自启
切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service
cd /lib/systemd/system/
vim nginx.service
将以下配置写入
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
退出并保存文件后,设置nginx开机启动
systemctl enable nginx.service
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #结束nginx
systemctl restart nginx.service #重启nginx
验证是否安装成功
方法一:输入http://服务器ip/
跳转到nginx界面,就表示安装成功了
方法二:进入nginx安装目录sbin下,输入命令./nginx -t
[root@localhost sbin]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
进程启动、停止、重启
ps -ef|grep nginx #查看进程号
kill -QUIT pid #从容停止
kill -TERM pid #快速停止
kill -INT pid #快速停止
kill -9 pid #强制停止
Nginx启动服务命令
描述 | |
---|---|
systemd | 保存,重新加载 |
systemctl daemon-reload | 保存,重新加载 |
systemctl enable nginx.service | 设置nginx服务开机自启动 |
systemctl start nginx.service | 启动nginx服务 |
systemctl stop nginx.service | 停止nginx服务 |
systemctl restart nginx.service | 重启nginx服务e |
systemctl reload nginx.service | 重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效) |
/usr/local/nginx/sbin/nginx | 路径启动,默认在 |
/usr/local/nginx/sbin/nginx -s reload | 平滑重启 |
/usr/local/nginx/sbin/nginx -s stop | 停止 |
/usr/local/nginx/sbin/nginx -s restart | 重启 |