基于WordPress搭建个人网站(Linux)
应用场景
本文主要介绍了如何使用弹性云服务器的Linux实例手工搭建LNMP平台,并部署WordPress环境。WordPress简称WP,最初是一款博客系统,后逐步演化成一款免费的CMS(内容管理系统/建站系统)。该指导具体操作以CentOS 7.2 64位操作系统为例。
方案架构
方案优势
- 快速构建站点,组网架构简单。
- 网站安全易用。
资源和成本规划
资源 |
资源说明 |
成本说明 |
---|---|---|
虚拟私有云VPC |
VPC网段:192.168.0.0/16 |
免费 |
虚拟私有云子网 |
|
免费 |
安全组 |
入方向规则:
|
免费 |
弹性云服务器 |
|
ECS涉及以下几项费用:
具体的计费方式及标准请参考计费模式概述。 |
Nginx |
是一个高性能的HTTP和反向代理web服务器。 获取方式: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm |
免费 |
MySQL |
是一款开源的关系数据库软件。 获取方式: http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm |
免费 |
PHP |
是一款开源软件,用于Web开发。 获取方式: |
免费 |
WordPress |
是一款开源的博客软件。 获取地址: |
免费 |
域名 |
用于访问搭建的个人网站。 |
域名的价格以域名注册商所示为准,请参考域名注册商帮助文档。 |
基于WordPress搭建个人网站操作流程
实施步骤
安装前准备
- 已购买虚拟私有云和弹性公网IP。
- 如果规划为网站配置域名,需已经购买好相应的域名。
- 弹性云服务器所在安全组添加了如表2所示的安全组规则,具体步骤参见为安全组添加安全组规则。
- 为了更好的获取和更新系统和软件,建议您更新镜像源为华为云镜像源,详细操作,请参见如何使用自动化工具配置华为云镜像源(x86_64和ARM)?。
操作步骤
- 安装Nginx。
- 安装MySQL。
- 依次执行以下命令,安装MySQL。
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
- 依次执行以下命令,启动MySQL服务并设置开机自启动。
systemctl enable mysqld
- 查看MySQL运行状态。
systemctl status mysqld.service
[root@ecs-adc3 ~]# systemctl status mysqld.service ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2021-08-16 19:33:40 CST; 36s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 7916 (mysqld) CGroup: /system.slice/mysqld.service └─7916 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Aug 16 19:33:35 ecs-adc3 systemd[1]: Starting MySQL Server... Aug 16 19:33:40 ecs-adc3 systemd[1]: Started MySQL Server.
- 执行以下命令,获取安装MySQL时自动设置的root用户密码。
grep 'temporary password' /var/log/mysqld.log
回显如下类似信息。2021-08-16T11:33:37.790533Z 1 [Note] A temporary password is generated for root@localhost: ;8nPd29lhs,k
- 执行以下命令,并按照回显提示信息进行操作,加固MySQL。
Securing the MySQL server deployment. Enter password for user root: #输入上一步骤中获取的安装MySQL时自动设置的root用户密码 The existing password for the user account root has expired. Please set a new password. New password: #设置新的root用户密码 Re-enter new password: #再次输入密码 The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : N #是否更改root用户密码,输入N ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y Success. All done!
- 依次执行以下命令,安装MySQL。
- 安装PHP。
- 执行以下命令,安装epel源。
- 执行以下命令,安装remi源。
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
- 执行以下命令,安装Yum源管理工具。
- 执行以下命令,安装PHP 7.4。
yum -y install php74-php-gd php74-php-pdo php74-php-mbstring php74-php-cli php74-php-fpm php74-php-mysqlnd
- 执行以下命令,验证PHP的安装版本。
回显如下类似信息:
PHP 7.4.33 (cli) (built: Jun 6 2023 15:55:08) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
- 执行以下命令,启动PHP服务并设置开机自启动。
systemctl enable php74-php-fpm
- 修改Nginx配置文件以支持PHP。
- 执行以下命令打开配置文件/etc/nginx/nginx.conf。
从nginx.conf可以看出,配置文件定向至/etc/nginx/conf.d/*.conf。
- 输入:quit退出nginx.conf。
- 执行以下命令打开配置文件/etc/nginx/conf.d/default.conf。
- 按i键进入编辑模式。
- 修改打开的“default.conf”文件。
找到server段落,修改或添加下列配置信息。
server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; }
如图4所示:
- 按Esc键退出编辑模式,并输入:wq保存后退出。
- 执行以下命令打开配置文件/etc/nginx/nginx.conf。
- 执行以下命令,重新载入nginx的配置文件。
- 浏览器访问测试。
- 创建数据库。
- 执行以下命令,并按照提示信息输入MySQL的root用户,登录到MySQL命令行。
- 执行以下命令,创建一个新的数据库。
其中,“wordpress”为数据库名,可以自行设置。
- 执行以下命令,为数据库创建用户并为用户分配数据库的完全访问权限。
GRANT ALL ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'xxxxx';
其中,“wordpressuser”为数据库用户名,“xxxxx”为对应的账户密码,可以自行设置。
- 执行以下命令,退出MySQL命令行。
- (可选)依次执行以下命令,验证数据库和用户是否已成功创建,并退出MySQL命令行。
SHOW DATABASES
exit
其中,“wordpressuser”为刚刚创建的数据库用户名。
- 安装WordPress。
- 请自行获取WordPress软件包并上传至/usr/share/nginx/html目录。操作方法请参见本地Windows主机使用WinSCP上传文件到Linux云服务器。
后续操作软件包以“wordpress-6.4.2-zh_CN.tar.gz”为例。
- 执行以下命令,进入/usr/share/nginx/html/目录,解压缩软件包。
tar zxvf wordpress-6.4.2-zh_CN.tar.gz
解压后生成一个“wordpress”的文件夹。
- 执行以下命令,进入 WordPress 安装目录,将wp-config-sample.php文件复制到wp-config.php文件中,并将原先的示例配置文件保留作为备份。
cd /usr/share/nginx/html/wordpress
cp wp-config-sample.php wp-config.php
- 执行以下命令,打开并编辑新创建的配置文件。
- 按i键进入编辑模式。找到文件中MySQL的部分,并将相关配置信息修改为步骤5中的内容。
图5 修改MySQL配置
- 按Esc键退出编辑模式,并输入:wq保存后退出。
- 浏览器访问“http://服务器IP地址/wordpress”进入安装向导。
- 设置站点标题、管理员账号、密码和邮箱,并单击“安装 WordPress”。
表3 配置参数 参数
说明
站点标题
WordPress网站名称。
用户名
WordPress管理员名称。
密码
可以使用默认的密码或者自定义密码。
请勿重复使用现有密码,并确保将密码保存在安全的位置。
您的电子邮箱地址
接收通知的电子邮件地址。
- 安装完成后,弹出安装成功界面。
- 单击“登录”,或者浏览器访问“http://服务器IP地址/wordpress/wp-login.php”,输入用户名/电子邮箱地址和密码后单击“登录”,进入WordPress。
- 请自行获取WordPress软件包并上传至/usr/share/nginx/html目录。操作方法请参见本地Windows主机使用WinSCP上传文件到Linux云服务器。
- 购买域名。
为了便于网站的访问和使用,可以给网站设置一个单独的域名,使用域名访问网站。首先,需要在域名注册商处获得授权的域名。
- 配置域名解析。
配置域名解析后才能使用注册的域名访问网站。具体操作请参见配置公网域名。
例如,配置的域名为“www.example.com”,配置成功后,可在浏览器地址栏中输入“http://www.example.com”访问部署的网站。