1.1 下载安装包
下载地址:http://nginx.org/en/download.html
下载 linux 稳定版本,如下图:
1.2 解压并安装
# 解压 tar -zxvf nginx-1.20.1.tar.gz # 重命名文件夹 mv nginx-1.20.1 nginx && cd nginx # 安装 ./configure && make && make install2. 修改配置
vi /usr/local/nginx/conf/nginx.conf
配置文件内容:
# 修改用户为 root
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 目录配置
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
server {
listen 80; # 监听端口
server_name _;
root /data/; # 文件服务根目录
location / {
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# 重新加载配置并重启 nginx 服务 /usr/local/nginx/sbin/nginx -s reload
其他相关操作命令
# 启动 /usr/local/nginx/sbin/nginx # 停止,此方式相当于先查出 nginx 进程 id 再使用 kill 命令强制杀掉进程 /usr/local/nginx/sbin/nginx -s stop # 退出,此方式停止步骤是待 nginx 进程处理任务完毕进行停止 /usr/local/nginx/sbin/nginx -s quit # 重启,重新加载配置文件并启动服务 /usr/local/nginx/sbin/nginx -s reload3. 加入系统服务并开机自启
3.1 在 /etc/init.d/ 路径下创建 nginx 文件
vi /etc/init.d/nginx
nginx 文件完整内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# 修改此处为实际 nginx 所在路径
#nginx="/usr/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
# 修改此处为实际 nginx 配置文件路径
#NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
retval=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
修改其中两处:
# 修改此处为实际 nginx 所在路径 nginx="/usr/local/nginx/sbin/nginx" # 修改此处为实际 nginx 配置文件路径 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
3.2 赋予权限
chmod a+x /etc/init.d/nginx
3.3 加入 chkconfig 管理
chkconfig --add /etc/init.d/nginx
3.4 设置开机自启动
chkconfig nginx on
3.5 通过 service 启动、停止、重启
service nginx start service nginx stop service nginx restart4. 参考链接
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
https://blog.csdn.net/weixin_36198509/article/details/113086213
https://blog.csdn.net/datadev_sh/article/details/83819791



