简介
本文主要介绍了如何使用弹性云服务器的Linux实例手工搭建LNMP平台,并部署WordPress环境。WordPress简称WP,最初是一款博客系统,后逐步演化成一款免费的CMS(内容管理系统/建站系统)。该指导具体操作以CentOS 7.2 64位操作系统为例。
前提条件
- 已购买虚拟私有云和弹性公网IP。
- 如果规划为网站配置域名,需已经购买好相应的域名。
- 弹性云服务器所在安全组添加了如表1所示的安全组规则,具体步骤参见为安全组添加安全组规则。
操作步骤
- 安装Nginx。
- 安装MySQL。
- 依次执行以下命令,安装MySQL。
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql-community-server
- 依次执行以下命令,启动MySQL服务并设置开机自启动。
systemctl enable mysqld
- 执行以下命令,获取安装MySQL时自动设置的root用户密码。
grep 'temporary password' /var/log/mysqld.log
回显如下类似信息。2018-08-29T07:27:37.541944Z 1 [Note] A temporary password is generated for root@localhost: 2YY?3uHUA?Ys
- 执行以下命令,并按照回显提示信息进行操作,加固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。
- 依次执行以下命令,安装PHP 7和一些所需的PHP扩展。
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm
- 执行以下命令,验证PHP的安装版本。
回显如下类似信息:
PHP 7.0.31 (cli) (built: Jul 20 2018 08:55:22) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
- 执行以下命令,启动PHP服务并设置开机自启动。
systemctl enable php-fpm
- 修改Nginx配置文件以支持PHP。
- 执行以下命令打开配置文件“default.conf”。
- 按i键进入编辑模式。
- 修改打开的“default.conf”文件。
- 在所支持的主页面格式中添加php格式的主页,如下所示:
location / { root /usr/share/nginx/html; index index.php index.html?fromacct=525a03cf-8bd2-4921-b724-84ea259121fb&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905 index.htm; }
- 取消如下内容的注释,并设置字体加粗部分为Nginx的默认路径,如下图所示:
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; }
- 在所支持的主页面格式中添加php格式的主页,如下所示:
- 按Esc键退出编辑模式,并输入:wq保存后退出。
- 执行以下命令,重新载入nginx的配置文件。
- 依次执行以下命令,安装PHP 7和一些所需的PHP扩展。
- 浏览器访问测试。
- 创建数据库。
- 执行以下命令,并按照提示信息输入MySQL的root用户,登录到MySQL命令行。
- 执行以下命令,创建一个新的数据库。
其中,“wordpress”为数据库名,可以自行设置。
- 执行以下命令,为数据库创建用户并为用户分配数据库的完全访问权限。
GRANT ALL ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'BLOck@123';
其中,“wordpressuser”为数据库用户名,“BLOck@123”为对应的帐户密码,可以自行设置。
- 执行以下命令,退出MySQL命令行。
- (可选)依次执行以下命令,验证数据库和用户是否已成功创建,并退出MySQL命令行。
SHOW DATABASES;
exit
其中,“wordpressuser”为刚刚创建的数据库用户名。
- 安装WordPress。
- 请自行获取WordPress软件包并上传至/usr/share/nginx/html目录。
- 执行以下命令,解压缩软件包。
tar -xvf wordpress-4.9.8.tar.gz
解压后生成一个“wordpress”的文件夹。
- 执行以下命令,设置解压后的文件权限。
- 浏览器访问“http://服务器IP地址/wordpress”进入安装向导。
- 按照界面信息提示准备好数据库的信息,并单击“Let's go”。
- 填写数据库连接信息,包括数据库名称、数据库用户和数据库密码。
- 数据库验证通过后,进入安装界面。 单击“Run the installtion”。
- 设置站点标题、管理员账号、密码和邮箱, 点击“Install WordPress”。
- 安装完成后,弹出安装成功界面。
- 单击“Log In”,或者浏览器访问“http//服务器IP地址/wordpress/wp-admin”,输入用户名和密码后单击“Log In”,进入WordPress。
- 购买域名。
为了便于网站的访问和使用,可以给网站设置一个单独的域名,使用域名访问网站。首先,需要在域名注册商处获得授权的域名。
- 进行备案。
如果网站未进行备案,且需要使用华为云进行托管,则需要通过华为云备案系统进行备案。具体操作请参见如何进行备案。
- 配置域名解析。
配置域名解析后才能使用注册的域名访问网站。具体操作请参见配置域名解析。
例如,配置的域名为“www.example.com”,配置成功后,可在浏览器地址栏中输入“http//www.example.com”访问部署的网站。