首頁 運維 Nginx CentOS7如何安裝Nginx並設定自動啟動

CentOS7如何安裝Nginx並設定自動啟動

May 14, 2023 pm 03:01 PM
nginx centos7

1、官網下載安裝包

選擇適合linux的版本,這裡選擇最新的版本,下載到本地後上傳到伺服器或centos下直接wget指令下載。

CentOS7如何安裝Nginx並設定自動啟動

切換到/usr/local目錄,下載軟體包

# cd /usr/local
# wget http://nginx.org/download/nginx-1.11.5.tar.gz
登入後複製

2、安裝nginx

先執行以下指令,安裝nginx依賴函式庫,如果缺少依賴函式庫,可能會安裝失敗,具體可以參考文章後面的錯誤提示資訊。

# yum install gcc-c++
# yum install pcre
# yum install pcre-devel
# yum install zlib 
# yum install zlib-devel
# yum install openssl
# yum install openssl-devel
登入後複製

解壓縮安裝套件
##

# tar -zxvf nginx-1.11.5.tar.gz
登入後複製

nginx被解壓縮到了/usr/local/nginx-1.11.5 目錄下(不要把壓縮包解壓縮到/usr/local/nginx目錄下,或將解壓縮後的目錄重新命名為nginx,因為nginx會預設安裝到/usr/local/nginx目錄下),切換到nginx-1.11.5/目錄

# cd /usr/local/nginx-1.11.5/
登入後複製

執行# ./configure

# ./configure
登入後複製

此操作會偵測目前系統環境,以確保能成功安裝nginx,執行此操作後可能會出現下列幾種提示:


    checking for os

      linux 3.10.0-123.el7.x86_64 x86_64

    checking for c compiler ... not found

#cc#  notr.

##    如果出現以上錯誤提示信息,執行yum install gcc-c 安裝gcc,

    ./configure: error: the http rewrite module requires the pcre library.    you can either disable the module by using --without-http_rewrite_module

    option, or install the pcre library into the 是--with-pcre= option.

    如果出現上方提示,表示缺少pcre庫

    ./configure: error: the http gzip module requires the zlib library.

    you can either disable the module by using --without-http_gzip_module#    option, or bull the zlib library into the system,   option, or bull the zlib library into the system#or #    statically from the source with nginx by using --with-zlib= option.

    如果出現以上提示,表示缺少zlib庫

 如果沒有出現./configure: error提示,表示目前環境可以安裝nginx,執行make和make install編譯nginx

# make
# make install
登入後複製

沒有出錯的話,表示nginx已經成功安裝完成,預設安裝位置為/usr/local/nginx ,之前的/usr/local/nginx-1.11.5/可以刪除掉了。

如果出現cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,可能是你把安裝套件解壓到了/usr /local/nginx目錄,解決方法是將目錄重新命名為其他名稱後再執行make,make install.

3、配置nginx開機啟動


#切換到/lib/systemd/system/目錄,建立nginx.service檔案vim nginx.service

# cd /lib/systemd/system/
# vim nginx.service
登入後複製

檔案內容如下:

[unit]
description=nginx 
after=network.target 
 
[service] 
type=forking 
execstart=/usr/local/nginx/sbin/nginx
execreload=/usr/local/nginx/sbin/nginx reload
execstop=/usr/local/nginx/sbin/nginx quit
privatetmp=true 
 
[install] 
wantedby=multi-user.target
登入後複製

退出並儲存文件,執行systemctl enable nginx.service使nginx開機啟動

# systemctl enable nginx.service
登入後複製
systemctl start nginx.service    啟動nginx

systemctl stop nginx.service    結束nginx

systemcservice restart nginx.    結束nginx


systemcservice restart nginx.   

#4、驗證是否安裝成功

輸入http://伺服器ip/ 如果能看到nginx的介面,就表示安裝成功了

以上是CentOS7如何安裝Nginx並設定自動啟動的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

nginx怎麼配置雲服務器域名 nginx怎麼配置雲服務器域名 Apr 14, 2025 pm 12:18 PM

在雲服務器上配置 Nginx 域名的方法:創建 A 記錄,指向雲服務器的公共 IP 地址。在 Nginx 配置文件中添加虛擬主機塊,指定偵聽端口、域名和網站根目錄。重啟 Nginx 以應用更改。訪問域名測試配置。其他注意事項:安裝 SSL 證書啟用 HTTPS、確保防火牆允許 80 端口流量、等待 DNS 解析生效。

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼查版本 nginx怎麼查版本 Apr 14, 2025 am 11:57 AM

可以查詢 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打開 Nginx 錯誤頁,查看頁面的標題。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

docker怎麼啟動容器 docker怎麼啟動容器 Apr 15, 2025 pm 12:27 PM

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

怎麼啟動nginx服務器 怎麼啟動nginx服務器 Apr 14, 2025 pm 12:27 PM

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP

docker怎麼創建容器 docker怎麼創建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

See all articles