CentOS 7學習筆記(二)之Nginx安裝

來源:互聯網
上載者:User

標籤:

說明:

1、這篇學習記錄的目的是如何在CentOS 7上面安裝Nginx,包括兩種安裝方式,yum源安裝和原始碼編譯安裝;

2、CentOS 7初學者,某些觀點帶有猜測之意,文中不足之處,還請批評指正,定會虛心受教。

第一部分 yum源安裝

  yum源安裝相對比較容易,不必處理複雜的依賴關係。初次安裝,我就先採用yum安裝方式。根據nginx的wiki上面的安裝介紹。

第一步 添加yum源

  為了獲得最新版的nginx,nginx安裝說明建議直接從nginx官網下載,需要建立一個源。建立檔案 /etc/yum.repos.d/nginx.repo,內容如下:

[nginx]                                                  //源名稱,會被yum識別name= nginx repo                                         //檔案功能說明,只是便於理解這個檔案的作用baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ //地址,非常重要,其中7(針對7.x)是版本,x86_64是CPU基本體系
gpgcheck=0 //gpg校正,0代表不校正,1代表校正
enabled=1 //啟用,0代表不啟用,1代表啟用

  其中baseurl中的$releasever、$basearch是取得系統中的版本、CPU基本體系,這兩個變數使用在yum檔案中,具體的變數定義可以尋找資料。

  當編輯好檔案後,執行命令,如下:

[[email protected] etc]$ yum repolist已載入外掛程式:fastestmirrorLoading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * extras: mirror.bit.edu.cn * updates: mirrors.btte.net源標識                              源名稱                                 狀態base/7/x86_64                       CentOS-7 - Base                        8,465extras/7/x86_64                     CentOS-7 - Extras                        104nginx/7/x86_64                      nginx repo                                13                   //說明新加的源生效updates/7/x86_64                    CentOS-7 - Updates                     1,605 repolist: 10,187

   當系統中配置了很多源,並且會有相同的軟體,我們就需要指定源安裝,如下:

[[email protected] etc]$ sudo yum install nginx --enablerepo=nginx[sudo] password for test:....================================================================================ Package        架構            版本                                     大小================================================================================正在安裝: nginx          x86_64          1:1.6.2-1.el7.ngx          nginx          353 k事務概要================================================================================安裝  1 軟體包總下載量:353 k安裝大小:845 kIs this ok [y/d/N]: y......  正在安裝    : 1:nginx-1.6.2-1.el7.ngx.x86_64                              1/1----------------------------------------------------------------------.....----------------------------------------------------------------------  驗證中      : 1:nginx-1.6.2-1.el7.ngx.x86_64                              1/1已安裝:  nginx.x86_64 1:1.6.2-1.el7.ngx完畢!

 查看nginx預設設定檔 /etc/nginx/conf.d/default.conf,內容如下:

server {    listen       80;                                //監聽連接埠80    server_name  localhost;               //伺服器名稱    #charset koi8-r;    #access_log  /var/log/nginx/log/host.access.log  main;    location / {        root   /usr/share/nginx/html;                //網站根目錄位置        index  index.html index.htm;          //網站首頁預設頁面名稱    }
......

查看nginx服務狀態,如下:

[[email protected] /]$ sudo systemctl status nginx.servicenginx.service - nginx - high performance web server   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)   Active: inactive (dead)      //服務未開啟

開啟nginx服務,如下:

 sudo systemctl start nginx.service
//再次查看狀態,如下

nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: active (running) since 二 2015-01-27 22:53:46 CST; 44s ago                 //服務已開啟
......

當我們用另外一台PC訪問該伺服器時,卻發現,無法開啟,是因為預設情況下防火牆開啟,禁止訪問80連接埠(此時,我們聯想到之前SSH,連接埠22,防火牆預設允許存取)。我們需要添加防火牆規則。同時,我們也可以先關閉防火牆,測試是否配置正確。

備忘:聽說之前的防火牆牌子是iptables,CentOS 7 預設是firewall,服務名稱是firewalld。

添加防火牆規則如下:

sudo firewall-cmd --add-port=80/tcp          //http協議基於TCP傳輸協議,允許存取80連接埠

此時我們可以使用命令,來查看firewalld開放連接埠情況,前後比較如下:

之前:
[[email protected] /]$ sudo firewall-cmd --list-allpublic (default) interfaces: sources: services: dhcpv6-client ssh //這是預設允許存取的 ports: //這裡是空的 ......之後:
[[email protected] /]$ sudo firewall-cmd --list-allpublic (default) interfaces: sources: services: dhcpv6-client ssh ports: 80/tcp //這是我們添加規則後的效果........

當我們的瀏覽器輸入伺服器位址後,看到Nginx的歡迎頁面,證明安裝成功!下面是成功返回的頁面:

 

CentOS 7學習筆記(二)之Nginx安裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.