iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 12
0
自我挑戰組

30天學 LAMP 安裝系列 第 12

Day12 CentOS 7 安裝 Nginx

  • 分享至 

  • xImage
  •  

前言

由於 Nginx 也是 Linux 界的主流網頁伺服器之一,因此除了 Apache 外,Nginx 也來試裝看看。

參考文件

預先準備動作

  • 安裝 yum-utils 套件,等會需要用到 yum-config-manager 指令,這在安裝 MySQL 時就已經安裝過了。

開始安裝 Nginx

  1. 建立 Nginx 使用的 yum repository,使用文字編輯器,於 /etc/yum.repos.d/ 路徑下,建立檔案 nginx.repo

    $ sudo vi /etc/yum.repos.d/nginx.repo
    

    內容如下:

    [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key

    [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key

    內容預設啟用 Nginx stable 穩定版本,較適合正式上線的系統,如果要使用 Nginx 的 mainline 版,則將 [nginx-stable] 區段調整為 enabled=0、[nginx-mainline] 區段調整為 enabled=1。

  2. 安裝 nginx 套件

    [doraemon@centos7-cli yum.repos.d]$ sudo yum install nginx -y
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: ftp.ksu.edu.tw
     * extras: ftp.ksu.edu.tw
     * updates: ftp.ksu.edu.tw
    Resolving Dependencies
    --> Running transaction check
    ---> Package nginx.x86_64 1:1.16.1-1.el7.ngx will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ========================================================================================================
     Package            Arch                Version                         Repository                 Size
    ========================================================================================================
    Installing:
     nginx              x86_64              1:1.16.1-1.el7.ngx              nginx-stable              766 k
    
    Transaction Summary
    ========================================================================================================
    Install  1 Package
    
    Total download size: 766 k
    Installed size: 2.7 M
    Downloading packages:
    warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.16.1-1.el7.ngx.x86_64.rpm: Header V4 RSA/                                    SHA1 Signature, key ID 7bd9bf62: NOKEY
    Public key for nginx-1.16.1-1.el7.ngx.x86_64.rpm is not installed
    nginx-1.16.1-1.el7.ngx.x86_64.rpm                                                | 766 kB  00:00:03
    Retrieving key from https://nginx.org/keys/nginx_signing.key
    Importing GPG key 0x7BD9BF62:
     Userid     : "nginx signing key <signing-key@nginx.com>"
     Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
     From       : https://nginx.org/keys/nginx_signing.key
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : 1:nginx-1.16.1-1.el7.ngx.x86_64                                                      1/1
    ----------------------------------------------------------------------
    
    Thanks for using nginx!
    
    Please find the official documentation for nginx here:
    * http://nginx.org/en/docs/
    
    Please subscribe to nginx-announce mailing list to get
    the most important news about nginx:
    * http://nginx.org/en/support.html
    
    Commercial subscriptions for nginx are available on:
    * http://nginx.com/products/
    
    ----------------------------------------------------------------------
      Verifying  : 1:nginx-1.16.1-1.el7.ngx.x86_64                                                      1/1
    
    Installed:
      nginx.x86_64 1:1.16.1-1.el7.ngx
    
    Complete!
    
    
  3. 啟動 nginx 服務

    $ sudo systemctl start nginx
    
  4. 啟用 httpd 服務,讓 httpd 服務每次開機就會自動被帶起來

    $ sudo systemctl enable nginx
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
  5. 檢視 httpd 服務啟動狀態

    $ sudo systemctl status nginx
    ● nginx.service - nginx - high performance web server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: active (running) since Thu 2019-09-12 11:27:03 CST; 2min 21s ago
         Docs: http://nginx.org/en/docs/
     Main PID: 5568 (nginx)
       CGroup: /system.slice/nginx.service
               ├─5568 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
               └─5569 nginx: worker process
    
    Sep 12 11:27:03 centos7-cli.lab.example.com systemd[1]: Starting nginx - high performance web server...
    Sep 12 11:27:03 centos7-cli.lab.example.com systemd[1]: Started nginx - high performance web server.
    
  6. 開通本機防火牆

    $ sudo firewall-cmd --permanent --add-service=http
    success
    $ sudo firewall-cmd --reload
    success
    
  7. 在其他台電腦開啟網頁測試 OK。

    https://ithelp.ithome.com.tw/upload/images/20190912/20119707HoTCDXOpjU.png


上一篇
Day11 CentOS 7 安裝 Apache
下一篇
Day13 CentOS 7 安裝 PHP
系列文
30天學 LAMP 安裝33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言