
使用 Ubuntu-18.04 和 WordPress 搭建博客
WordPress 有很优秀的博客文章管理, 也有很多的主题和插件可以制作各种各样的网站, 甚至购物网站, 这篇文章将帮助你基于 Ubuntu-18.04 部署 WordPress 搭建博客
环境
- 系统: Ubuntu 18.04 ×64
- 服务: Apache 2
- 语言: PHP 7.4
- 数据库: MySQL 5.7
- 网站框架: WordPress 5.4.2–zh_CN
安装
更新系统
1 2
| sudo apt update sudo apt upgrade
|
添加 ppa 源
1 2 3 4
| sudo apt install software-properties-common -y sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt upgrade
|
安装所需要的软件
Apache
1
| sudo apt install apache2 -y
|
PHP
1 2
| sudo apt install php7.4 -y sudo apt install libapache2-mod-php7.4 -y
|
MySQL
1 2
| sudo apt install php7.4-mysql -y sudo apt install mysql-server -y
|
WordPress
到官方简体中文网手动下载: cn.wordpress.org/download 或者执行:
1
| wget https://cn.wordpress.org/latest-zh_CN.tar.gz
|
下载后解压并拷贝到目录 /var/www/html/
下
1 2
| tar zxf latest-zh_CN.tar.gz sudo mv -f wordpress/ /var/www/html/
|
配置
修改 wordpress 文件夹的所有者和读写属性
将 wp 替换为你的用户名
www-data
是服务器的默认组
1
| sudo chown -R wp:www-data wordpress
|
修改读写属性
1
| sudo chmod 777 -R wordpress
|
启用 Apache Rewrite 操作
让其能够重定向你的访问链接
配置 MySQL 数据库
用于后面的网站数据存储, 先给 MySQL
控制台 root
用户添加免密登录
1
| sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
|
在最后一行添加
重启服务后进入 MySQL
控制台
1 2
| sudo service mysql restart mysql -u root -p
|
修改 MySQL root 用户的密码
将 xxxxxx
替换为你自己的密码
1 2 3
| update mysql.user set authentication_string=PASSWORD('xxxxxx'), plugin='mysql_native_password' where user='root'; flush privileges; exit
|
这样下次进入时就使用自己定义的密码登录 MySQL
控制台了, 然后记得回到 mysqld.cnf
把刚刚添加的 skip-grant-tables
删除, 否则不安全
1
| sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
|
删除
重启服务
1
| sudo service mysql restart
|
创建数据库
将 xxxxxx
替换为你自己的密码
1 2 3 4 5
| create database wordpressdb; create user wordpress@localhost IDENTIFIED BY 'xxxxxx'; GRANT ALL PRIVILEGES ON wordpressdb.* TO wordpress@localhost; FLUSH PRIVILEGES; exit
|
配置 Apache 主页
1
| sudo vi /etc/apache2/sites-enabled/000-default.conf
|
修改主页在 wordpress 目录下
000-default.conf1 2
| - DocumentRoot /var/www/html + DocumentRoot /var/www/html/wordpress
|
修改 Apache 服务器对文件夹的读写限制
1
| sudo vi /etc/apache2/apache2.conf
|
将原来的
apache2.conf1 2 3 4 5 6 7
| <Directory /var/www/> - Options Indexes FollowSymLinks + Options FollowSymLinks - AllowOverride None + AllowOverride All Require all granted </Directory>
|
并在最后添加一行域名地址, 否则重启服务会报错
apache2.conf1
| ServerName www.domain.com
|
修改 WordPress 数据库配置
1 2 3
| cd /var/www/html/wordpress/ cp wp-config-sample.php wp-config.php sudo vi wp-config.php
|
将要连接的数据库设置为前面 MySQL
控制台所配置的
wp-config.php1 2 3 4 5 6 7 8
|
define( 'DB_NAME', 'wordpressdb' );
define( 'DB_USER', 'wordpress' );
define( 'DB_PASSWORD', 'xxxxxx' );
|
并在最后面添加 WordPress
进行更新时免登录的配置
wp-config.php1 2 3
| + define("FS_METHOD", "direct"); + define("FS_CHMOD_DIR", 0777); + define("FS_CHMOD_FILE", 0777);
|
重启服务
1 2
| sudo service mysql restart sudo apache2ctl restart
|
到这里已经可以用你的域名访问 WordPress
进行网站的初始化配置了~
ssl 配置
若你的域名是 http
类型的, 可以在阿里云申请免费 ssl
证书, 将 http
升级为 https
, 申请比较简单这里就不说了
选择 Apache
服务器类型的证书下载, 并上传到 /etc/apache2/xxx
目录下, 如 /etc/apache2/cert
启用 SSL 模块
可以执行命令查看是否自动生成 default-ssl.conf
文件
1
| ll /etc/apache2/sites-available/
|
应该会看到
1 2 3 4 5
| total 20 drwxr-xr-x 2 root root 4096 Jun 22 12:26 ./ drwxr-xr-x 9 root root 4096 Jun 22 12:27 ../ -rw-r--r-- 1 root root 1342 Jun 22 10:28 000-default.conf -rw-r--r-- 1 root root 6308 Jun 22 10:47 default-ssl.conf
|
修改 SSL 配置文件
1
| sudo vi /etc/apache2/sites-available/default-ssl.conf
|
将对应的参数修改为你自己的内容
default-ssl.conf1 2 3 4 5 6 7 8 9 10
| <IfModule mod_ssl.c> <VirtualHost *:443> # 添加ServerName, 域名替换为你自己的 ServerName www.domain.com DocumentRoot /var/www/html/wordpress ... SSLCertificateFile /etc/apache2/cert/cert.crt SSLCertificateKeyFile /etc/apache2/cert/cert.key ... SSLCertificateChainFile /etc/apache2/cert/chain.crt
|
创建软连接进行关联
1
| sudo ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/001-ssl.conf
|
重新加载 Apache 配置文件
1
| sudo /etc/init.d/apache2 force-reload
|
重启 Apache 服务
1
| sudo /etc/init.d/apache2 restart
|
然后使用 https://www.domain.com
访问你的域名, 看是否能正常访问并且域名左边有个小锁头, 说明成功~
强制 https 访问
1 2
| cd /var/www/html/wordpress/ sudo vi .htaccess
|
在配置里最后增加两行, 必须在 <IfModule>
以外, 否则会被自动重写从而丢失
.htaccess1 2 3 4 5 6
| </IfModule>
+ ## END WordPress + ## 强制 HTTPS 访问 + RewriteCond %{SERVER_PORT} !^443$ + RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
|
这样用 http
访问也会自动跳转到 https
, 同时最好也在 WordPress
后台管理的 个人资料 和 设置 里面的把 站点/域名 都改为 https
类型的
美化 WordPress 搜索 url
将搜索后的 url 从 https://www.domain.com/?s=搜索词
变为 https://www.domain.com/search/搜索词
在主题函数模板 functions.php
最后添加即可
functions.php1 2 3 4 5 6 7 8 9 10 11
| function change_search_url_rewrite() { if ( is_search() && ! empty( $_GET['s'] ) ) { wp_redirect( home_url( '/search/' ) . urlencode( get_query_var( 's' ) ) ); exit(); } elseif ( is_search() && empty( get_query_var( 's' ) ) ) { wp_redirect( home_url( '/search' ) ); exit(); } } add_action( 'template_redirect', 'change_search_url_rewrite' );
|
安装 PHP 模组
安装完后在 管理后台
– 工具
– 站点健康
会看到 WordPress
检测到 PHP
缺失的模组, 根据提示的模组名字一个个安装即可, 比如 curl
1
| apt-cache search curl | grep php
|
可能得到:
1 2 3 4 5 6 7 8
| php7.2-curl - CURL module for PHP php-http-request2 - Provides an easy way to perform HTTP requests php-curl - CURL module for PHP [default] php5.6-curl - CURL module for PHP php7.0-curl - CURL module for PHP php7.1-curl - CURL module for PHP php7.3-curl - CURL module for PHP php7.4-curl - CURL module for PHP
|
根据搜索结果安装与当前 php 版本相同的模组
1
| sudo apt install php7.4-curl -y
|
提示缺失的模组全部安装完后重启 Apache2
服务即可
1
| sudo /etc/init.d/apache2 restart
|
又或者直接执行下面命令, 全部一次性安装
1 2
| sudo apt install php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl php-imagick -y sudo /etc/init.d/apache2 restart
|