All Products
Search
Document Center

Elastic Compute Service:Build a WordPress website on a CentOS 7 instance

Last Updated:Jan 30, 2024

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 topic describes how to build a WordPress website on an Elastic Compute Service (ECS) instance that runs CentOS 7.

Preparations

To build 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.

    Important

    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

The quick deployment mode leverages Alibaba Cloud Resource Orchestration Service (ROS) to help you quickly deploy the LNMP stack and the WordPress website. Compared with manual deployment, this method automates resource deployment and configuration.

  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.地域.png

  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.

    Important

    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.

    1. 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.

    2. 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.

    3. Click Install Wordpress.

    4. 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. Connect to the ECS instance.

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

  2. Deploy the LNMP stack.

    For more information, see Deploy an LNMP stack on a CentOS 7 instance.

  3. Configure the WordPress database.

    1. 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
    2. 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;
    3. Create a new 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.';
    4. Grant the user all permissions on the wordpress database.

      grant all privileges on wordpress.* to 'user'@'localhost' identified by 'PASSword123.';
    5. Apply the preceding settings.

      flush privileges;
    6. Exit MySQL.

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

    1. Download WordPress.

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

      sudo yum -y install wordpress
    2. Move the WordPress package to the root directory of the website that you want to build.

      sudo mv /usr/share/wordpress /usr/share/nginx/html/wordpress
  5. Modify the configuration file of WordPress.

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

      cd /usr/share/nginx/html/wordpress
      sudo ln -snf /etc/wordpress/wp-config.php wp-config.php
    2. Edit the wp-config.php file.

      sudo vim wp-config.php
    3. 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');
    4. Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and exit the configuration file.

  6. Modify the configuration file of NGINX.

    1. Open the NGINX configuration file.

      sudo vim /etc/nginx/nginx.conf
    2. 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.

      • Within the location ~ .php$ 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配置文件

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

    4. Restart NGINX.

      sudo systemctl restart nginx
  7. Install WordPress and log on to the WordPress website.

    1. 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.

    2. 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.

    3. Click Install Wordpress.

    4. 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.

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, you must 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.

    1. 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.

    2. Log on to MySQL.

      mysql -uroot -p
    3. Switch to the WordPress database.

      use wordpress;
    4. 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';
    5. Exit MySQL.

      exit;

      The new domain name is configured for your WordPress website.

FAQ

Why am I unable to access a fixed URL after I specify the URL in WordPress?

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.

    sudo 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 to exit Insert mode. Enter :wq and press the Enter key to save and exit the configuration file.

  6. Restart NGINX.

    sudo systemctl restart nginx

Why am I prompted for FTP logon credentials or that 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.

    sudo 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 to exit Insert mode. Enter :wq and press the Enter key to save and exit the configuration 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.

    sudo chown -R nginx /usr/share/nginx/html/wordpress