一:安装nginx
1、访问nginx官网在download页面,选择版本为Stable version,下载最新tar包。
2、上传tar包至服务器,目录统一放置/usr/local目录下
ssh工具使用方法请参考:http://kirayamato.fun/20240913/b9c12786d045/
cd /usr/local
解压
tar zxvf nginx-1.26.2.tar.gz
3、通过yum安装依赖包
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
4、进入nginx目录,编译
cd nginx-1.26.2
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module
5、make编译安装
make&&make install
6、启动nginx
/usr/local/nginx/sbin/nginx
查看状态
ps aux| grep nginx
7、立即停止服务
/usr/local/nginx/sbin/nginx -s stop
8、查看端口占用情况
netstat -tlnp
9、添加至环境变量
vim /etc/profile
添加以下配置至profile
export PATH=$PATH:/usr/local/nginx/sbin/nginx
刷新配置
source /etc/profile
二:设置开机自启
1、编写脚本
vim /etc/init.d/nginx
内容如下
#! /bin/sh
# chkconfig:35 85 15
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
nginx=/usr/local/nginx/sbin/nginx
case $1 in
start)
netstat -anptu|grep nginx
if [ $? -eq 0 ]; then
echo “nginx server is already running”
else
echo “nginx server begin start…”
$nginx
fi
;;
stop)
$nginx -s stop
if [ $? -eq 0 ]; then
echo “nginx server is stop”
else
echo “nginx server stop fail,try again!”
fi
;;
status)
netstat -anptu|grep nginx
if [ $? -eq 0 ]; then
echo “nginx server is running”
else
echo “nginx server is stoped”
fi
;;
reload)
$nginx -s reload
if [ $? -eq 0 ]; then
echo “nginx server is begin reload”
else
echo “nginx server reload fail!”
fi
;;
*)
echo “please enter {start|reload|status|stop}”
;;
esac
编辑结束后使用shift+:wq,写入并退出
2、添加权限
chmod 775 /etc/init.d/nginx
3、添加nginx开机自启
chkconfig --add nginx
查看自启列表
chkconfig --list
三:常用命令列表
1、service命令
service nginx start #启动
service nginx stop #立即停止
service nginx status #查看状态
service nginx reload #重载
2、全路径命令
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop #立即停止
/usr/local/nginx/sbin/nginx -s reload #重载
3、nginx根目录:
/usr/local/nginx