<span class="begin">在</span>開始前,可以先準(zhǔn)備好用于搭建個人博客的服務(wù)器和域名,或者使用虛擬機練習(xí)搭建。服務(wù)器選擇1cpu 1g內(nèi)存 1M帶寬的最低配置即可,后續(xù)若有需要可再升級。購買服務(wù)器時請查看平臺當(dāng)前正在進(jìn)行的活動,通常能以非常低廉的價格購買。系統(tǒng)鏡像這里推薦ubuntu,安裝軟件相對比較簡單,centos則需要一定的門檻。另外,選擇國內(nèi)的服務(wù)器搭建網(wǎng)站是需要備案的,如不過不打算備案可選擇香港或海外的服務(wù)器。
我的博客地址: 藍(lán)月楓 Aitsuki
MySql
sudo apt install mysql-server
sudo mysql
# 創(chuàng)建wordpress數(shù)據(jù)庫、賬戶、并授權(quán)。
create database wordpress;
create user username identified with 'password';
grant all privileges on wordpress.* to username;
flush privileges;
exit
將上面的username和password替換成你想要的用戶名和密碼,記住這個數(shù)據(jù)庫和賬戶,之后需要寫入到wordpress配置中。
Nginx
sudo apt install nginx
此時用瀏覽器你的網(wǎng)站就可以看到Nginx的默認(rèn)首頁了。
配置Gzip:https://kinsta.com/blog/enable-gzip-compression/
sudo vim /etc/nginx/nginx.conf
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 或者使用 sudo service nginx reload
sudo nginx -s reload
PHP
sudo apt install php-fpm
# 安裝擴展,這些擴展都是wordpress必備或推薦的。
sudo apt install php-mysql php-gd php-curl php-dom php-mbstring php-imagick php-zip php-gmp php-bcmath
# 7.4是目前最新版本,請?zhí)鎿Q成你安裝的版本,盡量使用7.2以上的。
sudo vim /etc/php/7.4/fpm/php.ini
修改php配置:
# Nginx + PHP CGI的一個可能的安全漏洞,https://www.laruence.com/2010/05/20/1495.html
cgi.fix_pathinfo = 0
# 配置文件上傳大小限制(上傳媒體庫,插件時)
upload_max_filesize = 128M
post_max_size = 128M
# 重啟fpm讓配置生效
sudo service php7.4-fpm restart
phpMyAdmin
phpMyAdamin是數(shù)據(jù)庫管理工具,某些情況下非常有用。
# 下載phpMyAdamin
wget https://files.phpmyadmin.net/phpMyAdmin/4.9.7/phpMyAdmin-4.9.7-all-languages.tar.gz
# 解壓
tar -xf phpMyAdmin-4.9.7-all-languages.tar.gz
# 重命名
mv phpMyAdmin-4.9.7-all-languages phpMyAdmin
# 移動到html目錄
sudo mv phpMyAdmin /var/www/html
# 修改目錄歸屬
sudo chown -R www-data:www-data /var/www/html/phpMyAdmin/
配置phpMyAdmin
cd /var/www/html/phpMyAdmin
sudo cp config.sample.inc.php config.inc.php
sudo vim config.inc.php
// 在這里輸入32位以內(nèi)的密碼,用于cookie加密,增加安全性。不用記住,盡量設(shè)置長點。
$cfg['blowfish_secret'] = '請隨意輸入32位以內(nèi)密碼';
配置phpMyAdmin的Nginx服務(wù)
# 進(jìn)入sites-available目錄
cd /etc/nginx/sites-available/
# 開始為phpMyAdmin配置Nginx服務(wù)
sudo vim phpMyAdmin
server {
# 輸入你希望的端口
listen 8100 default_server;
listen [::]:8100 default_server;
root /var/www/html/phpMyAdmin;
index index.php;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
讓配置生效
# sites-available 目錄保存我們可用的服務(wù)配置,而sites-enabled目錄則相當(dāng)于一個開關(guān)。
# 將我們想開啟的服務(wù)軟鏈接到sites-enabled目錄即可
sudo ln -s /etc/nginx/sites-available/phpMyAdmin /etc/nginx/sites-enabled/phpMyAdmin
# 檢查配置(每次修改配置后都推薦此命令檢查)
sudo nginx -t
sudo nginx -s reload
現(xiàn)在登陸網(wǎng)站的8100端口(記得在服務(wù)器的安全組中放開對應(yīng)端口)即可看到phpMyadmin的登陸界面,登陸時的賬號密碼就是前面MySql命令新建的。
phpMyAdmin 高級功能(可選)
另外phpMyAdmin首頁底部可能會提示“phpMyAdmin 高級功能尚未完全設(shè)置,部分功能未激活”,不知道所謂的高級功能究竟是什么,如果感興趣的話可以嘗試開啟:
sudo mysql
# 執(zhí)行sql:創(chuàng)建了一個phpmyadmin數(shù)據(jù)庫并創(chuàng)建若干張表
source /var/www/html/phpMyAdmin/sql/create_tabls.sql;
# 創(chuàng)建phpmyadmin數(shù)據(jù)庫的管理賬戶,將pma和pmapass替換成你需要的
create user pma identified by 'pmapass';
grant all privileges on phpmyadmin.* to pma;
flush privileges;
exit
配置高級功能:
sudo vim /var/www/html/phpMyAdmin/config.inc.php
/* User used to manipulate with storage */
// $cfg['Servers'][$i]['controlhost'] = '';
// $cfg['Servers'][$i]['controlport'] = '';
# 這里輸入剛剛創(chuàng)建的用戶名和密碼
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = 'pmapass';
/* Storage database and tables */
# 放開下面注釋
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
$cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
$cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
$cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';
重新登陸phpmyadmin即可。
Wordpress
# 下載wordpress中文版
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
# 解壓
tar -xf latest-zh_CN.tar.gz
# 移動到html目錄
sudo mv wordpress /var/www/html/
# 更改wordpress的owner和group,與nginx相同(www-data)
# 這是為了避免日后出現(xiàn)各種各樣的權(quán)限問題
sudo chown -R www-data:www-data /var/www/html/wordpress
配置wordpress的Nginx服務(wù)
cd /etc/nginx/sites-available/
sudo vim wordpress
# https://www.digitalocean.com/community/tutorials/how-to-implement-browser-caching-with-nginx-s-header-module-on-ubuntu-16-04
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
# 增加上傳文件限制大小
client_max_body_size 128m;
client_body_buffer_size 128m;
# 緩存頭
expires $expires;
root /var/www/html/wordpress;
index index.php;
# 輸入你的域名
server_name yourdomain.com www.yourdomain.com;
location / {
# 偽靜態(tài)
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
sudo nginx -t
sudo nginx -s reload
現(xiàn)在只需要登錄你的網(wǎng)站,即可根據(jù)wordpress向?qū)瓿珊罄m(xù)操作。
配置SSL證書(可選)
免費證書可以到 https://freessl.cn/ 這個網(wǎng)站申請,最終你能獲得名為xxx.crt, xxx.key 的證書文件和密鑰。
將證書和密鑰上傳到服務(wù)器,并放置在/etc/nginx/ssl
目錄,目錄本身不存在需要創(chuàng)建。也可以放到/etc/ssl
目錄,或者其他目錄,沒有規(guī)定。
配置ssl
sudo vim /etc/nginx/sites-available/wordpress
添加以下規(guī)則:(原來的80端口改成443端口,然后重新聲明一個80端口的服務(wù)重定向到443端口的服務(wù))
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 http2;
ssl_certificate /etc/nginx/ssl/xxx.crt;
ssl_certificate_key /etc/nginx/ssl/xxx.key;
add_header Strict-Transport-Security "max-age=31536000";
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5';
...
}
sudo nginx -t
sudo nginx -s reload
現(xiàn)在證書已部署,訪問主頁可能出現(xiàn)問題,需要到wordpress后臺的設(shè)置-常規(guī)
配置wordpress地址和站點地址的前綴為https。
文件權(quán)限的注意事項
wordpress是部署Nginx上的,用戶組是www-data。所以前面教程中就有了更改wordpress目錄用戶組的操作 sudo chown -R www-data:www-data wordpress
,否則wordpress后臺會出現(xiàn)很多權(quán)限導(dǎo)致的問題。
另外有的做法是將wordpress的讀寫權(quán)限放開給所有用戶chmod 777
,本質(zhì)上和修改用戶組沒什么區(qū)別,但是前者更安全。
推薦插件
- WP Mail SMTP by WPForms,郵件服務(wù)。
- UpdraftPlus,備份。推薦只備份數(shù)據(jù)庫和上傳目錄。
- WP-Optimize – Clean, Compress, Cache.
- WP Statistics,統(tǒng)計插件,可配合Sakura主題使用。
- WP Githuber MD,markdown編輯器。
- Wordfence Security,安全插件。
- Akismet Anti-Spam,垃圾評論過濾。
- AMP,生成AMP 頁面,有利于提升 Google 排名。
- Rank Math SEO,搜索引擎優(yōu)化。