这篇文章也可以在我的博客中查看
WordPress是一个功能强大的内容管理系统,易于使用和扩展,因此被广泛应用于博客、企业网站、电商平台等各类网站的建设中。本文将为大家介绍如何在Linux操作系统上,利用Nginx作为Web服务器,搭建一个基于WordPress的网站。
更新包
sudo apt update
安装
sudo apt install mariadb-server
sudo mysql_secure_installation
跟着指引走就好
sudo systemctl status mariadb
输出:
● mariadb.service - MariaDB 10.5.15 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-11-18 14:33:41 CST; 4 months 13 days ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Debian系统的MariaDB 10.5默认不使用密码,而是使用unix_socket
插件进行root用户身份验证。为了方便其它软件登入,还是需要配置密码登录。
而root帐户会执行诸如日志轮换和启动/停止服务器之类的任务,最好不要更改root帐户的身份验证详细信息。
因此,建议创建一个使用密码的管理员帐户。新帐户名为admin,与root帐户具有相同的功能。在终端中打开MariaDB提示以进行操作。
sudo mariadb
GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;
sudo mariadb -u admin -p
创建数据库,db_wordpress
是数据库的命名
CREATE DATABASE db_wordpress;
SHOW DATABASES;
exit;
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
php的拓展装起来比较蛋疼,建议将这段东西保存下来,方便日后查找自己到底装了什么,也方便升级……
sudo apt install php8.2 php8.2-bcmath php8.2-cgi php8.2-cli php8.2-common php8.2-fpm php8.2-gd php8.2-mbstring php8.2-mysql php8.2-xml php8.2-zip
php
php-mysql
php-fpm
是必须的,php-cli
php-common
建议安装
查看安装的php及拓展
dpkg -l | grep php
你可能需要安装unzip
sudo apt install unzip
your_site_name
是你网站文件夹的名字,用于自己辨别
cd ~
wget https://wordpress.org/latest.zip
unzip -q latest.zip
mv wordpress your_site_name
cd your_site_name
cp wp-config-sample.php wp-config.php
vim wp-config.php
在打开的配置文件中,修改这几处数据库配置:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
sudo apt install nginx
建议在conf.d
新增配置块
sudo vim /etc/nginx/conf.d/your_site_name.conf
在your_site_name.conf
里输入以下字段配置php后端及html服务器
注意两个配置项:
root
是你WordPress文件夹的路径,以本文为例是/home/[username]/your_site_name
server_name
是你域名,没有域名可以不填upstream php
{
#server unix:/run/php/php7.4-fpm.sock;
server 127.0.0.1:9000;
}
server
{
listen 80;
listen [::]:80;
charset utf-8;
server_name www.yourdomain.com;
root /path-to-your-wordpress-folder;
index index.php index.html index.htm;
client_max_body_size 64M;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location = /robots.txt
{
allow all;
log_not_found off;
access_log off;
}
location /
{
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$
{
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
#include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
{
expires max;
log_not_found off;
}
}
⚠注意:http并不安全
建议配置https服务器,将http重定向至https
ssl_certificate
: 你ssl证书的地址,一般存放于/etc/ssl/...
文件夹内ssl_certificate_key
: 你sll私钥的地址ssl_verify_client
: 如果你使用Cloudflare等CDN,可以取消注释设置为on
,以防止CDN以外的人员直接访问。如果开启,请注意:
ssl_client_certificate
: 客户端ssl证书,在CDN官方下载,用于识别其身份upstream php
{
#server unix:/run/php/php7.4-fpm.sock;
server 127.0.0.1:9000;
}
server
{
listen 80;
listen [::]:80;
charset utf-8;
server_name www.yourdomain.com;
return 301 https://$host$request_uri;
}
server
{
listen 443 ssl http2;
listen [::]:443 ssl http2;
charset utf-8;
server_name www.yourdomain.com;
root /path-to-your-wordpress-folder;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
# ssl_client_certificate /etc/ssl/cloudflare.crt;
# ssl_verify_client on;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m; # about 40000 sessions
ssl_session_tickets off;
# intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;
#root /usr/share/nginx/html;
index index.php index.html index.htm;
client_max_body_size 64M;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location = /robots.txt
{
allow all;
log_not_found off;
access_log off;
}
location /
{
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$
{
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
#include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
{
expires max;
log_not_found off;
}
}
sudo systemctl restart nginx
如果启动出现问题,请使用
sudo nginx -t
追踪问题
找到你的php-fpm配置文件,一般位于/etc/php/...
这里我是:
sudo vim /etc/php/8.2/fpm/pool.d/www.conf
在配置文件中找到listen = /var/run/php5-fpm.sock
一项,将其注释,并改为socket模式:
;listen = /var/run/php8.2-fpm.sock <-- 注释
listen = 127.0.0.1:9000 <-- 与上一步nginx配置的端口一致
sudo systemctl restart php8.2-fpm
打开浏览器,输入服务器地址,或者映射到服务器的域名(注意域名建议要和nginx中配置的一致),可以访问到WordPress站点。
首次访问后,会出现WordPress站点的初始化界面。
按照其引导进行操作即可完成安装。
如果你发现站点没有写入权限
(比如无法上传媒体库、修改语言不生效)
请确保你的nginx
和php-fpm
的运行用户有对wordpress站点文件夹写入的权限。
nginx
和php-fpm
的运行用户(默认一般为www-data)有写入权限www-data
写入的权限nginx
可以到/etc/nginx/nginx.conf
文件中修改user
字段php-fpm
可以到上面提到的同一配置文件修改user
和group
字段