CentOS 7 安裝與設定 Nginx + MariaDB + PHP + phpMyAdmin(LEMP) - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

CentOS 7 安裝與設定 Nginx + MariaDB + PHP + phpMyAdmin(LEMP)

本篇文章將詳細解說如何在 CentOS 7 安裝與設定 Nginx、MariaDB 與 PHP(簡稱 LEMP)來架設 WEB 伺服器的環境,並使用 phpMyAdmin 這套資料庫管理工具,搭配 MariaDB 來管理資料庫。

CentOS

Nginx

安裝

CentOS 7.1 中,Nginx 並未被正式收錄在 yum,因此無法安裝,可自行加入 Nginx 官方提供的 CentOS 7 yum repository,即可直接安裝。

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

設定

Nginx 相關檔案位置:

  • 所有設定檔:/etc/nginx/
  • 主要設定檔:/etc/nginx/nginx.conf
  • 預設設定檔:/etc/nginx/conf.d/default.conf
  • 程序設定檔:/usr/sbin/nginx
  • log 檔:/var/log/nginx/

主要設定檔

Nginx 服務的主要設定檔。

vi /etc/nginx/nginx.conf
# 啟用 Nginx 的 Linux 帳戶
user  nginx;
# 啟用的執行緒數量(建議搭配 CPU 核心數 * 2)
worker_processes  1;
 
