在 CentOS 7 上使用 wget 安装 Nginx 并设置开机自启,你可以按照以下步骤进行操作:
- 
首先,确保你已经以 root 用户或者具有 sudo 权限的用户身份登录到 CentOS 7。
 - 
安装 Nginx 所需的依赖包。在终端中运行以下命令:
|---|-----------------------------------------------------------------------------------------|
| |sudo yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel| - 
使用 wget 下载 Nginx 的源代码包。在终端中运行以下命令(请检查是否有更新的版本):
|---|------------------------------------------------------|
| |wget http://nginx.org/download/nginx-1.22.1.tar.gz|注意:版本号可能随时间变化,请根据实际情况选择合适的版本。
 - 
解压下载的 Nginx 源代码包:
|---|---------------------------------|
| |tar -zxvf nginx-1.22.1.tar.gz| - 
进入解压后的目录:
|---|--------------------|
| |cd nginx-1.22.1/| - 
编译和安装 Nginx:
|---|------------------------------------------|
| |./configure --prefix=/usr/local/nginx|
| |make|
| |make install| - 
安装完成后,配置 Nginx 为系统服务以便开机自启。创建一个 systemd 服务文件,例如
/usr/lib/systemd/system/nginx.service,内容如下:|---|--------------------------------------------------------------------------|
| |[Unit]|
| |Description=The NGINX HTTP and reverse proxy server|
| |After=syslog.target network.target remote-fs.target nss-lookup.target|
| | |
| |[Service]|
| |Type=forking|
| |PIDFile=/usr/local/nginx/logs/nginx.pid|
| |ExecStartPre=/usr/local/nginx/sbin/nginx -t|
| |ExecStart=/usr/local/nginx/sbin/nginx|
| |ExecReload=/bin/kill -s HUP $MAINPID|
| |ExecStop=/bin/kill -s QUIT $MAINPID|
| |PrivateTmp=true|
| | |
| |[Install]|
| |WantedBy=multi-user.target|请确保
PIDFile和ExecStart中的路径与你的 Nginx 安装路径一致。 - 
重新加载 systemd 配置文件:
|---|--------------------------------|
| |sudo systemctl daemon-reload| - 
启动 Nginx 服务并设置为开机自启:
|---|-------------------------------|
| |sudo systemctl start nginx|
| |sudo systemctl enable nginx| - 
检查 Nginx 服务状态:
|---|-------------------------------|
| |sudo systemctl status nginx|