×
Community Blog Build a WordPress Website on a CentOS 7 Instance

Build a WordPress Website on a CentOS 7 Instance

This article describes how to build a WordPress website on an Elastic Compute Service (ECS) instance that runs CentOS 7.

By Alibaba Cloud ECS Team

WordPress is a blog-publishing system written in PHP. You can use WordPress as a content management system (CMS) or use WordPress to build your own websites on servers that support PHP and MySQL databases. This article describes how to build a WordPress website on an Elastic Compute Service (ECS) instance that runs CentOS 7.

Background Information

You can deploy a WordPress website by using one of the following methods based on your business requirements:

  • Quick deployment: This method allows you to quickly deploy an LNMP stack and a WordPress website by using Resource Orchestration Service (ROS). Compared with manual deployment, this method automates resource deployment and configuration.
  • Manual deployment: This method is suitable for users who are familiar with the Linux operating system, software versions, and compatibility. Before you manually deploy a WordPress website, deploy an LNMP stack. For more information, see Deploy an LNMP stack on a CentOS 7 instance.

Note: You can also use the WordPress image provided in Alibaba Cloud Marketplace to build a WordPress website.

Preparations

To deploy a WordPress website on an existing ECS instance, make sure that the instance meets the following requirements:

  • The instance has a public IP address or an elastic IP address (EIP).
  • The instance runs a minor version of CentOS 7.
  • An inbound rule is added to a security group of the instance to allow traffic on port 22, port 80, and port 443. For more information, see Add a security group rule.

Note: To ensure server data security, only ports that are required to deploy and test the LNMP stack and the WordPress website are open. Open other ports based on your business requirements. For example, if you want to connect to a MySQL database that is deployed on an ECS instance, you must add an inbound rule to a security group of the instance to allow traffic on port 3306, which is the default port used for MySQL.

Build a WordPress Website

Quick Deployment

1.  Log on to the ROS console and go to the Use New Resources (Standard) page.

2.  In the upper-left corner of the top navigation bar, select a region.

1

3.  In the Configure Template Parameters step, enter a name for the stack, create or select an ECS instance, and then enter a password for the MySQL database.

To create an ECS instance, specify the zone, instance type, system disk category, and password for the instance.

Note: Make sure that the created instance is a pay-as-you-go instance that runs a minor version of CentOS 7, uses the pay-by-traffic method for network usage, and has a public IP address. For information about billing of ECS instances and public IP addresses, see Billing overview.

4.  Click Create.

If Creating changes to Created in the Status section on the Stack Information tab, WordPress is deployed.

5.  Install WordPress and log on to the WordPress website.

a) Click the Outputs tab and then click the URL next to WordPressUrl. This URL is the public IP address of the ECS instance that you use.

b) Specify basic information of the website and click Run the installation.

Configure the following parameters:

  • Site Title: the name of the WordPress website. Example: demowp.
  • Username: the username that you want to use to log on to WordPress. Make sure that your username is secure. Example: testwp.
  • Password: the password that you want to use to log on to WordPress. We recommend that you specify a strong password. Example: Wp.123456.
  • Your Email: the email address that you want to use to receive notifications. Example: username@example.com.

c) Click Install Wordpress.

d) Enter the username testwp and password Wp.123456 that you specified when you install WordPress, and then click LOGIN.

You are logged on to your WordPress website.

Manual Deployment

1.  Use the ECS console to connect to the ECS instance that is deployed with an LNMP stack and configure a database for the WordPress website.

a) Connect to the ECS instance. For more information, see Connect to a Linux instance by using a password or key.

b) Log on to MySQL.

Use the root username and enter the password to log on to MySQL. The password is the password you set for MySQL when you built the stack.

mysql -uroot -p

c) Create a database for the WordPress website that you want to build.

In this example, a database named wordpress is created for the WordPress website.

create database wordpress;

d) Create a user to manage the wordpress database to improve security.

In MySQL 5.7 and later, the validate_password plug-in is installed by default to validate the strength of passwords. You can log on to MySQL to view the password strength rules.

show variables like "%password%";

In this example, the created user is user and its password is PASSword123.

create user 'user'@'localhost' identified by 'PASSword123.';

e) Grant the user all permissions on the wordpress database.

grant all privileges on wordpress.* to 'user'@'localhost' identified by 'PASSword123.';

f) Apply the preceding settings.

flush privileges;

g) Exit MySQL.

exit;

2.  Download WordPress and move it to the root directory of the website that you want to build.

a) Download WordPress.

Run the following yum command to download WordPress. The downloaded WordPress package is automatically saved in the /usr/share/wordpress directory.

yum -y install wordpress

b) Move the WordPress package to the root directory of the website that you want to build.

mv /usr/share/wordpress /usr/share/nginx/html/wordpress

3.  Modify the configuration file of WordPress.

a) Go to the new WordPress directory and create a symbolic link to the wp-config.php configuration file.

cd /usr/share/nginx/html/wordpress
ln -snf /etc/wordpress/wp-config.php wp-config.php

