1. 下载安装
1.1 下载安装包
下载地址:/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 install
2. 修改配置
vi /usr/local/nginx/conf/nginx.conf
配置文件内容:
# 修改用户为 rootuser 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 reload
3. 加入系统服务并开机自启
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/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {# make required directoriesuser=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`if [ -n "$user" ]; thenif [ -z "`grep $user /etc/passwd`" ]; thenuseradd -M -s /bin/nologin $userfioptions=`$nginx -V 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d "=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifidonefi}start() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $prog -HUPretval=$?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" instart)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 2esac
修改其中两处:
# 修改此处为实际 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 startservice nginx stop service nginx restart
4. 参考链接
/resources/wiki/start/topics/examples/redhatnginxinit/
/weixin_36198509/article/details/113086213
/datadev_sh/article/details/83819791