Hướng dẫn Cài WordPress tối ưu trên CentminMod

Hướng dẫn Cài WordPress tối ưu trên CentminMod

1. Thêm domain vào VPS

Tại cửa sổ Putty, các bạn gõ:

cd /usr/local/src/centmin-v1.2.3mod
./centmin.sh

Chọn 2 để add domain. nó sẽ yêu cầu bạn điền domain ( ko có www.). Ok! bạn cứ điền bình thường.

Bây giờ bạn chỉ việc upload code vào : /home/nginx/domains/domain.com/public

Chạy lệnh

chown -R nginx:nginx /home/nginx/domains/domain.com/public/

Mục đích là đưa thư mục đó về trạng thái có thể ghi dữ liệu vào.

2. Tạo User và Database

Tạo Database

mysqladmin -u root -p create [Tên DB]

Sẽ có thông báo yêu cầu nhập pass. pass này chính là pass root của MySql bạn nhập lúc cai đặt Centmin. Bạn làm theo các lệnh dưới đây nhé.

Truy cập mysql để tạo database user

mysql -u root -p
MariaDB [(none)]> CREATE USER '[tên user]'@'localhost' IDENTIFIED BY '[pass]';
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON [tên DB].* TO '[Tên user]'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
Exit

3. Cài WordPress + WP-FFPC Plugin + ngx_pagespeed

Cài WordPress

cd /home/nginx/domains/yourdomain.com/public/
wget http://wordpress.org/latest.zip
unzip latest.zip

Sau đó bạn cứ truy cập vào website và cài đặt WordPress như bình thường.

Tiếp theo mình khuyên các bạn nên dùng WinSCP để đăng nhập vào sửa file cho dễ sử dụng vì phải edit khá nhiều file.

Sau đó cài thêm WP-FFPC Plugin:  Đây là plugin tự động cài cache cho WordPress như PHP Memcached, X-Cache, …

Từ màn hình WinSCP, các bạn truy cập vào /usr/local/lib/php.ini

Edit :

disable_functions = exec,passthru,shell_exec,system,proc_open,popen

Thành :

disable_functions = passthru,shell_exec,system,popen

Restart PHP:

service php-fpm restart

Cài plugin WP FFPC

Bật WordPress cache. Mở file config.php, thêm dòng này vào sau /* That’s all, stop editing! Happy blogging. */

define('WP_CACHE',true);

Kích hoạt plugin lên và chỉnh như sau:

Save lại.

Dùng WinSCP, mở /usr/local/nginx/conf/wpffpc.conf

Tìm đoạn sau :

if ( $memcached_request = 0) {
rewrite ^ /index.php last;
}
}

## rewrite rules
location @rewrites {
add_header X-Cache-Engine "";
rewrite ^ /index.php last;
}

Sửa index.php thành index.php?q=$request_uri.

Nếu cài wordpress vào thư mục con thì sửa thành thư_mục_con/index.php?q=$request_uri.

Mở /usr/local/nginx/conf/nginx.conf

