易西 发表于 2025-9-25 19:33:34

linux手动安装nginx后如何启动以及开机自启动

linux手动安装nginx后如何启动以及开机自启动

易西 发表于 2025-9-25 19:34:15



linux手动安装nginx后如何启动以及开机自启动
在Linux系统中手动安装Nginx并启动以及设置开机自启动,可以通过以下步骤进行。这里以Ubuntu系统为例,因为Ubuntu是目前使用最广泛的Linux发行版之一,但这些步骤在大多数基于Debian的Linux发行版(如Debian本身、Linux Mint等)上都是类似的。

1. 安装Nginx
首先,你需要确保你的系统已经安装了curl和gnupg2(或者其他类似的工具,比如wget),因为我们会用到它们来添加Nginx官方仓库的签名。

更新你的包管理器:

sudo apt update

安装curl和gnupg2(如果尚未安装):

sudo apt install curl gnupg2 ca-certificates lsb-release

添加Nginx官方签名密钥:

curl -s https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

添加Nginx官方仓库:

echo "deb http://nginx.org/packages/ubuntu/ `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

再次更新你的包管理器以获取Nginx包:

sudo apt update

安装Nginx:

sudo apt install nginx

2. 启动Nginx
安装完成后,你可以通过以下命令启动Nginx:

sudo systemctl start nginx
3. 设置开机自启动
要让Nginx在系统启动时自动启动,你可以使用以下命令:

sudo systemctl enable nginx
4. 检查Nginx状态
为了确认Nginx是否成功启动,你可以使用以下命令来检查其状态:

sudo systemctl status nginx
5. 访问Nginx默认页面(可选)
最后,你可以通过在浏览器中访问http://your-server-ip来查看Nginx的默认页面,确认一切设置正确。确保将your-server-ip替换为你的服务器IP地址。

6. 防火墙配置(如果需要)
如果你的服务器运行了UFW防火墙,你可能需要允许HTTP和HTTPS流量通过防火墙:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
重新加载UFW规则:

sudo ufw reload
以上步骤应该可以帮助你在大多数基于Debian的Linux系统上安装、启动和设置Nginx的开机自启动。如果你使用的是其他Linux发行版(如CentOS、Fedora等),安装步骤会有所不同,但基本原理相似。例如,在CentOS上,你会使用yum或dnf来安装Nginx,而在Fedora上,你会使用dnf。在这些系统中,你同样可以使用systemctl来管理服务。

参考

易西 发表于 2025-9-25 19:37:21



linux cento手动安装nginx后如何启动以及开机自启动
在CentOS系统中手动安装Nginx,并确保它能够在启动时自动运行,你可以按照以下步骤进行操作。

1. 安装Nginx
首先,你需要确保你的系统已经安装了必要的软件包,如gcc、pcre-devel和openssl-devel等,这些是编译Nginx所必需的。然后,你可以从Nginx的官方网站下载源码包,编译安装,或者使用yum/dnf来安装。

使用YUM/DNF安装(推荐方法)
如果你使用的是CentOS 7或更高版本,可以使用yum或dnf(CentOS 8及以后使用dnf)来安装Nginx。

sudo yum install epel-release# CentOS 7及以下版本需要此步骤
sudo yum update             # 更新软件包索引
sudo yum install nginx      # 安装Nginx
对于CentOS 8及之后版本:

sudo dnf install epel-release# 可能需要此步骤
sudo dnf update
sudo dnf install nginx
从源码编译安装(可选)
如果你需要从源码编译安装Nginx,可以按照以下步骤操作:

下载Nginx源码包:

wget http://nginx.org/download/nginx-1.20.1.tar.gz# 请替换为最新版本号
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1

安装依赖:

sudo yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

编译并安装Nginx:

./configure
make
sudo make install

2. 启动Nginx
安装完成后,你可以使用以下命令来启动Nginx:

sudo systemctl start nginx
或者使用:

sudo service nginx start
3. 设置开机自启动
要让Nginx在系统启动时自动运行,你可以使用以下命令设置开机自启动:

sudo systemctl enable nginx
或者使用:

sudo chkconfig nginx on# CentOS 7及以下版本使用此命令
4. 检查Nginx状态和日志
启动后,你可以检查Nginx的状态:

sudo systemctl status nginx
或者查看Nginx的错误日志和访问日志:

tail -f /var/log/nginx/error.log# 查看错误日志
tail -f /var/log/nginx/access.log # 查看访问日志
以上步骤应该可以帮助你在CentOS系统上成功安装、启动并设置Nginx的开机自启动。

提示:AI自动生成,仅供参考

参考

易西 发表于 2025-9-25 19:44:26

https://cloud.tencent.com/developer/article/2143720
参考

易西 发表于 2025-9-25 19:46:43

https://blog.51cto.com/u_16099302/12619470

参考

易西 发表于 2025-9-25 21:00:01

. 创建启动脚本(init script)
为了方便地启动、停止和重启 Nginx,你可以创建一个 init 脚本。例如,创建一个 /etc/init.d/nginx 文件:

sudo vim /etc/init.d/nginx
在文件中添加以下内容(根据实际安装路径调整):

