在linux系统下安装两个nginx以及启动、停止、重起

如果没有安装过nginx请看:linux下nginx部署以及配置详解

1、第一个nginx已经安装完成后,现在安装第二个nginx

启动:sudo /usr/sbin/nginx3
重起:sudo /usr/sbin/nginx3 -s reload
停止:sudo /usr/sbin/nginx3 -s stop

一、进入nginx目录进行编译安装

1、切换目录:

cd /usr/local/src/nginx-1.16.1
安装第二个路径会有所不同,我这里安装的是第三个nginx3,如果要安装4个 就改为nginx4,需要注意nginx.conf不需要改变,命令如下:

./configure \
--prefix=/usr/local/nginx3 \
--sbin-path=/usr/sbin/nginx3 \
--conf-path=/etc/nginx3/nginx.conf \
--error-log-path=/var/log/nginx3/error.log \
--http-log-path=/var/log/nginx3/access.log \
--pid-path=/var/run/nginx3.pid \
--lock-path=/var/run/nginx3.lock \
--http-client-body-temp-path=/var/tmp/nginx3/client \
--http-proxy-temp-path=/var/tmp/nginx3/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx3/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx3/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx3/scgi \
--user=nginx3 \
--group=nginx3 \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
View Code
 
  

  执行完后下面就是nginx3的路径

 
 

2、安装编译:

make && make install

mkdir -pv /var/tmp/nginx3/client

3、添加SysV启动脚本

vi /etc/init.d/nginx3

按i进入编辑状态,需要多个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="/usr/sbin/nginx3"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx3/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
killall -9 nginx
}
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
View Code

 

4.赋予脚本执行权限

chmod +x /etc/init.d/nginx3

8.添加nginx服务进程用户

groupadd -r nginx3

useradd -r -g nginx nginx3

9、添加至服务管理列表,设置开机自启

chkconfig --add nginx3

chkconfig nginx3 on

 10、修改nginx.conf端口号,我这里改为81

vi /etc/nginx3/nginx.conf

 11、启动nginx3

/usr/sbin/nginx3

查看端口号
netstat -lntp

 

 11、在启动一个nginx

 

 12、停止nginx3

sudo /usr/sbin/nginx3 -s stop

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Linux系统安装两个Nginx服务器可以通过以下步骤完成: 1. 打开终端,在终端中输入以下命令来安装第一个Nginx: ```shell sudo apt-get update sudo apt-get install nginx ``` 上述命令将更新包列表并安装Nginx。 2. 安装完成后,可以通过以下命令来检查Nginx是否已成功安装: ```shell nginx -v ``` 如果显示了Nginx的版本信息,则说明已成功安装。 3. 接下来,我们需要创建第二个Nginx实例的配置文件。在终端中输入以下命令,打开Nginx配置文件: ```shell sudo nano /etc/nginx/nginx.conf ``` 4. 在打开的配置文件中,找到 `http` 模块的配置块,这里是添加第二个Nginx实例的位置。在 `http` 配置块中添加以下内容: ```shell http { # 其他配置项... } http { include /etc/nginx/sites-enabled/*; } ``` 上述代码中的 `include` 配置将会引入 `/etc/nginx/sites-enabled/` 目录下的所有配置文件。 5. 创建第二个Nginx实例的配置文件。在终端中输入以下命令: ```shell sudo nano /etc/nginx/sites-available/example.com ``` 将文件名 `example.com` 替换为你想要设置的域名。 6. 在打开的配置文件中,添加以下内容: ```shell server { listen 80; server_name example.com; root /var/www/example.com/html; index index.html; #其他配置项... } ``` 上述代码中,我们设置了监听的端口、服务器名、根目录和默认的索引文件。 7. 启用第二个Nginx实例的配置文件。在终端中输入以下命令来创建一个符号链接(软链接): ```shell sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ ``` 8. 检查Nginx配置是否正确。在终端中输入以下命令: ```shell sudo nginx -t ``` 如果没有任何错误信息,说明配置正确。 9. 重启Nginx服务器。在终端中输入以下命令: ```shell sudo service nginx restart ``` 10. 现在,你可以在浏览器中输入第二个Nginx实例的域名(例如:example.com)来访问该服务器。 通过以上步骤可以在Linux安装并配置两个独立的Nginx服务器实例。 ### 回答2: 在Linux系统安装两个nginx的步骤如下: 1. 打开终端,使用root用户或者sudo权限执行以下命令来安装nginx: ``` sudo apt-get update sudo apt-get install nginx ``` 2. 安装完成后,进入nginx配置文件的目录,通常是`/etc/nginx/`,可以使用以下命令进入目录: ``` cd /etc/nginx/ ``` 3. 复制一份默认配置文件,用于创建第二个nginx实例的配置: ``` sudo cp nginx.conf nginx2.conf ``` 4. 修改新的配置文件`nginx2.conf`,可通过以下命令打开并进行编辑: ``` sudo nano nginx2.conf ``` 5. 在配置文件中修改监听端口: ``` listen 81; ``` 6. 修改`pid`文件路径,以避免与第一个nginx实例冲突: ``` pid /run/nginx/nginx2.pid; ``` 7. 修改`access_log`和`error_log`文件路径: ``` access_log /var/log/nginx/nginx2-access.log; error_log /var/log/nginx/nginx2-error.log; ``` 8. 保存并退出配置文件。 9. 执行以下命令启动第二个nginx实例: ``` sudo nginx -c /etc/nginx/nginx2.conf ``` 10. 确保第二个nginx实例已经成功启动,可以通过以下命令检查进程是否存在: ``` ps -ef | grep nginx ``` 11. 如果需要在系统启动时自动启动第二个nginx实例,可以将启动命令添加到`/etc/rc.local`文件中。 通过以上步骤,您就可以在Linux系统上成功安装和配置两个独立的nginx实例。一个是默认的实例监听80端口,另一个是修改后的实例监听81端口。 ### 回答3: 要在Linux系统安装两个Nginx,可以按照以下步骤进行操作: 1. 首先,确保你的系统已经安装了必要的编译工具和依赖项。可以使用以下命令安装: ``` sudo apt-get update sudo apt-get install build-essential ``` 2. 接下来,你需要下载和编译Nginx源代码。你可以在Nginx的官方网站上找到最新的稳定版本。使用以下命令下载和解压: ``` wget http://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1.tar.gz ``` 3. 进入解压后的Nginx目录,并执行配置和编译命令: ``` cd nginx-1.20.1 ./configure make sudo make install ``` 4. 默认情况下,Nginx会被安装在`/usr/local/nginx`目录下。现在你可以通过执行以下命令来启动Nginx: ``` sudo /usr/local/nginx/sbin/nginx ``` 5. 要安装第二个Nginx实例,你需要使用不同的配置文件和端口。创建一个新的Nginx配置文件,例如`nginx2.conf`: ``` sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx2.conf ``` 6. 编辑新的配置文件,在其中更改监听端口和其他必要的配置项: ``` sudo nano /usr/local/nginx/conf/nginx2.conf ``` 7. 启动第二个Nginx实例,并指定新的配置文件: ``` sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx2.conf ``` 现在,你可以在Linux系统上同时运行两个独立的Nginx实例。记住要根据需要调整每个实例的配置文件来避免冲突,并确保每个实例使用不同的监听端口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值