Thêm code sau vào sau http {

# memcached servers, generated according to wp-ffpc config
upstream memcached-servers {
server 127.0.0.1:11211;

Save lại.

Tạo file wpsecure.conf trong thư mục /usr/local/nginx/conf/ với nội dung:

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}

# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_
{
return 444;
}

#nocgi
location ~* \.(pl|cgi|py|sh|lua)\$ {
return 444;
}

#disallow
location ~* (roundcube|webdav|smtp|http\:|soap|w00tw00t) {
return 444;

location ~ /(\.|wp-config.php|readme.html|license.txt) { deny all; }
}

Tạo file wpnocache.conf trong thư mục /usr/local/nginx/conf/ với nội dung:

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

Tiếp tục, edit file vHost của domain tại /usr/local/nginx/conf/conf.d/domain.com.conf

Với bạn sử dụng wordpress trực tiếp, không qua thư mục con.

Chèn đoạn code sau lên trên dòng location / {

include /usr/local/nginx/conf/wpsecure.conf;
include /usr/local/nginx/conf/wpnocache.conf;

Chèn code sau xuống dưới dòng location / {

try_files $uri $uri/ /index.php?$args @memcached;
if (!-f $request_filename){
set $rule_1 1$rule_1;
}
if (!-d $request_filename){
set $rule_1 2$rule_1;
}
if ($rule_1 = "21"){
rewrite /. /index.php last;
}

Với bạn sử dụng wordpess qua thư mục con.

Chèn code sau xuống dưới dòng location / {

include /usr/local/nginx/conf/wpsecure.conf;
include /usr/local/nginx/conf/wpnocache.conf;
try_files $uri $uri/ /thư_mục_con/index.php?$args @memcached;
if (!-f $request_filename){
set $rule_1 1$rule_1;
}
if (!-d $request_filename){
set $rule_1 2$rule_1;
}
if ($rule_1 = "21"){
rewrite /. /thư_mục_con/index.php last;
}

Sửa location / { thành location /thư_mục_con {

Kéo lui xuống dưới 1 chút, bạn sẽ thấy 1 đoạn như sau:

include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;

Sửa lại thành:

include /usr/local/nginx/conf/wpffpc.conf;
include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
#include /usr/local/nginx/conf/phpstatus.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;

Sửa file cuối cùng 😀 /usr/local/nginx/conf/staticfiles.conf

Tìm :

location ~* \.(html|htm|txt)$ {

Sửa thành: location ~* \.(htm|txt)$ { Restart Nginx và php-fpm: service nginx restart service php-fpm restart

4. Bật ngx_pagespeed module.

Mặc định, CentminMod đã config pagespeed tương đối chuẩn cho website. Các bạn chỉ việc bật lên và dùng thôi.

Sửa file vHost của domain tại /usr/local/nginx/conf/conf.d/domain.com.conf

Tìm:

# ngx_pagespeed & ngx_pagespeed handler
#include /usr/local/nginx/conf/pagespeed.conf;
#include /usr/local/nginx/conf/pagespeedhandler.conf;
#include /usr/local/nginx/conf/pagespeedstatslog.conf;

Sửa thành :

# ngx_pagespeed & ngx_pagespeed handler
include /usr/local/nginx/conf/pagespeed.conf;
include /usr/local/nginx/conf/pagespeedhandler.conf;
include /usr/local/nginx/conf/pagespeedstatslog.conf;

Bật Pagespeed:

pscontrol on

Kiểm tra Pagespeed đã chạy chưa:

curl -I http://yourdomain.com/

5. Bật PageSpeed Console

Bật MessageBufferSize.

pscontrol edit

Tìm:

#######################################################
# Set it to 0 if you want to disable this feature.
# pagespeed MessageBufferSize 100000;

Xóa dấu # ở ngay dòng pagespeed MessageBufferSize 100000;, bạn sẽ có:

#######################################################
# Set it to 0 if you want to disable this feature.
pagespeed MessageBufferSize 100000;

Bật PageSpeed statistics

/Để bật/
pscontrol statson

/Để tắt/
pscontrol statsoff

Thêm IP được phép truy cập PageSpeed Console

pscontrol handler

Ví dụ, IP của mình là 192.168.1.2 thì sửa thành như sau:

location /ngx_pagespeed_statistics { allow 127.0.0.1; allow 192.168.1.2; deny all; }
location /ngx_pagespeed_message { allow 127.0.0.1; allow 192.168.1.2; deny all; }
location /pagespeed_console { allow 127.0.0.1; allow 192.168.1.2; deny all; }

Như vậy là chỉ IP 192.168.1.2 là được phép truy cập PageSpeed Console xem thông kê, còn lại đều bị chặn.

Số liệu thống kê điều khiển PageSpeed ​​sẽ được nhập vào /var/log/pagespeed trong một thư mục gọi là stats_log_global . Vì vậy bạn phải tạo thư mục stats_log_global và chmod cho phù hợp.

cd /var/log/pagespeed/
mkdir stats_log_global
chown nginx:nginx /var/log/pagespeed/stats_log_global
service nginx restart

Bây giờ, bạn xem thành quả của mình nhé :

domain.com/pagespeed_console
domain.com/ngx_pagespeed_statistics
domain.com/ngx_pagespeed_message

Vậy là xong, trong bài mình đã rewrite url của wordpress sẵn với định dạng : %category%/%postname%.html

các bạn chỉ việc vào Settings ->Permalinks, chọn Custom Structure và dán đoạn cấu trúc trên vào là xong thôi.

Be the first to comment

Leave a Reply

Your email address will not be published.


*