linux安装nginx并开机启动

本文档详细介绍了在Linux环境下如何下载、解压、配置、编译、安装Nginx,包括添加HTTP_GZIP_STATIC_MODULE模块进行静态文件压缩,并提供了一个用于启动、停止、重启和检查状态的初始化脚本。同时,提到了配置文件中设置nginx.pid路径的方法,以及如何将Nginx添加到开机自启动服务。

nginx下载地址:https://nginx.org/download/

## 解压

tar -zxvf nginx-1.9.9.tar.gz

##进入nginx目录

cd nginx-1.9.9

配置./configure --prefix=/usr/local/nginx

编译

make

安装

make install

## 配置vue压缩

./configure --prefix=/usr/local/nginx --with-http_gzip_static_module

## 重新安装

make && make install

测试是否安装成功

# cd到刚才配置的安装目录/usr/loca/nginx/
./sbin/nginx -t

make: *** No rule to make target `build', needed by yum update

更新完成后,先删除之前准备make的nginx包,然后重新解压一个。

 

安装前置库:

[root@cqakserver local]# yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel

启动nginx:

Nginx安装目录下的sbin目录下./nginx起动;

开机启动:

Nginx和NGINX_CONF_FILE改成自己的。

在/etc/init.d/下面创建nginx文件

脚本如下

#!/bin/sh
# chkconfig: 2345 10 90  
# description: Start and Stop nginx

# 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:     /usr/local/nginx/logs/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

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 $nginx -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

chmoh 777 nginx  给文件权限

chkconfig –add nginx 添加到开机自启

nginx.pid问题:nginx.conf文件配置pid路径:#pid   /usr/local/nginx/logs/nginx.pid

<think>好的,我现在需要在Rocky Linux安装配置Nginx设置开机启动。首先,我应该从哪里开始呢?记得用户提到Rocky Linux 8和9,所以步骤可能类似,但要注意版本差异。 首先,安装Nginx。Rocky Linux属于RHEL系,应该使用yum或者dnf来安装。可能需要先用EPEL仓库,不过不确定Nginx是否在默认仓库里。可能需要先添加Nginx的官方仓库,因为默认仓库里的版本可能比较旧。或者用户可能希望用默认的版本,但用户提到的是安装配置,可能直接通过dnf安装。 然后,安装完成后,配置文件的位置是/etc/nginx/nginx.conf,用户引用里提到过这个路径。所以配置的时候需要修改这个文件。之后要设置开机启动,应该用systemctl enable nginx,然后启动服务检查状态。 不过具体步骤可能有遗漏,比如防火墙设置,开放80和443端口,或者SELinux配置?用户可能没有提到这些,但作为完整的配置可能需要考虑。不过用户的问题主要是安装配置开机,所以重点在这三个步骤。 步骤大概是:1. 安装Nginx;2. 修改配置文件;3. 启动设置开机。同时可能需要验证安装是否成功,通过访问服务器IP看看欢迎页面。 需要注意权限问题吗?比如是否需要sudo来执行命令。还有,安装前是否需要更新系统包?虽然不是必须,但建议先运行sudo dnf update。 另外,关于配置文件的修改,用户可能只是需要基本的配置,或者有特定需求。但用户的问题中没有具体说明配置内容,所以可能只需要确认配置文件的位置,确保服务启动。 然后,开机启动部分,使用systemctl enable nginx命令即可。之后用systemctl start nginx启动服务,用systemctl status nginx检查状态。 可能需要提醒用户在防火墙中允许HTTP和HTTPS流量,比如使用firewall-cmd命令添加服务,然后重新加载防火墙。 总结步骤: 1. 更新系统包(可选) 2. 安装Nginx:sudo dnf install nginx 3. 启动Nginx设置开机启动:sudo systemctl enable --now nginx 4. 配置防火墙(如果需要):sudo firewall-cmd --permanent --add-service=http --add-service=https;sudo firewall-cmd --reload 5. 修改配置文件(根据需求编辑/etc/nginx/nginx.conf) 6. 测试配置是否正确:sudo nginx -t 7. 重新加载Nginx:sudo systemctl reload nginx 这样用户就能完成安装配置开机启动了。需要确保每个步骤的命令正确,特别是Rocky Linux中使用的是dnf而不是yum,虽然两者在RHEL8+中都可以用,但dnf是默认的。 另外,在回答中需要引用用户提供的引用内容,比如引用[2]提到配置文件的位置,所以步骤里要提到这一点,正确标注引用标记。</think>### 在 Rocky Linux安装配置 Nginx 设置开机 #### **1. 安装 Nginx** 首先更新系统包安装 Nginx: ```bash sudo dnf update -y # 更新系统包(可选) sudo dnf install nginx -y # 安装Nginx ``` Nginx 的官方仓库通常已包含在 Rocky Linux 默认源中,若需最新版本可手动添加 Nginx 官方仓库[^2]。 --- #### **2. 启动服务设置开机** 安装完成后,启动服务设为开机: ```bash sudo systemctl enable nginx # 设置开机 sudo systemctl start nginx # 立即启动服务 sudo systemctl status nginx # 检查运行状态 ``` 若状态显示 `active (running)`,则表明服务已成功启动。 --- #### **3. 配置防火墙(可选)** 允许 HTTP/HTTPS 流量通过防火墙: ```bash sudo firewall-cmd --permanent --add-service=http --add-service=https sudo firewall-cmd --reload ``` --- #### **4. 修改配置文件** Nginx 的主配置文件路径为 `/etc/nginx/nginx.conf`[^2],按需编辑: ```bash sudo vi /etc/nginx/nginx.conf # 使用文本工具修改配置 ``` 修改后验证语法重新加载配置: ```bash sudo nginx -t # 检查语法是否正确 sudo systemctl reload nginx # 应用新配置 ``` --- #### **5. 验证安装** 浏览器访问服务器 IP 或域名,若显示 Nginx 默认欢迎页,则安装成功。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值