Ubuntu 16.04 我採用的 web server 為 Nginx , Database 是 MariaDB .
在使用 WordPress 前你必須先確定你的系統軟體符合他的需求
- PHP 版本需要大於 7 ,安裝 Nginx (Web server)
- MySQL 版本需要大於 5.6 或是 MariaDB 10.0 以上的版本 ,安裝 Mariadb (DataBase)
- HTTPS support – 非必要,之後會再討論
Nginx
Nginx (音同 Engine X ,Web 伺服器) , 如果使用 Apache2 請參考 https://benjr.tw/95861
root@ubuntu:~# apt-get update
root@ubuntu:~# apt-get upgrade
root@ubuntu:~# apt-get install nginx Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: nginx-common nginx-core Suggested packages: fcgiwrap nginx-doc The following NEW packages will be installed: nginx nginx-common nginx-core 0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded. Need to get 458 kB of archives. After this operation, 1,482 kB of additional disk space will be used. Do you want to continue? [Y/n] y ...
root@ubuntu:~# systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2017-02-06 01:08:19 PST; 4min 19s ago Main PID: 23282 (nginx) CGroup: /system.slice/nginx.service ├─23282 nginx: master process /usr/sbin/nginx -g daemon on; master_process on └─23283 nginx: worker process
透過curl 文字瀏覽器 http://yourIP 正確會看到如下的文字畫面:
root@ubuntu:~# curl 172.16.15.208 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...
PHP-FPM
使用 Nginx 來架設 Web (HTTP) 伺服器,但 nginx 並不認識 PHP ,這時候開啟 PHP 檔案 Nginx 會把它當作一般檔案來下載,所以需要透過另外一個服務 PHP-FPM (FastCGI Process Manager) 將所有 PHP 導向給他.
root@ubuntu:~# apt-get install php-fpm Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: php-common php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline Suggested packages: php-pear The following NEW packages will be installed: php-common php-fpm php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline 0 upgraded, 8 newly installed, 0 to remove and 5 not upgraded. Need to get 3,524 kB of archives. After this operation, 14.1 MB of additional disk space will be used. Do you want to continue? [Y/n] y ...
root@ubuntu:~# apt-get install php-mysql Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: php7.0-mysql The following NEW packages will be installed: php-mysql php7.0-mysql 0 upgraded, 2 newly installed, 0 to remove and 14 not upgraded. Need to get 125 kB of archives. After this operation, 484 kB of additional disk space will be used. Do you want to continue? [Y/n] y ...
root@ubuntu:~# systemctl status php7.0-fpm ● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2017-02-06 01:10:50 PST; 1min 31s ago Main PID: 29969 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /system.slice/php7.0-fpm.service ├─29969 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─29972 php-fpm: pool www └─29973 php-fpm: pool www
預設的 nginx 並不會把 PHP 導向給 php-fpm ,下面是 nginx 的原始 default 設定檔.
root@ubuntu:~# cat /etc/nginx/sites-enabled/default server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }
主要新增 location ~ \.php$ 內容 與 index 加入 wordpress 預設的 index.php (沒有這一項網頁會顯示 403 forbidden).
root@ubuntu:~# vi /etc/nginx/sites-enabled/default server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } }
檢查設定檔並且重新啟動 Nginx.
root@ubuntu:~# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
root@ubuntu:~# systemctl reload nginx
root@ubuntu:~# systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2017-02-06 01:08:19 PST; 27min ago Process: 30172 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, s Main PID: 23282 (nginx) CGroup: /system.slice/nginx.service ├─23282 nginx: master process /usr/sbin/nginx -g daemon on; master_process on └─30176 nginx: worker process
試一下 PHP 功能運作是否正常.用編輯器來鍵入下面的內容.
root@ubuntu:~# vi /var/www/html/phpinfo.php <?php phpinfo(); ?>
透過curl 文字瀏覽器 http://yourIP/phpinfo.php 正確會看到如下的文字畫面:
root@ubuntu:~# curl http://172.16.15.208/phpinfo.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <style type="text/css"> ....
如果出現 File not found. 檢查一下 /var/log/nginx/error.log
Database
因為 WordPress 會將資料儲存在 database 中,我們用 MariaDB 當成我們的資料庫系統.
root@ubuntu:~# apt-get install mariadb-server Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient20 libreadline5 libterm-readkey-perl mariadb-client-10.0 mariadb-client-core-10.0 mariadb-common mariadb-server-10.0 mariadb-server-core-10.0 mysql-common Suggested packages: libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx mariadb-test tinyca The following NEW packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient20 libreadline5 libterm-readkey-perl mariadb-client-10.0 mariadb-client-core-10.0 mariadb-common mariadb-server mariadb-server-10.0 mariadb-server-core-10.0 mysql-common 0 upgraded, 14 newly installed, 0 to remove and 5 not upgraded. Need to get 16.0 MB of archives. After this operation, 145 MB of additional disk space will be used. Do you want to continue? [Y/n] y ...
確定一下 MariaDB 執行中.
root@ubuntu:~# systemctl status mysql ● mysql.service - LSB: Start and stop the mysql database server daemon Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled) Active: active (running) since Mon 2017-02-06 00:42:03 PST; 1h 10min ago Docs: man:systemd-sysv-generator(8) CGroup: /system.slice/mysql.service ├─22657 /bin/bash /usr/bin/mysqld_safe ├─22801 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysq └─22802 logger -t mysqld -p daemon error
資料庫初始化設定.
root@ubuntu:~# mysql_secure_installation 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): 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] y 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] y ... 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] y ... 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] y - 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] y ... 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!
剛剛就已經設定了 MariaDB root 密碼,接下來我們要為將來使用的 wordpress 建立一個資料庫,方法很簡單只要下面幾個步驟,建立一個資料庫名稱為 wordpress 以及使用者帳號 wordpressuser 以及其該使用者密碼 password,你可以依據自己的喜好來命名.
root@ubuntu:~# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 50 Server version: 10.0.29-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
這邊就是要建立 wordpress 專用的資料庫,create database 資料庫名稱你可以依據自己的需求做改變,注意每個指令後面都要加上 “;”
MariaDB [(none)]> CREATE DATABASE wordpress; Query OK, 1 row affected (0.00 sec)
建立 wordpressuser 這個帳號.
MariaDB [(none)]> CREATE USER wordpressuser@localhost IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.01 sec)
- CREATE USER wordpressuser@localhost
username@localhost , username – 新增的使用者, localhost – 限制可從哪裡來存取, 可用 ‘%’ (代表全部) - IDENTIFIED BY ‘password’
password: 使用者密碼
將剛剛建立的 wordpress 資料庫權限給 wordpressuser.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost; Query OK, 0 rows affected (0.00 sec)
- GRANT ALL PRIVILEGES
授權的權限 (ALL: SELECT, INSERT, DELETE …. etc) - wordpress.*
指定可以存取哪些 Db_name/Table - wordpressuser@localhost
username@localhost , username – 新增的使用者, localhost – 限制可從哪裡來存取, 可用 ‘%’ (代表全部)
MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> exit; Bye
試一下剛剛建立好的 wordpress 資料庫和使用者吧!!這邊要輸入的密碼是 wordpressuser 的密碼,我剛剛設定的是 password.
root@ubuntu:~# mysql -u wordpressuser -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 51 Server version: 10.0.29-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
連線到 wordpress 資料庫看一下.如果你可以用 wordpressuser 連線到 wordpress 資料庫,那代表資料庫的連結沒有問題.可以開始進入安裝 wordpress 了.跳出來吧!!以後再也用不到了 SQL 的指令了.
MariaDB [(none)]> connect wordpress; Connection id: 52 Current database: wordpress MariaDB [wordpress]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | wordpress | +--------------------+ 2 rows in set (0.00 sec)
MariaDB [wordpress]> exit; Bye
WordPress
直接下載最新版本的 WordPress 來使用.
root@ubuntu:~# cd ~
root@ubuntu:~# wget http://wordpress.org/latest.tar.gz
root@ubuntu:~# tar xzvf latest.tar.gz
還記得剛剛在 Databases 設定資料庫 wordpress 使用者帳號 wordpressuser 以及其該使用者密碼 password,這些資料都需要寫入到 wp-config.php 設定檔.
root@ubuntu:~# cd ~/wordpress
root@ubuntu:~/wordpress# cp wp-config-sample.php wp-config.php
root@ubuntu:~/wordpress# vi wp-config.php // ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpressuser'); /** MySQL database password */ define('DB_PASSWORD', 'password');
Nginx 預設的根目錄為 /var/www/html/ 需要將剛剛整理好的文件複製到該處.如果裡面有檔案建議先刪除.
root@ubuntu:~/wordpress# ll /var/www/html total 24 drwxr-xr-x 2 root root 4096 Oct 23 04:43 ./ drwxr-xr-x 3 root root 4096 Oct 23 04:42 ../ -rw-r--r-- 1 root root 11510 Oct 23 04:42 index.html -rw-r--r-- 1 root root 20 Oct 23 04:43 phpinfo.php root@ubuntu:~/wordpress# rm /var/www/html/index.html /var/www/html/phpinfo.php
root@ubuntu:~/wordpress# rsync -avP ~/wordpress/ /var/www/html/ sending incremental file list wp-config.php 2,839 100% 0.00kB/s 0:00:00 (xfr#1, ir-chk=1016/1025) sent 40,328 bytes received 169 bytes 80,994.00 bytes/sec total size is 22,582,692 speedup is 557.64
在瀏覽器輸入你的網域或是 IP ( http://Domain or IP/wp-config.php ),完成下面幾個步驟即將大功告成.
輸入你的 Site Title , Username , Password , Your Email
WordPress 資料夾權限
預設的 wordpress 資料夾與檔案 使用者/群組 為 nobody / nogroup ,有人建議改成 www-data (Ubuntu 下的 Nginx 所使用的使用者) .
root@ubuntu:/var/www/html# ps aux | egrep nginx root 925 0.0 0.0 125104 4 ? Ss 18:19 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 930 0.0 0.2 125696 2768 ? S 18:19 0:00 nginx: worker process root 4630 0.0 0.0 14224 908 pts/18 S+ 19:47 0:00 grep -E --color=auto nginx
來看看 nobody 與 nogroup
root@ubuntu:~# cat /etc/passwd | grep -i nobody nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
root@ubuntu:~# cat /etc/group | grep -i nogroup nogroup:x:65534:
在 /var/www/html 目錄下的檔案其權限為 -rw-r–r– ,只有 Nobody 有權限可以寫入,但這個帳號沒有登入的權限.我們應該不用把 使用者/群組 改成 www-data .
不過上傳圖片的目錄 /var/www/html/wp-content/uploads 是要改成 www-data ,才能把圖片正確上傳.
root@ubuntu:~# mkdir /var/www/html/wp-content/uploads
root@ubuntu:~# chown www-data:www-data /var/www/html/wp-content/uploads
詳細 WordPress 檔案權限設定,請參考 https://benjr.tw/10912
其他設定
其他設定可以參考如下
- 連線型態 FTP FTPS (SSL) SSH2 – https://benjr.tw/10928
- 固定網址設定 – https://benjr.tw/10918
- Linux – Virtual Host – https://benjr.tw/11082
- 網域 DNS 設定 – https://benjr.tw/13311
- Ubuntu 防火牆 ufw – https://benjr.tw/93733