1,701 瀏覽數

CentOS 7 + wordpress

CentOS 7 我採用的 web server 為 Nginx , Database 是 MariaDB .

在使用 WordPress 前你必須先確定你的系統軟體符合他的需求

  • PHP 版本需要大於 7 ,安裝 Nginx (Web server)
    1. Nginx (Web server)http://benjr.tw/95761
    2. Nginx (Web server) + PHP-FPMhttp://benjr.tw/95767
    3. PHP-mysql
      [root@localhost ~]$ yum install php-mysql
      
  • MySQL 版本需要大於 5.6 或是 MariaDB 10.0 以上的版本 ,安裝 Mariadb (DataBase)
    http://benjr.tw/95289
  • HTTPS support – 非必要,之後會再討論

Database

資料庫初始化設定.

[root@localhost ~]$ 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@localhost ~]$ 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@localhost ~]$ 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@localhost ~]$ cd ~
[root@localhost ~]$ wget http://wordpress.org/latest.tar.gz
[root@localhost ~]$ tar xzvf latest.tar.gz

還記得剛剛在 Databases 設定資料庫 wordpress 使用者帳號 wordpressuser 以及其該使用者密碼 password,這些資料都需要寫入到 wp-config.php 設定檔.

[root@localhost ~]$ cd ~/wordpress
[root@localhost wordpress]$ cp wp-config-sample.php wp-config.php
[root@localhost 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 預設的根目錄為 /usr/share/nginx/html/ 需要將剛剛整理好的文件複製到該處.如果裡面有檔案建議先刪除.

[root@localhost wordpress]$ ll /usr/share/nginx/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@localhost wordpress]$ rm /usr/share/nginx/html/index.html /usr/share/nginx/html/phpinfo.php 

或是修改 index 的內容 (wordpress 預設的檔案為 index.php)

[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
server {
    listen       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.php index.html index.htm;
    }
....
}
[root@localhost ~]# systemctl reload nginx
[root@localhost wordpress]$ rsync -avP ~/wordpress/ /usr/share/nginx/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_install01

wordpress_install02

wordpress_install03

WordPress 資料夾權限

預設的 wordpress 資料夾與檔案 使用者/群組 為 nobody / nogroup ,有人建議改成 nginx (CentOS 下的 Nginx 所使用的使用者) .

[root@localhost ~]$ 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;
    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@localhost ~]$ cat /etc/passwd | grep -i nobody
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
[root@localhost ~]$ cat /etc/group | grep -i nogroup
nogroup:x:65534:

在 /usr/share/nginx/html/目錄下的檔案其權限為 -rw-r–r– ,只有 Nobody 有權限可以寫入,但這個帳號沒有登入的權限.我們應該不用把 使用者/群組 改成 nginx .

不過上傳圖片的目錄 /usr/share/nginx/html/wp-content/uploads 是要改成 apache (不知道為什麼是 apache 而不是 nginx ,可以透過 #ps aux | egrep apache 來查查看),才能把圖片正確上傳.

[root@localhost ~]$ mkdir /usr/share/nginx/html/wp-content/uploads
[root@localhost ~]$ chown apache:apache /usr/share/nginx/html/wp-content/uploads

其他設定

其他設定可以參考如下

發表迴響