Build a WordPress website (Linux)

更新时间:
复制 MD 格式

For full control over your website, manually deploy an LNMP stack and install WordPress. This approach allows you to build a blog, portal, or other types of websites.

Prerequisites

  • Enable public access: Assign a public IP address or associate an elastic IP address (EIP) with the instance.

  • Add a security group rule: Allow inbound traffic over TCP on ports 22 and 80 in the security group of your instance.

Procedure

The following steps show how to deploy WordPress 6.4.4 on an Alibaba Cloud Linux 3 instance.

Step 1: Deploy the LNMP stack

  1. Log on to an ECS instance.

    1. Go to ECS console - Instances. In the top-left corner, select the region and resource group for the target instance.

    2. Navigate to the details page of the target instance. Click Connect and select Workbench. Follow the on-screen prompts to access the terminal.

  2. Deploy the LNMP stack.

    Install core components such as Nginx, MySQL, and PHP.

    WordPress depends on specific versions of PHP and MySQL. A version mismatch will cause the installation to fail. For details, see WordPress Compatibility.

Step 2: Create a WordPress database

Create a dedicated database and database user for WordPress.

  1. Log on to MySQL. Use the password you set for the database when you deployed the LNMP stack.

    mysql -u root -p
  2. Create a database and user, and grant privileges.

    Make a note of the database name, username, and password for later use.

    -- Create the WordPress database.
    CREATE DATABASE WORDPRESS_DATABASE;
    
    -- Create a dedicated user and set a password. We recommend a strong password of at least 12 characters, containing uppercase and lowercase letters, numbers, and special symbols.
    CREATE USER 'WORDPRESS_USER'@'localhost' IDENTIFIED BY 'WORDPRESS_PASSWORD';
    
    -- Grant this user all privileges on the WordPress database.
    GRANT ALL PRIVILEGES ON WORDPRESS_DATABASE.* TO 'WORDPRESS_USER'@'localhost';
    
    -- Flush the privileges to apply the changes.
    FLUSH PRIVILEGES;
    
    -- Exit MySQL.
    EXIT;
    In MySQL 5.7 and later, the password strength validation plugin is installed by default. Passwords must be 8 to 30 characters long and contain uppercase letters, lowercase letters, digits, and special characters. Supported special characters include ()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/.

Step 3: Download and configure WordPress

  1. Go to the Nginx web root directory and download the Chinese version of WordPress 6.4.4.

    To install the English version, replace the URL with https://wordpress.org/wordpress-6.4.4.zip. In later steps, replace the archive name with wordpress-6.4.4.zip.
    sudo cd /usr/share/nginx/html
    sudo wget https://cn.wordpress.org/wordpress-6.4.4-zh_CN.zip
  2. Install unzip and extract the WordPress archive.

    sudo yum install unzip -y
    sudo unzip wordpress-6.4.4-zh_CN.zip
  3. Copy the wp-config-sample.php file to wp-config.php, keeping the original as a backup.

    cd /usr/share/nginx/html/wordpress
    sudo cp wp-config-sample.php wp-config.php
  4. Edit the configuration file and enter the database name, username, and password you created in Step 2.

    sudo vim wp-config.php

    Press the i key to enter Insert mode.

    /** The name of the database for WordPress */
    define('DB_NAME', 'WORDPRESS_DATABASE');
    
    /** Database username */
    define('DB_USER', 'WORDPRESS_USER');
    
    /** Database password */
    define('DB_PASSWORD', 'WORDPRESS_PASSWORD');
    
    /** Database hostname */
    define('DB_HOST', 'localhost');

    After you make the changes, press the Esc key, enter :wq, and then press Enter to save and exit.

Step 4: Configure Nginx

