All Products
Search
Document Center

Elastic Compute Service:Build a WordPress website on a Linux instance

Last Updated:Dec 09, 2024

WordPress is a free, open-source content management system (CMS) that allows you to easily create and manage websites, including blogs, news websites, e-commerce websites, and social media websites. WordPress has a rich library of themes and plug-ins that allows you to customize the appearance and functionality of your websites. This topic describes how to build a WordPress website on Elastic Compute Service (ECS) instances that run different Linux operating systems.

Preparations

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

  • The Linux ECS instance is assigned a static public IP address (also called system-assigned public IP address or auto-assigned public IP address) or associated with an elastic IP address (EIP).

    If the Linux ECS instance is not associated with a static public IP address, associate an EIP with the instance. For more information, see Associate one or more EIPs with an instance.

  • Inbound rules are added to a security group of the Linux ECS instance to open ports 22 and 80. For information about how to add a security group rule, see Add a security group rule.

    Important

    For security reasons, only ports that are required to deploy and test the LNMP stack and the WordPress website are open. LNMP is the acronym for the names of the following open source components: Linux operating system, NGINX web server, MySQL relational database management system, and PHP programming language. You can 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 open port 3306, which is the default port for MySQL.

Procedure

Warning

The commands that you must use to build a WordPress website on a Linux ECS instance may vary based on the operating system of the instance. Use the commands that are suitable for the operating system of your Linux ECS instance. Otherwise, you may be unable to build the WordPress website. In this topic, ECS instances that run Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 7, and CentOS 8 are used to deploy WordPress 6.4.

  1. Deploy an LNMP stack. For more information, see Deploy an LNMP stack on an Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 7, or CentOS 8 instance.

    Important
    • For information about the methods for deploying an LNMP stack, see Deploy an LNMP stack. Each WordPress version requires specific PHP and MySQL versions. If the PHP or MySQL version is incompatible with the WordPress version that you want to install, WordPress may fail to be installed. For information about PHP and MySQL versions that are compatible with each WordPress version, see WordPress Compatibility.

    • In this topic, NGINX is used, which is a web server. If Apache is installed on your Linux ECS instance, WordPress is inaccessible because port 80 is used by Apache.

  2. Connect to an ECS instance.

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  3. Configure a database for WordPress.

    1. Log on to MySQL.

      Note

      Use the root user and enter the password of the user to log on to MySQL. The password is the password you configured for MySQL when you built the LNMP stack.

      mysql -u root -p
    2. Create a database named wordpress for the WordPress website that you want to build.

      create database wordpress;
    3. Create a user named user to manage the wordpress database and set the password of the user to PASSword123. to increase data security.

      create user 'user'@'localhost' identified by 'PASSword123.';
      Note
      • You can run the show variables like "%password%"; command to query the MySQL password strength rules. In this example, the following strength rules apply to MySQL passwords: The password must be 8 to 30 characters in length and contain uppercase letters, lowercase letters, digits, and special characters. Supported special characters include ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ‘ < > , . ? /

      • By default, the validate_password plug-in is installed in MySQL 5.7 and later versions to validate the strength of passwords.

    4. Grant the user named user all permissions on the wordpress database.

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

      flush privileges;
    6. Exit MySQL.

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

    1. Go to the root directory of the NGINX website and download the WordPress package.

      Note

      In the example, the Chinese version of WordPress is installed by default. To install the English version of WordPress, run the wget https://wordpress.org/wordpress-6.4.4.zip command to download the package for the English version of WordPress. Replace wordpress-6.4.4-zh_CN.zip with wordpress-6.4.4.zip in commands used in subsequent steps.

      cd /usr/share/nginx/html
      sudo wget https://cn.wordpress.org/wordpress-6.4.4-zh_CN.zip
    2. (Optional) Install unzip.

      sudo yum install unzip -y
    3. Decompress the WordPress package.

      sudo unzip wordpress-6.4.4-zh_CN.zip
    4. Copy the content of the wp-config-sample.php file in the WordPress installation directory to the wp-config.php file and use the wp-config-sample.php file as a backup.

      cd /usr/share/nginx/html/wordpress
      sudo cp wp-config-sample.php wp-config.php
  5. Modify the configuration file of WordPress.

    1. Open the wp-config.php file.

      sudo vim wp-config.php
    2. Press the I key to enter Insert mode.

      Modify the MySQL-related settings based on the configured database for WordPress as shown in the following code snippet.

      Note

      The name of the database for WordPress, MySQL database username, and MySQL database password that you specify in the WordPress configuration file must be the same as those that you configure in the Configure a database for WordPress step.

      // ** MySQL settings - Obtain the information from your host. ** //
      /** The name of the database for WordPress */
      define('DB_NAME', 'wordpress');
      
      /** The MySQL database username */
      define('DB_USER', 'user');
      
      /** The MySQL database password */
      define('DB_PASSWORD', 'PASSword123.');
      
      /** The MySQL hostname */
      define('DB_HOST', 'localhost');
      Note

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

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

  6. Modify the NGINX configuration file.

    Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7

    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.

        In this example, the root directory of the WordPress website is /usr/share/nginx/html/wordpress.

      • Within the location ~ .php$ braces, replace the content that follows root with the root directory of the WordPress website.

      Note

      When you replace the content with the root directory of the WordPress website, append a semicolon (;) to the root directory. Otherwise, NGINX cannot be restarted.

      nginx配置文件

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

    4. Restart NGINX.

      sudo systemctl restart nginx

    CentOS 8

    1. Open the NGINX configuration file.

      sudo vi /etc/nginx/conf.d/default.conf
    2. Press the I key to enter Insert mode and replace the content that follows root with the root directory of the WordPress website.

      In this example, the root directory of the WordPress website is /usr/share/nginx/html/wordpress.

      • Within the location / braces, replace the content that follows root with the root directory of the WordPress website.nginx

      • Within the location ~ \.php$ braces, replace the content that follows 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 close 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 computer, use a web browser to access http://<Public IP address of the ECS instance> to go to the WordPress installation page.

    2. Configure basic information about the WordPress 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 the 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 testwp username and the Wp.123456 password that you specified when you installed WordPress, and click LOGIN.

      You are logged on to your WordPress website. For more information about how to use WordPress, see the WordPress documentation.

