溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

linux下安裝nginx的步驟是什么

發布時間:2022-01-24 09:48:59 來源:億速云 閱讀:412 作者:柒染 欄目:開發技術
# Linux下安裝Nginx的步驟是什么

Nginx是一款高性能的HTTP和反向代理服務器,在Linux系統中被廣泛使用。本文將詳細介紹在主流Linux發行版(Ubuntu/Debian/CentOS)上安裝Nginx的完整流程,包含源碼編譯和包管理器兩種安裝方式。

## 一、準備工作

### 1. 系統要求
- Linux操作系統(推薦Ubuntu 20.04+/CentOS 7+)
- 至少1GB可用磁盤空間
- root或具有sudo權限的用戶

### 2. 更新系統包
安裝前建議先更新系統軟件包:

```bash
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y

# CentOS/RHEL
sudo yum update -y

二、通過包管理器安裝(推薦)

1. Ubuntu/Debian系統安裝

添加Nginx官方倉庫

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

安裝Nginx

sudo apt update
sudo apt install nginx -y

2. CentOS/RHEL系統安裝

添加EPEL倉庫

sudo yum install epel-release -y

安裝Nginx

sudo yum install nginx -y

3. 驗證安裝

nginx -v
# 應顯示類似:nginx version: 1.25.3

三、通過源碼編譯安裝(高級)

1. 下載源碼包

wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3

2. 安裝依賴項

# Ubuntu/Debian
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

# CentOS/RHEL
sudo yum install gcc make pcre-devel zlib-devel openssl-devel

3. 編譯配置

./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module

4. 編譯安裝

make
sudo make install

5. 創建系統服務

創建服務文件/lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MNPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

啟用服務:

sudo systemctl enable nginx
sudo systemctl start nginx

四、基本配置與管理

1. 服務管理命令

# 啟動服務
sudo systemctl start nginx

# 停止服務
sudo systemctl stop nginx

# 重啟服務
sudo systemctl restart nginx

# 重載配置
sudo systemctl reload nginx

# 查看狀態
sudo systemctl status nginx

2. 配置文件結構

  • 主配置文件:/etc/nginx/nginx.conf(包管理器安裝)
  • 站點配置:/etc/nginx/conf.d//etc/nginx/sites-enabled/
  • 默認網站根目錄:/usr/share/nginx/html

3. 測試配置文件

sudo nginx -t
# 成功輸出示例:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

五、防火墻配置

1. 開放HTTP/HTTPS端口

# Ubuntu使用ufw
sudo ufw allow 'Nginx Full'

# CentOS使用firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

六、驗證安裝

在瀏覽器訪問服務器IP:

http://your_server_ip

應看到Nginx歡迎頁面。

七、卸載Nginx

1. 包管理器安裝的卸載

# Ubuntu/Debian
sudo apt purge nginx nginx-common -y

# CentOS/RHEL
sudo yum remove nginx -y

2. 源碼安裝的卸載

sudo rm -rf /usr/local/nginx
sudo rm /lib/systemd/system/nginx.service
sudo systemctl daemon-reload

常見問題解決

  1. 端口沖突:如果Apache已占用80端口,需先停止Apache服務
  2. 權限問題:確保Nginx用戶(通常為www-data或nginx)對網站目錄有讀取權限
  3. SELinux阻止:在CentOS上可能需要調整SELinux策略或設置為寬松模式

通過以上步驟,您應該已成功在Linux系統上安裝并運行Nginx服務器。建議生產環境使用包管理器安裝以獲得自動更新支持,開發環境可選用源碼編譯以獲得更靈活的定制選項。 “`

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女