手动搭建 WordPress

WordPress无疑是全世界最流行的CMS平台,据统计,43%的互联网网站都由wordpress搭建。全世界虚拟主机提供商几乎都会提供一键建wordpress的功能,方便个人、企业快速搭建门户、博客网站,这种方式也是首选的方式,免去很多维护管理的成本。

本文则按照一步步方式搭建,有兴趣的可以了解下过程。

拓扑如下:

图片

在两台机器上都更新下

sudo apt update & sudo apt upgrade
sudo reboot

代理机器安装 nginx

sudo apt install nginx
sudo systemctl status nginx

博客机器(wordpress)安装apache2

sudo apt install apache2
sudo systemctl status apache2

在浏览器上测试这两个地址能正常访问。

由于还没设置相关安全性,先关闭这两个服务,

另外在两台服务器上新建普通用户kelemi,避免用root用户操作

systemctl stop nginx # nginx服务器
systemctl stop apache2 # apache服务器
adduser kelemi
usermod -aG sudo kelemi

限制root远程ssh

限制特定的IP才能登录服务器

vim /etc/ssh/sshd_config

修改 PermitRootLogin 为 no

systemctl restart ssh

为Wordpress设置UFW和MariaDB

设置ufw,只允许合法IP地址访问

comment 在高版本 ufw中支持

以下是nginx的ufw设置

which ufw # 确认已安装
sudo ufw allow from ... to any port 22 proto tcp comment “ssh”
ufw enable
ufw status numbered
sudo ufw allow 80 comment ‘apache’
sudo ufw allow 443 comment ‘https’
ufw status numbered

设置apache的防火墙

只允许 nginx 服务器访问

sudo ufw allow from to any port 22 proto tcp comment ‘ssh’
sudo ufw allow from to any port 80 proto tcp comment ‘apache’
sudo ufw enable

下面开始设置 MariaDB,在apache服务器上

sudo apt install mariadb-server
systemctl status mariadb

设置mariadb

mariadb # 进入mariadb
CREATE DATABASE wordpressdb;
show databases;
GRANT ALL PRIVILEGES on wordpressdb.* to wordpress@localhost IDENTIFIED BY ‘mypassword’;
mysql_secure_installation # 设置root密码等信息

设置Wordpress的Apache

ssh到apache服务器,因上一节的IP限制,需先ssh到nginx,再从nginx服务器ssh到apache.

需要限制实际的服务器对公众的开放。

实际还有堡垒主机的解决方案,保证所有ssh都通过严格控制的堡垒主机才能访问,当然这超出了本节的范围。

更新系统

sudo apt update

安装一些php包

sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip

启用apache重写模块,并重启apache,检查是否正常

sudo a2enmod rewrite
sudo systemctl restart apache2
sudo systemctl status apache2

查看 /etc/apache2/sites-available

禁用原有的站点

sudo a2dissite 000-default.conf
sudo a2dissite default-ssl.conf

新建wordpress站点,命名是 blog

sudo vim /etc/apache2/sites-available/wordpress.td.masterpeak.cn.conf
DocumentRoot “/var/www/html/wordpress.td.masterpeak.cn” ServerName wordpress.td.masterpeak.cn Options MultiViews FollowSymlinks AllowOverride All Order allow,deny Allow from all TransferLog /var/log/apache2/wordpress.td.masterpeak.cn_access.log ErrorLog /var/log/apache2/wordpress:w.td.masterpeak.cn_error.log

启用站点

sudo a2ensite wordpress.td.masterpeak.cn.conf
sudo systemctl restart apache2
sudo systemctl status apache2

回到主目录下载wordpress

wget https://wordpress.org/latest.tar.gz
tar -xvf latest.tar.gz

改变属主为apache使用

www-data: 等同于 www-data:www-data,属于简化写法

sudo chown www-data: -R wordpress/
ls -l wordpress/

迁移到到apache的目录

sudo mv wordpress /var/www/html/wordpress.td.masterpeak.cn
ls -l /var/www/html

删除原有的文件

rm /var/www/html/index.html

sudo !! # tips:两个感叹号表示最后一次的命令

用sudo !!就可以加上sudo执行,避免重复输入

cd /var/www/html/wordpress.td.masterpeak.cn/
ls -l

重命名wp-config-sample.php,再作修改

sudo mv wp-config-sample.php wp-config.php
vim wp-config.php

修改数据库名,用户名,密码,服务器等数据库连接信息

:wq # 保存

最后完成wordpressp安装

启动nginx

sudo systemctl status nginx
sudo systemctl start nginx

进入nginx 目录,查看情况

cd /etc/nginx
ls -l

看到有sites-available和sites-enabled目录

与apache非常相似,sites-available有默认的站点,可以添加自己定义的

比如 blog.learnlinux.cloud.conf

而在 sites-enabled 有指向 sites-available下的文件的链接

nginx没有apache的 a2ensite命令,所以需要手动创建软链接

删除默认站点配置文件

cd /etc/nginx/sites-enabled
rm default
cd ../sites-available
rm default

vim /etc/nginx/sites-available/wordpress.td.masterpeak.cn.conf

修改反向代理指向wordpress服务器

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/wordpress.td.masterpeak.cn.conf

同名软链接可以省略名称

重启nginx

sudo systemctl restart nginx
sudo systemctl status nginx

apache服务器需要安装几个包才能运行

sudo apt install php-mysql libapache2-mod-php
sudo systemctl restart apache2

测试访问正常,设置登陆的管理员用户密码

目前nginx没有ssl,显示不安全,需要处理

与 nextcloud设置类似

sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-nginx
sudo certbot –nginx -d wordpress.td.masterpeak.cn

apache服务器有个文件可能需要修改

sudo vim /var/www/html/wordpress.td.masterpeak.cn/.htaccess

最上面添加一行

SetEnvIf X-Forwarded-Proto https HTTPS

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注