b) Edit the wp-config.php file.

vim wp-config.php

c) Press the I key to enter Insert mode. Modify the MySQL-related settings based on the configured WordPress database by using the following code snippet.

The data of the WordPress website is stored by the user user in the wordpress database.

// ** MySQL settings - The information comes from your host. ** //
/** The name of the WordPress database */
define('DB_NAME', 'wordpress');

/** The username of the MySQL database */
define('DB_USER', 'user');

/** The password of the MySQL database */
define('DB_PASSWORD', 'PASSword123.');

/** The host of the MySQL database */
define('DB_HOST', 'localhost');

d) Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and exit the configuration file.

4.  Modify the configuration file of NGINX.

a) Open the NGINX configuration file.

vim /etc/nginx/nginx.conf

b) Press the I key to enter Insert mode.

Within the server braces, replace the content after root with the `root directory of the WordPress website. In this example, the root directory is /usr/share/nginx/html/wordpress.nginx

2

Within the location ~ .php$ braces, replace the content after root with the root directory of the WordPress website.nginx

3

Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and exit the configuration file.

c) Restart NGINX.

systemctl restart nginx

5.  Install WordPress and log on to the WordPress website.

a) On your on-premises machine, use a browser to access the public IP address of the ECS instance to go to the WordPress installation page.

b) Specify basic information of the website and click Run the installation.

Configure the following parameters:

  • Site Title: the name of the WordPress website. Example: demowp.
  • Username: the username that you want to use to log on to WordPress. Make sure that your username is secure. Example: testwp.
  • Password: the password that you want to use to log on to WordPress. We recommend that you specify a strong password.
  • Your Email: the email address that you want to use to receive notifications. Example: username@example.com.

c) Click Install Wordpress.

d) Enter the username testwp and password Wp.123456 that are used to install WordPress and click LOGIN.

You are logged on to your WordPress website.

Resolve the Domain Name of the WordPress Website

If you allow users to access your WordPress website by using the public IP address of the ECS instance, the security of the ECS instance is compromised. If you have a domain name or need to register a domain name for your WordPress website, perform the following steps. In this example, the domain name www.example.com is registered.

1.  Register a domain name.

For more information, see How to register a domain name on Alibaba Cloud.

2.  Apply for an Internet Content Provider (ICP) filing for the domain name.

If the website of your domain name is hosted on an ECS instance that is located in a Chinese mainland region, apply for an ICP filing.

3.  Resolve the domain name to the public IP address of the ECS instance.

Domain name resolution is a prerequisite for using domain names to access your website. For more information, see Get started with Alibaba Cloud DNS.

4.  Replace the public IP address of the ECS instance with the new domain name.

a) Connect to the ECS instance on which the WordPress website is built.

For more information, see Connect to a Linux instance by using a password or key.

b) Log on to MySQL.

mysql -uroot -p

c) Switch to the WordPress database.

use wordpress;

d) Replace the public IP address of the ECS instance with the new domain name.

update wp_options set option_value = replace(option_value, 'http://<Public IP address of the instance>', 'http://www.example.com') where option_name = 'home' OR option_name = 'siteurl';

e) Exit MySQL.

exit;

The new domain name is configured for your WordPress website.

FAQ

Question 1: Why am I unable to access a fixed URL in WordPress after specify it?

You can make a website static to help search engines index the website. Before you specify a fixed URL for a WordPress website, configure static rules in the NGINX server. Perform the following operations:

1.  Log on to the ECS instance on which the WordPress website is built.

For more information, see Connect to a Linux instance by using a password or key.

2.  Open the NGINX configuration file.

vim /etc/nginx/nginx.conf

3.  Press the I key to enter Insert mode.

4.  Within the location / code braces, add the following code snippet:

if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}

5.  Press the Esc key, enter :wq, and then press the Enter key to save and close the file.

6.  Restart NGINX.

systemctl restart nginx

Question 2: Why am I prompted for FTP logon credentials or the directory cannot be created when I try to update the version, or upload a theme or plug-in in WordPress?

Your user may not have permissions on the theme or plug-in in the WordPress configuration file. Perform the following operations:

1.  Log on to the ECS instance on which the WordPress website is built.

For more information, see Connect to a Linux instance by using a password or key.

2.  Open the WordPress configuration file.

vim /usr/share/nginx/html/wordpress/wp-config.php

3.  Press the I key to enter Insert mode.

4.  Add the following code snippet at the end of the file:

define("FS_METHOD","direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

5.  Press the Esc key, enter :wq, and then press the Enter key to save and close the file.

6.  Return to the WordPress dashboard and refresh the page. Check whether the FTP logon credential issue is solved.

If the directory still cannot be created, run the following command in the ECS instance to update the user who has permissions on the website root directory to the NGINX user. In this example, the nginx user is used.

chown -R nginx /usr/share/nginx/html/wordpress
0 1 0
Share on

Alibaba Cloud Community

1,042 posts | 256 followers

You may also like

Comments

Alibaba Cloud Community

1,042 posts | 256 followers

Related Products