#!/bin/sh
# chkconfig:   2345 80 20
# description:Nginx init script
# processname: nginx
NGINX_BIN=/usr/local/nginx/sbin/nginx
test -x $NGINX_BIN || exit 0
case "$1" in
    start)
      $NGINX_BIN
      ;;
    stop)
      $NGINX_BIN -s stop
      ;;
    reload)
      $NGINX_BIN -s reload
      ;;
    restart)
      $NGINX_BIN -s stop || exit 0
      $NGINX_BIN
      ;;
    *)echo "Usage: $0 {start|stop|reload|restart}" >&2
      exit 1
      ;;esacexit 0# End of script. The rest is just for explanation.# chkconfig:   2345 80 20 # description:Nginx init script # processname: nginx NGINX_BIN=/usr/local/nginx/sbin/nginx test -x $NGINX_BIN || exit 0 case "$1" in start) $NGINX_BIN ;; stop) $NGINX_BIN -s stop ;; reload) $NGINX_BIN -s reload ;; restart) $NGINX_BIN -s stop || exit 0 $NGINX_BIN ;; *) echo "Usage: $0 {start|stop|reload|restart}" >&2 exit 1 ;; esac exit 0# End of script. The rest is just for explanation.# chkconfig:   2345 80 20 # description:Nginx init script # processname: nginx NGINX_BIN=/usr/local/nginx/sbin/nginx test -x $NGINX_BIN || exit 0 case "$1" in start) $NGINX_BIN ;; stop) $NGINX_BIN -s stop ;; reload) $NGINX_BIN -s reload ;; restart) $NGINX_BIN -s stop || exit 0 $NGINX_BIN ;; *) echo "Usage: $0 {start|stop|reload|restart}" >&2 exit 1 ;; esac exit 0# End of script. The rest is just for explanation.# chkconfig:   2345 80 20 # description:Nginx init script # processname: nginx NGINX_BIN=/usr/local/nginx/sbin/nginx test -x $NGINX_BIN || exit 0 case "$1" in start) $NGINX_BIN ;; stop) $NGINX_BIN -s stop ;; reload) $NGINX_BIN -s reload ;; restart
提示:AI自动生成,仅供参考


参考

易西 发表于 2025-9-25 21:11:04



/usr/local/nginx/sbin ./nginx 如何开机自动执行
要让 Nginx 在开机时自动执行,你需要根据你所使用的操作系统采取不同的方法。下面是一些常见操作系统的步骤:

对于 Linux 系统(以 systemd 为例,如 Ubuntu 16.04+、CentOS 7+)
确保 Nginx 的服务文件存在:

通常,Nginx 的服务文件位于 /etc/systemd/system/ 目录下。如果这个文件不存在,你可以通过创建一个符号链接来链接到 Nginx 的实际服务文件。例如,如果你的 Nginx 安装在 /usr/local/nginx/sbin/ 目录下,你可以这样做:

sudo ln -s /usr/local/nginx/sbin/nginx /etc/systemd/system/nginx.service

创建或修改 systemd 服务文件:

如果上面的链接方法不可行,或者你需要自定义服务文件,你可以创建一个新的服务文件。例如,创建一个名为 /etc/systemd/system/nginx.service 的文件,并添加以下内容:


Description=The NGINX HTTP and reverse proxy server
After=network.target


Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload


WantedBy=multi-user.target

注意替换 ExecStart 和 PIDFile 中的路径为你实际的 Nginx 安装路径。

重新加载 systemd 配置:

sudo systemctl daemon-reload

启用并启动 Nginx 服务:

sudo systemctl enable nginx
sudo systemctl start nginx

检查状态:

sudo systemctl status nginx

对于使用 init.d 的系统(如 CentOS 6 或更早的版本)
创建或修改 init.d 脚本:

创建一个新的脚本 /etc/init.d/nginx,并添加以下内容:

#!/bin/sh
# chkconfig: 2345 80 20
# description: Nginx is a high-performance web and reverse proxy server and a IMAP/POP3 proxy server

case "$1" in
    start)
      /usr/local/nginx/sbin/nginx
      ;;
    stop)
      /usr/local/nginx/sbin/nginx -s stop
      ;;
    reload)
      /usr/local/nginx/sbin/nginx -s reload
      ;;
    *)
      echo "Usage: $0 {start|stop|reload}"
      exit 1
esac
exit 0

设置脚本为可执行:

sudo chmod +x /etc/init.d/nginx

添加到 chkconfig:

sudo chkconfig --add nginx
sudo chkconfig nginx on

启动 Nginx:

sudo service nginx start

检查状态:

sudo service nginx status

根据你的具体操作系统版本和配置,选择合适的方法来设置 Nginx 开机自启。如果你使用的是其他类型的 init 系统(如 SysVinit、Upstart 等),步骤会有所不同。但基本思想是相同的:创建或修改一个服务脚本来管理 Nginx 的启动、停止和重载,然后确保这个服务在系统启动时被自动调用。

提示:AI自动生成,仅供参考

参考
页: [1]
查看完整版本: linux手动安装nginx后如何启动以及开机自启动