# log 檔位置
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    # 允許同一時間連線的數量
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    # 預設 log 記錄的格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    # access log 檔位置
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    # 是否啟用 gaip 壓縮(預設為註解,不啟用)
    #gzip  on;
 
    # 載入該路徑的所有設定檔,通常就是每個虛擬主機的配置
    include /etc/nginx/conf.d/*.conf;
 
    # 設定可上傳最大檔案容量(依需求設定)
    client_max_body_size 5m;
}

虛擬主機設定檔

vi /etc/nginx/conf.d/default.conf
server {
    # 這個主機的 Port
    listen       80;
    # 這個主機的名稱
    server_name  localhost;
 
    # 設定預設編碼,但通常都是由網頁 <meta> 來定義,因此預設註解
    #charset koi8-r;
    # 針對這個主機的 log 檔位置
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    # html 檔
    location / {
        # 網站的根目錄位置
        root   /usr/share/nginx/html;
        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
        index  index.html index.htm;
    }
 
    # 發生 404 指定導向哪個網頁
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    # php 檔
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #    include        fastcgi_params;
    #}  
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

服務設置

啟用服務並開機自動啟用。

systemctl start nginx
systemctl enable nginx

防火牆

設定 firewall 允許 http(80 Port)、https(443 Port)封包通行。

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

檢查 firewall 的設定。

firewall-cmd --zone=public --list-all
public (default, active)
  interfaces: eth0 eth1
  sources:
  services: dhcpv6-client http https ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

測試

打開瀏覽器輸入網址 http://Server IP,沒有問題就會看到 Nginx 的歡迎頁面。

問題

上傳檔案失敗

「擁有者」與「權限」均設定無誤,但仍無法上傳,這是因為沒有設定 SELinux 的 httpd_sys_content_t 這個文件類型,查看網站根目錄文件類型。

ll -dZ /usr/share/nginx/html
drwxrwxr-x. nginx nginx system_u:object_r:usr_t:s0       /usr/share/nginx/html

使用遞迴來讓所有的目錄加入 SELinux 的 httpd_sys_content_t 文件類型。

chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html

已正確設定 SELinux 的文件類型了。

ll -dZ /usr/share/nginx/html
drwxr-xr-x. nginx nginx unconfined_u:object_r:httpd_sys_rw_content_t:s0 /usr/share/nginx/html

PHP

安裝

安裝相關軟體:

  • php:PHP 程式語言
  • php-fpm:也就是 FastCGI,透過它來讓 Nginx 與 PHP 之間交互連動
yum install php php-fpm

PHP-FPM

設定

修改 php-fpm 的配置。

vi /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
 
;user = apache
user = nginx
 
;group = apache
group = nginx
 
; 預設帳戶、群組,為正在運作的帳戶
;listen.owner = nobody
;listen.group = nobody
listen.owner = nginx
listen.group = nginx
 
; 權限(預設為 0666)
;listen.mode = 0666
 
; session 的路徑
php_value[session.save_path] = /var/lib/php/session

修改 session 路徑的擁有者、群組為 nginx。

chown nginx:nginx /var/lib/php/session/

Nginx 與 PHP 連動設定

Nginx 針對 PHP 的設定,這樣才可透過 php-fpm 讓 Nginx 與 PHP 之間交互連動。

vi /etc/nginx/conf.d/default.conf
# ...(前面省略)...
 
    # html 檔
    location / {
        # 網站的根目錄位置
        root   /usr/share/nginx/html;
        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
        index  index.php index.html index.htm;
        # [須手動新增] 在瀏覽器呈現目錄樹為 on;反之 off,正試上線最好設成 off(預設值 off)
        autoindex on;
 
        try_files $uri $uri/ /index.php?$args;
    }
 
    # php 檔
    location ~ \.php$ {
        # 網站的根目錄位置
        root   /usr/share/nginx/html;
        # 要使用 FastCGI 解析的檔案位置
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
        fastcgi_index  index.php;
 
        try_files $uri =404;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include     fastcgi_params;
    }   
 
# ...(後面省略)...

WordPress 固定網址 404 排除

WordPress 使用固定網址設定,因此必須啟用伺服端的 Rewrite 功能,如下 14 和 18 行:

vi /etc/nginx/conf.d/default.conf
# ...(前面省略)...
 
    # html 檔
    location / {
        # 網站的根目錄位置
        root   /usr/share/nginx/html;
        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
        index  index.php index.html index.htm;
        # [須手動新增] 在瀏覽器呈現目錄樹為 on;反之 off,正試上線最好設成 off(預設值 off)
        autoindex on;
 
        try_files $uri $uri/ /index.php?$args;
    }
 
    # php 檔
    location ~ \.php$ {
        # 網站的根目錄位置
        root   /usr/share/nginx/html;
        # 要使用 FastCGI 解析的檔案位置
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
        fastcgi_index  index.php;
 
        try_files $uri =404;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include     fastcgi_params;
    }   
 
# ...(後面省略)...

修改網站根目錄的擁有者、群組為 nginx。

chown -R nginx:nginx /usr/share/nginx/html/

重啟 Nginx 服務。

systemctl restart nginx

服務設置

啟用 PHP-FPM 服務並開機自動啟用。

systemctl start php-fpm
systemctl enable php-fpm

測試

在網頁根目錄新增一個顯示 php 資訊的檔案。

vi /usr/share/nginx/html/index.php
<?php
phpinfo();

打開瀏覽器輸入網址 http://Server IP/index.php,沒有問題就會看到 php 資訊。

MariaDB

安裝

安裝相關軟體。

yum install mariadb mariadb-server

初始化設定

須先啟用服務,才可進行初始化。

systemctl start mariadb

進行初始化設定。

mysql_secure_installation
 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client:命令找不到
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):  # 輸入目前 MariaDB 的 root 密碼(第一次設定應該是空的,所以直接「ENTER」即可)
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n]    # 直接「ENTER」
New password:               # 設定新的密碼
Re-enter new password:      # 再次確認密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n]  # 是否要移除 anonymous user 的資料,直接「ENTER」
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n]  # 是否只允許讓 root 從 localhost 登入,無法從其他的網路登入,直接「ENTER」
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n]  # 是否移除 test 的資料庫,直接「ENTER」
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n]  # 是否要重新載入權限的 table 資訊,直接「ENTER」
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB!

服務設置

開機自動啟用服務。

systemctl enable mariadb

phpMyAdmin

安裝

目前 yum 套件資料庫中沒有 phpMyAdmin 的安裝資訊存在,因此須先更新 yum 套件資料庫。先到 EPEL 網頁,找到 The newest version of 'epel-release' for EL7,復製 epel-release-7-5.noarch 的連結,將連結貼至下述指令後並執行,完成後即可安裝軟體。

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install phpmyadmin

設定

設定 phpMyAdmin 連結到 Nginx 設置的網站根目錄下。

ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/html/

修改 phpMyAdmin 認證方式為 http(預設為 cookie),提高安全性。

vi /etc/phpMyAdmin/config.inc.php
 
$cfg['Servers'][$i]['auth_type']     = 'http';

服務設置

重啟 Nginx、PHP-FPM 與 MariaDB 服務。

systemctl restart nginx php-fpm mariadb

測試

打開瀏覽器輸入網址 http://192.168.0.205/phpMyAdmin/index.php,沒有問題就會看到 phpMyAdmin 登入頁面。

參考

在〈CentOS 7 安裝與設定 Nginx + MariaDB + PHP + phpMyAdmin(LEMP)〉中有 1 則留言

發表迴響