Modify the default Nginx site configuration to correctly handle PHP requests.

  1. Back up the default configuration and create a new Nginx configuration file.

    sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
    sudo vim /etc/nginx/conf.d/default.conf
  2. Modify the Nginx configuration file.

    Press the i key to enter Insert mode. In the server and location ~ .php$ blocks, replace the value after root with the WordPress root directory. In this example, the WordPress root directory is /usr/share/nginx/html/wordpress.

    server {
        listen       80;
        server_name  localhost;
        root /usr/share/nginx/html/wordpress;
        location / {
        index index.php index.html index.htm;
        }
        location ~ .php$ {
            root /usr/share/nginx/html/wordpress;
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

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

  3. Check the configuration syntax.

    If the output shows syntax is ok, you can safely restart the service.

    sudo nginx -t
    If a syntax error is reported, you can restore the default configuration by running sudo mv /etc/nginx/conf.d/default.conf.bak /etc/nginx/conf.d/default.conf.
  4. Restart Nginx to apply the configuration.

    sudo systemctl restart nginx

Step 5: Install and log in to WordPress

After completing the server-side configuration, you must complete the WordPress setup in your web browser.

  1. In a web browser on your local computer, go to http://<Public IP address of your ECS instance> to access the WordPress installation page.

  2. Enter your site information, including Site Title, administrator User Name, Password, and Your Email, and then click Install WordPress.

  3. After the installation is complete, click Log In, and log in with the username and password you set in the previous step.

    A successful login confirms that your WordPress site is running. For more information, see the official WordPress documentation.

Next steps

Manage files with FTP

To upload WordPress themes or plugins using FTP, you first need to build an FTP site (Linux).

Configure a domain name and HTTPS

Accessing your site via a public IP address is insecure and not recommended for production environments.

  1. Register a domain name and complete an ICP filing

  2. Configure DNS records

    Point your domain name to the public IP address of your ECS instance.

  3. Replace the instance public IP address with your new domain name.

    1. Remotely connect to the instance and log on to the MySQL database.

      mysql -u root -p
    2. Switch to the WordPress database and set the domain name.

      Replace PUBLIC_IP with your instance's public IP address and DOMAIN with your domain name.

      use WORDPRESS_DATABASE;
      UPDATE wp_options SET option_value = replace(option_value, 'http://PUBLIC_IP', 'http://DOMAIN') where option_name = 'home' OR option_name = 'siteurl';
      EXIT;
  4. Configure an SSL certificate (HTTPS) 

    Deploying an SSL certificate enables HTTPS encrypted data transfer for your website, to protect user privacy, enhance browser trust, and improve SEO rankings.

FAQ

Can't access WordPress with public IP?

  • Check the security group: Confirm that port 80 is allowed in the instance's security group rules.

  • Check the firewall: Make sure the operating system's internal firewall (like firewalld) is not blocking port 80.

  • Check service status: On the ECS instance, run sudo systemctl status nginx and sudo systemctl status php-fpm to ensure the services are in an active (running) state.

  • Check port listening: Confirm that a service is listening on port 80.

For detailed troubleshooting methods, see Troubleshoot issues when a service on an ECS instance is inaccessible.

"Not Found" error with permalinks?

Using static-like URLs improves search engine optimization. Before setting permalinks in WordPress, you must first configure a rewrite rule on your Nginx server.

  1. Connect to your instance and open the Nginx configuration file.

    sudo vim /etc/nginx/conf.d/default.conf

    Press the i key to enter Insert mode. Inside the location / block, add the following line.

    try_files $uri $uri/ /index.php?$args;

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

  2. Restart the Nginx service to apply the changes.

    sudo systemctl restart nginx

FTP or permission errors during updates?

This issue is typically due to insufficient file permissions for the WordPress configuration, themes, or plugins. Follow these steps to resolve it.

  1. Log on to your ECS instance and open the WordPress configuration file.

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

    Press the i key to enter Insert mode and add the following lines at the end of the file.

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

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

  2. Return to your WordPress dashboard and refresh the page. This action should resolve the FTP credential prompt.

    If you still see a "could not create directory" error, update the owner of the web root directory to the Nginx user, which is nginx.

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

Change default MySQL user and password?

  1. Remotely connect to your ECS instance and log on to the MySQL database.

    mysql -u root -p
  2. Switch to the mysql database and view the current usernames.

    use mysql;
    select user from mysql.user;
  3. Change the database username and password.

    • Change the database username.

      UPDATE user SET user='NEW_USER_NAME' WHERE user='USER_NAME';

      For example, to change the username from root to admin:

      UPDATE mysql.user SET user='admin' WHERE user='root';
    • Change the user's password.

      ALTER USER 'USER_NAME'@'localhost' IDENTIFIED BY 'PASSWORD';

      For example, to change the password for the root user to Password@2025!:

      ALTER USER 'root'@'localhost' IDENTIFIED BY 'Password@2025!';
  4. Flush the privileges to apply the changes, then exit the MySQL database.

    FLUSH PRIVILEGES; 
    EXIT;

Related topics