Related operations

Configure a domain name for the WordPress website

If you allow users to access the WordPress website by using the public IP address of the ECS instance on which the WordPress website is built, the security of the instance is compromised. To improve security, configure a domain name for the WordPress website. If you have a domain name or want to register a domain name for the WordPress website, perform the following steps:

  1. Register a domain name.

    You can register a domain name on Alibaba Cloud. For more information, see Register a domain name on Alibaba Cloud.

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

    If the WordPress website is built on an ECS instance that is located in a region inside the Chinese mainland, apply for an ICP filing for the domain name before you use the domain name for the WordPress website. For more information, see ICP filing process.

    Note
    • You can apply for an ICP filing for the domain name of a website hosted on an ECS instance only if the instance is a subscription instance that has a subscription duration of at least three months. If the ECS instance on which the WordPress website is built does not meet the preceding requirements, perform one of the following operations:

    • The entire ICP filing process requires 1 to 22 business days to complete. The actual amount of time required may vary.

    • For the frequently asked questions about the ICP filing process, see FAQ.

  3. Resolve the registered domain name

    to the public IP address of the ECS instance. For more information, see Get started with Alibaba Cloud DNS.

    Note

    For the answers to frequently asked questions about the DNS resolution issues, see FAQ about DNS resolution issues.

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

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

      For more information, see Connect to an instance.

    2. Log on to MySQL.

      mysql -u root -p
    3. Switch to the database for WordPress.

      use wordpress;
    4. Configure the registered domain name for the WordPress website.

      Note

      Replace the following variables in the command with actual values:

      • http://<Public IP address of the ECS instance>: Replace <Public IP address of the ECS instance> with the actual public IP address of the ECS instance on which the WordPress website is built.

      • http://www.example.com: Replace this domain name with the registered domain name for the WordPress website.

      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 registered domain name is configured for your WordPress website.

FAQ

Q1: Why am I unable to access the WordPress website by using the public IP address of the ECS instance on which the WordPress website is hosted?

Possible causes and solutions:

  • Port 80 is not open in the security groups of the ECS instance, the system firewall is enabled on the ECS instance, or port 80 is used by a different service.

    For information about how to troubleshoot the issue based on the preceding causes, see What do I do if I cannot access a service deployed on an instance?

  • The PHP or MySQL version is incompatible with the WordPress version.

    Each WordPress version requires specific PHP and MySQL versions. If the PHP or MySQL version is incompatible with the WordPress version, WordPress may fail to be installed. For information about PHP and MySQL versions that are compatible with each WordPress version, see WordPress Compatibility.

Q2: Why am I unable to access a permalink after I configure the permalink in WordPress?

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

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

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  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 close the configuration file.

  6. Restart NGINX.

    sudo systemctl restart nginx

Q3: Why am I prompted for FTP logon credentials or that the directory cannot be created when I attempt to update the WordPress version or upload a theme or plug-in?

You may not have the permissions to configure themes or plug-ins in the WordPress configuration file. To resolve the issue, perform the following steps:

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

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  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 to 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 close 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 cannot be created, run the following command on the ECS instance to switch 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

Q4: How do I change the default username and password of the MySQL 5.7 database in a WordPress image?

  1. Connect to the ECS instance that uses the WordPress image.

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Log on to MySQL.

    mysql -u root -p

    Enter the default password of the root user of MySQL as prompted.

    [test@izbp1cgfkqcrs8pi2li**** ~]# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.23 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
  3. Log on to the MySQL database.

    use mysql
    Note

    To view the username of the MySQL database, run the select user from mysql.user; command.

  4. Change the default username and password of the MySQL database based on your business requirements.

    • Change the default username of the MySQL database.

      update user set user="<New username>" where user="<Username>";

      In this example, the following command is run to change the default username from root to admin:

      update mysql.user set user="admin" where user="root";
    • Change the password of the default username.

      update mysql.user set authentication_string=password("<New password>") where user="<Username whose password you want to change>";

      In this example, the following command is run to change the password of the root username to newpassword:

      update mysql.user set authentication_string=password("newpassword") where user="root";
  5. Reload the privilege tables and apply the changes.

    flush privileges; 
  6. Exit MySQL.

    quit;

References