本文只是我對自己安裝WordPress過程的一個總結,可能并不準確,歡迎指出。愛掏網 - it200.com我使用的是 Nginx、MySQL和php-fpm。愛掏網 - it200.com如果你用的是 Apache2 與 phpMyAdmin/MariaDB 的話,這篇文章可能幫不到你。愛掏網 - it200.com
如果你嫌麻煩,可以直接用WordPress.com的免費方案,就是可能會有廣告。愛掏網 - it200.com
吐嘈:
- Google到的很多教程(比如wpbeginner.com與freecodecamp.org)都推薦用Bluehost/HostGator/SiteGround/WP Engine/Hostinger等解決方案……但我沒法用啊……
- 網上的很多教程在如何設置 PHP 的部分過于模糊了……導致我折騰了很久。愛掏網 - it200.com
如果你想要更詳細的教程,推薦這個:How to Install WordPress with Nginx on Ubuntu 22.04 LTS - LinuxCapable(但是注意該文中提到的PHP 8.1 可能會產生錯誤,最好使用PHP 8.0)
WordPress官方也有 安裝指南 ,不過說得不是很詳細,需要借助官方論壇或者Google。愛掏網 - it200.com
TODO
如果早知道……
WordPress
- WordPress 是一個免費開源的引擎(FOSS),其官網是 WordPress.org
- WordPress.com 是一個商業的 WordPress-hosting 平臺。愛掏網 - it200.com我以前只知道這個,導致我一直以為 WordPress 是個商業軟件,一直不想用它……后悔為什么不早知道。愛掏網 - it200.com
排雷
- 不能用PHP 8.1,需要用PHP 8.0或更低的PHP 7.4(參考1)。愛掏網 - it200.com
- WordPress除了
php
以外,還需要php-fpm
或php-cgi
——不能只安裝php
!
可能的報錯(及其解決方案)
這里總結了我在安裝過程中出現的報錯信息以及我是如何解決它們的。愛掏網 - it200.com
502
如果是 502 ,說明你 php-fpm
沒有安裝,或沒有設置好 nginx 的配置文件。愛掏網 - it200.com
Error establishing a database connection
如果你建了數據庫,且 wp-config.php
中的參數正確,那么這應該是因為你沒有給你的用戶足夠的權限。愛掏網 - it200.com
如果你嫌麻煩,可以直接使用
GRANT ALL PRIVILEGES ON * . * TO wordpress@localhost;
來給予wordpress@localhost
所有數據庫的權限。愛掏網 - it200.com
白屏
如果你在瀏覽器中訪問時出現了白屏(“white screen of death”),說明你的 PHP 版本不對——不能用最新的 PHP 8.1 。愛掏網 - it200.com你可以在wp-config.php
中設置
error_reporting(E_ALL); ini_set(display_errors, 1);define( WP_DEBUG_LOG, true );define( WP_DEBUG_DISPLAY, false );
來輸出信息。愛掏網 - it200.com
安裝依賴
sudo apt-get update
Nginx
sudo apt-get install nginx
MySQL
參考:1, 2.
sudo apt-get install mysql-client-core-8.0 mysql-server-core-8.0 mysql-server mysql-client --fix-missingsudo service mysql start
PHP
sudo add-apt-repository ppa:ondrej/phpsudo apt-get updatesudo apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl php7.4-cgi
我使用的是 PHP7.4,如果你安裝了 PHP 8.0 的話,需要用sudo apt-get purge php8.*
或者sudo update-alternatives --config php
來更改PHP版本。愛掏網 - it200.com(參考1)
DNS
如果你用的是騰訊云,在 DNSPod 里加條解析即可。愛掏網 - it200.com
開始
訪問WordPress.org的官網,在遠程服務器上下載并解壓最新版WordPress:
wget https://wordpress.org/latest.zipunzip latest.zip
用MySQL建數據庫
此部分對應官方指南的 Step 2: Create the Database and a User 以及 Creating Database for WordPress – Using the MySQL Client 。愛掏網 - it200.com
sudo mysql -u adminusername -p
運行(數據庫和用戶的名字任意,這里都用了wordpress):
注:直接按照指南運行代碼會報錯,需要參考 這個 改一下。愛掏網 - it200.com
CREATE DATABASE wordpress;CREATE USER wordpress@localhost IDENTIFIED BY password;GRANT ALL PRIVILEGES ON * . * TO wordpress@localhost;FLUSH PRIVILEGES;EXIT;
把password
替換掉,記住它,之后要填在wp-config.php
里。愛掏網 - it200.com
改wp-config.php
該部分對應Step 3: Set up wp-config.php。愛掏網 - it200.com(參考 wp-config.php | Common APIs Handbook | WordPress Developer Resources)
cd wordpresscp wp-config-sample.php wp-config.phpnano wp-config.php
在
// ** Database settings - You can get this info from your web host ** //
下面把 DB_NAME、DB_USER、DB_PASSWORD、DB_HOST根據指南改掉。愛掏網 - it200.com
如果你根據這個指南完成了上一步,那么改成這個即可(password
是你前一節設置的密碼):
/** The name of the database for WordPress */define( DB_NAME, wordpress );/** Database username */define( DB_USER, wordpress );/** Database password */define( DB_PASSWORD, password );/** Database hostname */define( DB_HOST, localhost );
然后把
* Authentication Unique Keys and Salts.
下面的AUTH_KEY等替換成用官方的 生成器 生成的secret key。愛掏網 - it200.com
移動文件
該部分對應Step 4: Upload the files
直接
cd ..sudo mv wordpress /var/www/html/wordpresschown -R www-data:www-data /var/www/htmlsudo find /var/www/html/wordpress -type d -exec chmod 755 {} \;sudo find /var/www/html/wordpress -type f -exec chmod 644 {} \;
即可(參見 LinuxCapable的教程 )。愛掏網 - it200.com
搞nginx
-
/etc/nginx/sites-available/default
:這個文件里應該已經有fastcgi_pass unix:/run/php/php7.4-fpm.sock;
了,不用動就可以了。愛掏網 - it200.com -
/etc/nginx/nginx.conf
:不用改。愛掏網 - it200.com
只需要修改/etc/nginx/sites-available/wordpress
即可,我是直接在 LinuxCapable的模板 上把php8.1-fpm改成了php7.4-fpm。愛掏網 - it200.com可能也可以直接用 Nginx官網的WordPress recipe 。愛掏網 - it200.com
我的/etc/nginx/sites-available/wordpress
如下:
server { listen 80; listen [::]:80; server_name example.com; root /var/www/html/wordpress; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php?$args; } location ~* /wp-sitemap.*\.xml { try_files $uri $uri/ /index.php$is_args$args; } client_max_body_size 100M; location ~ \.php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; fastcgi_buffer_size 128k; fastcgi_buffers 4 128k; fastcgi_intercept_errors on; } gzip on; gzip_comp_level 6; gzip_min_length 1000; gzip_proxied any; gzip_disable msie6; gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml; # assets, media location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ { expires 90d; access_log off; } # svg, fonts location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ { add_header Access-Control-Allow-Origin *; expires 90d; access_log off; } location ~ /\.ht { access_log off; log_not_found off; deny all; }}
之后,運行:(參見 LinuxCapable的教程 )
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/sudo nginx -t # 如果這一步不ok,則需要根據提示檢查你的nginx配置文件sudo systemctl restart nginx
即可。愛掏網 - it200.com現在應該可以在example.com
(你的域名)上看到你的網站了