LAMP 安装
参考:How To Install WordPress on Ubuntu 20.04 with a LAMP Stack | DigitalOcean
1 Apache安装并更新防火墙
sudo apt update
sudo apt install apache2
sudo ufw app list # 列出当前所有UFW的可用应用
# Output
# Available applications:
# Apache # 代表Apache只能用80端口
# Apache Full # 代表Apache能使用所有端口
# Apache Secure # 代表Apache只能用443端口
# OpenSSH
sudo ufw allow in "Apache" # 只为Apache开放80端口
sudo ufw status
此时访问http://your_server_ip可以看到Apache的默认界面
以下可以查询本机IP:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
2 MySQL安装
sudo apt install mysql-server
sudo mysql_secure_installation
sudo mysql
3 PHP安装
sudo apt install php libapache2-mod-php php-mysql
php -v
4 创建虚拟主机
以下的your_domain
需要自己定义命名
sudo mkdir /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain
sudo vim /etc/apache2/sites-available/your_domain.conf
# 添加配置如下
<VirtualHost *:80>
ServerName your_domain
ServerAlias www.your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite your_domain # 为新的虚拟主机使能
sudo a2dissite 000-default
sudo apache2ctl configtest
sudo systemctl reload apache2
vim /var/www/your_domain/index.html # 编写网页如下
<html>
<head>
<title>your_domain website</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the landing page of <strong>your_domain</strong>.</p>
</body>
</html>
此时重启Apache服务,访问http://server_domain_or_IP可以看到新的主页
备注:
# 如果要添加index.php为默认主页选项
sudo vim /etc/apache2/mods-enabled/dir.conf # 修改配置如下
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
WordPress 安装配置
参考:How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04 | DigitalOcean
1 创建WordPress专用的MySQL数据库
mysql -u root -p
如果上述不能直接使用,则:
sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
mysql -u root -p
(以下wordpress
代表数据库名、wordpressuser
代表专用用户名,可更改)
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';
FLUSH PRIVILEGES;
EXIT;
2 安装PHP附加扩展
sudo apt update
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
sudo systemctl restart apache2
3 调整Apache的配置文件 许可覆盖和重写
sudo vim /etc/apache2/sites-available/wordpress.conf
# 添加配置如下
<Directory /var/www/wordpress/>
AllowOverride All
</Directory>
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl restart apache2
4 下载WordPress
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
mkdir /tmp/wordpress/wp-content/upgrade
sudo cp -a /tmp/wordpress/. /var/www/wordpress # 此处的第二个wordpress可改名
5 配置WordPress
以下wordpress
均可改名,与第4步中的一致
sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
curl -s https://api.wordpress.org/secret-key/1.1/salt/ # 获得类似于如下的输出
Output
define('AUTH_KEY', '1jl/vqfs<XhdXoAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<J@4H');
define('SECURE_AUTH_KEY', 'E2N-h2]Dcvp+aS/p7X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');
define('LOGGED_IN_KEY', 'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');
define('NONCE_KEY', 'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');
define('AUTH_SALT', 'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES 07VC*Lj*lD&?3w!BT#-');
define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');
define('LOGGED_IN_SALT', 'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');
define('NONCE_SALT', 'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');
sudo vim /var/www/wordpress/wp-config.php # 用上一步的输出修改如下配置
. . .
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
. . .
. . .
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
# 记得修改数据库配置
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'wordpressuser' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
. . .
define('FS_METHOD', 'direct'); # 添加这一步配置
6 登录并完成账户配置
https://server_domain_or_IP,进行语言选择、账号设置、密码设置等,最终完成。
插件设置
避坑(可能是我自己不会用):
- WP Editor.md,一个支持markdown编辑的插件。没有用,非常卡顿,最终显示效果也很差。
- Enlighter,一个代码高亮插件。非常不好用。
- SyntaxHighlighter Evolved,一个轻量化代码高亮插件。非常不好用。
提速插件
Autoptimize插件,通过优化页面JS、CSS、图像、Google字体等为网站提速,效果显著。
数学公式查看与代码高亮
- WP Gitbuber MD,支持以Markdown形式编辑文章,也有高亮功能(勾选设置行号、高亮都没起作用,但至少有明显的框和一键复制了),支持块公式和内联公式(但内联公式不起作用)
- MathML block,搭配上一个插件使用,内联公式也可以显示了
- 主题换成了Twenty Fifteen,因为之前的主题会使文章显示时很多东西渲染不出来,只能在编辑界面看到
标签页ico
由于我没有Appearance-Customizer选项,故参考https://www.wpbeginner.com/wp-tutorials/how-to-fix-missing-theme-customizer-in-wordpress-admin/中https://example.com/wp-admin/customize.php的链接直接访问,最终修改成功。
访问量统计
WP Statistics,一个非常方便好用的统计访问量、访问来源的插件(还有很多其他高级功能)。其中对访问来源所属地区的统计,需要打开“访问识别”设置中的“GeoIP收集”和“计划每月更新GeoIP数据库”。