WordPress with LEMP on Alibaba Cloud - Part 4 Installing WordPress on Your Alibaba Cloud ECS Instance-Alibaba Cloud Developer Community

By Jeff Cleverley, Alibaba Cloud Tech Share Author

In the previous tutorials we secured an Alibaba Ubuntu 16.04 ECS instance, then installed NGINX, MariaDB, and PHP7 to complete our LEMP stack. After that, we configured our DNS and nameservers so our site is now served with a domain name and protected with a Let's Encrypt SSL certificate.

Now we are ready to install WordPress!

Completing the previous tutorials is a prerequisite for this one, so if you haven't completed them yet, then follow the links above and come back here when you're done.

Step 1. Configure NGINX for WordPress

The First thing we need to do is make some slight modifications to our NGINX server blocks. Open the default NGINX configuration file with sudo privileges:

$ sudo nano /etc/nginx/sites-available/default

Within the server block we need to add some locations blocks.

Turn off logging requests for /favicon.ico and /robots.txt . Then use a regular expression to match requests for static files, then turn logging off for these requests too, and mark them as highly cacheable since they are generally expensive resources to serve. If you have any other static files you want to cache add them here too:

server {                    
    . . .                                
    location = /favicon.ico {                 
        log_not_found off;                 
        access_log off;                     
    location = /robots.txt {                 
        log_not_found off;                     
        access_log off;                         
        allow all;                         
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {    
        expires max;                            
        log_not_found off;                        
    . . .                

We also need to make some adjustments inside the location / block.Comment out the try_files list line, where it returns a 404 error as a default option, and add code that passes control to the index.php file with the request argument instead:

server {        
    . . .        
    location / {                                
        #try_files $uri $uri/ =404;                
        try_files $uri $uri/ /index.php$is_args$args;
    . . .        

When you are finished your NGINX configuration file should look like the following:

<Adjust NGINX configuration file for WordPress>

Make sure to check for syntax errors:

$ sudo nginx -t

If there are none, reload NGINX:

$ sudo systemctl reload nginx

Step 2. Install PHP Extensions most commonly used with WordPress

Previously we only installed the minimum PHP extensions required to get PHP to communicate with MariaDB and NGINX. However, WordPress and its myriad ecosystem of plugins often leverage additional PHP extensions for increased functionality. We will install some of the most popular ones next.

This step is optional, you could install these when you need them, but since we are already tweaking the LEMP stack for WordPress, why not do these now?

First, update your package repository index, and then install the chosen extensions by entering:

$ sudo apt-get update
$ sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Once they are finished installing, we need to restart the PHP-FPM process so these new features can be leveraged:

$ sudo systemctl restart php7.0-fpm

Now PHP and NGINX are ready, we just need to add our Database with MariaDB and we can move on to WordPress.

Step 3. Create a MySQL Database and User for WordPress

WordPress needs a MySQL Database to store site and user information. In the previous tutorial, we installed MariaDB as a drop in, more performant, replacement for MySQL. Now we need to use MariaDB to create the Database and User for WordPress to use.

Log in to MariaDB, with root privileges, using the following mysql command:

$ sudo mysql -u root -p

As usual, you will need to enter your superuser password before you can progress.Now create a Database for WordPress. Name it appropriately, for this tutorial I will be sticking with wordpress as my database name:

MariaDB [(none)]> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Next, we need to create the user for this database. This user will be exclusive to our new database. I'm just going to use the same username as I have for my superuser to keep things simple. You should choose whatever username you think is appropriate. I am also using a simple password for the benefit of the tutorial, but yours should be much more secure:

MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'new_user'@'localhost' IDENTIFIED BY 'new_users_password';

Now we have the necessary Database and User account, all we have to do is flush the privileges so that MariaDB knows about the changes we've made. Then exit MariaDB:

MariaDB [(none)]> EXIT;

If you have followed along exactly, then your terminal should look like this:

<Create a MariaDB Database and User for WordPress>

Step 4. Configure root directory ownerships

We will be setting up WordPress in the /var/www/html directory, as set up in our NGINX configuration file server block. Change directory to this directory:

$ cd /var/www/html

If we try to import the WordPress files in this directory immediately, then we will receive a permissions error. That is because this directory currently belongs to root user in the root group.

You can verify this by entering the following command to show the owner:

$ ls -l

Your terminal will display the file and directory permissions, owner, date, time, and contents. You should see root root in the output. We will need to change the owner of the directory and the group the directory belongs to. This is necessary to allow us to download the files that we need into this directory.

First, with root privileges, add your superuser to the www-data usergroup, this is the NGINX server's usergroup. In my case that is new_user:

$ sudo usermod -aG www-data new_user

Next, change the ownership of the /var/www/html directory to your superuser and the www-data usergroup. In the case of my new_user that would be the following command:

$ sudo chown -R new_user:www-data /var/www/html

Once this is done you can check the ownership of the directory again with the same command as previously:

$ ls -l

Now the terminal should show the directory as belonging to your superuser and the www-data usergroup. If everything has been completed correctly, your terminal should look like the following:

<Change ownership of the directory for WordPress>

We will be revisiting this directory and its ownership once we have installed WordPress, as it currently stands we won't be able to install themes and plugins once WordPress is up and running. We can do that later.

Installing WordPress

This is where you have a decision to make as this tutorial is going to show you two different ways to install WordPress.

The first is the original way, using curl and editing config files using nano etc. It is a trusty and reliable method, and worth going through the process incase you need to fall back to this method in future.

The second is using WP CLI, the awesome command line interface for WordPress, if you haven't used it before then this is your chance to start. It's an incredible time saver and, once you get used to it, you can use it to manage all your WordPress sites with a workflow that offers such increased speed and efficiencies that you will never look back.

Step 5. Install WordPress using CURL

Step 5a. Download WordPress

First change into a writable directory, we previously took ownership of the WordPress destination directory so this isn't, strictly speaking, necessary other than as a safety precaution:

$ cd /tmp

Now use curl to download the compressed release with the following command:

$ curl -O https://wordpress.org/latest.tar.gz

After that extract the compressed file to create the WordPress directory and structure:

$ tar xzvf latest.tar.gz

Next, we need to change the wp-config-sample.php file into the wp-config.php filename needed by wordpress, use the copy command like so:

$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

We also need to create the upgrade directory needed by WordPress, so that it won't run into permission problems later when it tries to do this on it's own:

$ mkdir /tmp/wordpress/wp-content/upgrade

Okay, now we have a correctly configured and structured WordPress directory in the /tmp directory, we can copy its entire contents into our html root directory.

We will use the -a flag to maintain permissions. Notice the . period at the end of our source directory, this indicates that everything in the directory should be copied, including hidden files:

$ sudo cp -a /tmp/wordpress/. /var/www/html

Step 5b. Set up the WordPress Configuration File

We need to configure some settings in the wp-config.php file so WordPress can talk to our database, and also to secure the installation.

Create and open the configuration file:

$ nano /var/www/html/wp-config.php

Enter the settings for the database we created with MariaDB earlier. You will need to correctly enter the name of the database, the database user, database user password and localhost, from earlier:

. . .                                        
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');               
/** MySQL database username */                
define('DB_USER', 'new_user');                
/** MySQL database password */                
define('DB_PASSWORD', 'new_users_password');
/** MySQL hostname */                        
define('DB_HOST', 'localhost');                
. . .                                        

Close the file and save it.

Your wp-config.php should look similar to the following screenshot:

<Add your Database settings to wp-config.php>

The wp-config.php file will also contain a section for secure 'salt' keys. It will look like this:

. . .                        
define('AUTH_KEY',         'put your unique phrase here');    
define('SECURE_AUTH_KEY',  'put your unique phrase here');    
define('LOGGED_IN_KEY',    'put your unique phrase here');    
define('NONCE_KEY',        'put your unique phrase here');    
define('AUTH_SALT',        'put your unique phrase here');    
define('SECURE_AUTH_SALT', 'put your unique phrase here');    
define('LOGGED_IN_SALT',   'put your unique phrase here');    
define('NONCE_SALT',       'put your unique phrase here');    
. . .                        

You will need to replace these keys with secure key values. We can get them from the WordPress secret key generator with the following command:

$ curl -s https://api.wordpress.org/secret-key/1.1/salt/

Your terminal will display some unique values that look like the following:

salt keys with the WordPress salt key generator>

Copy the salt keys. Open your wp-config.php file, and paste these keys in place of the placeholder values:

$ nano /var/www/html/wp-config.php

<Copy and paste the secure salt keys into your wp-config.php>

Make sure you close and save the wp-config.php file. Now you're ready to complete the installation.

Step 5c. Complete the Famous WordPress 5 Minute Installation through the web interface

The server configuration is complete and the WordPress directory and Configuration file is prepared, so now you can complete WordPress's famous 5 minute installation by visiting your domain.

Open your browser and navigate to your domain url:


You will need to select your installation language, and then you'll be prompted to complete your site and admin user information:

<Complete the famous WordPress 5 Minute installation>

Click to install WordPress, and you are done:

<WordPress installed the Old Skool way!>

Your WordPress site should be up and running, and secured with an SSL.

Permissions will need some adjusting to allow you to install and delete plugins and themes, and make other WordPress configuration changes, but we can do that later.

Next we shall install WordPress using the fantastic WP CLI.

Step 6. Installing WordPress using WP-CLI

Step 6a. Install WP-CLI

You can download the WP-CLI PHP Archive file wp-cli.phar using either curl or wget commands:

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar


$ wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Next, you need to make the wp-cli.phar file executable, and move it to the /usr/local/bin directory, so that it can be run directly:

$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp

When these commands are completed, your terminal should look like the following:

<Install WP-CLI>

You can check that it has been installed correctly by checking its info:

$ wp --info

If you see something similar to the following, you can proceed:

<Check wp --info to ensure WP-CLI installed correctly>

You will want to activate Bash completion because this allows you to see all the available WP-CLI commands on the fly.

First, download the bash script into your home directory:

$ cd ~/
$ wget https://github.com/wp-cli/wp-cli/raw/master/utils/wp-completion.bash

Next open the .bashrc file:

$ nano .bashrc

Add the following line of code at the end of the .bashrc file:

. . .
source /home/$USER/wp-completion.bash

In the nano editor, the file should look like the following:

shell loads it every time you login>

Close and save the file, then run the following command to reload the bash profile:

$ source ~/.bashrc

Bash completion is now enabled, you can test this by typing wp theme , including the trailing space, and then press tab twice. You will see a list of available commands with the wp theme prompt.

Your terminal should be showing:

<Test Bash completion>

Step 6b. Install WordPress using WP-CLI

Move to the /var/www/html root directory for the site:

$ cd /var/www/html

Since we will be creating a set up with only one WordPress installation on the server, this is where we will download the WordPress files.

If your server will host several WordPress applications, then you would create separate folders as a root for each installation. This would also require some reconfiguration of the NGINX set up, this will covered in a different tutorial.

We took ownership of the /var/www/html root directory previously, so we shouldn't run into any errors while downloading the files and unpacking them.

A full list of the WP-CLI commands can be found here.

You can use the wp core download command to download WordPress core files into the directory:

$ wp core download --locale=en_GB

As you can see, I am downloading core WordPress with the British Language pack. You can use whichever language extension you require.

Now we need to create our wp-config file using our database values:

$ wp core config --dbname=wordpress --dbuser=new_user --dbpass=new_users_password --dbhost=localhost --dbprefix=wp_

Obviously you should use the database values that you created with MariaDB earlier, and not the above.

Also notice how we don't need to bother with the WordPress Salt Key Generator, that's because WP-CLI does that for us.

Finally, all we need to do is run the installation with the following command:

$ wp core install --url="https://an-example-domain.com" --title="New Skool WP-CLI Installation" --admin_user="new_user" --admin_password="some_password" --admin_email="email@domain.com"

Again, make sure to use the values for your domain, admin username, password, and email. Your terminal should look something like this:

<Install WordPress with WP-CLI>

And that's it, you have a new WordPress installation created using WP-CLI. How easy was that?

<WordPress is not installed New Skool - WP-CLI FTW!>

You can use WP-CLI to update WordPress core, plugins, themes, copy whole installations, and databases, add admin users so you never get locked out, and so much more. But that is for another article.

We aren't finished yet though, remember those permissions?

Step 7. Configure Root Directory Permissions for WordPress functions

Log in to your WordPress installation, go to the Plugins menu, and try to delete one of the preinstalled plugins.

You should see a pop up box like the following:

<Directory permissions problem - WordPress needs FTP credentials to function properly>

You could configure the SFTP account and credentials. While that would work, it is not ideal.

Better to change the ownership of the WordPress root directory to the www-data NGINX server user and usergroup so that our installation has full access to the filesystem it needs.

Issue the following command:

$ sudo chown www-data:www-data /var/www/html/

You can check that the www-data user and usergroup has taken ownership with the following command from within the domain root directory:

$ ls -l

You should see the following output:

<Set ownership of root directory to server user/usergroup>

Now you should be able to install and delete plugins, themes, and upload media:

<Now WordPress has access to the web server>

So now we have a fully functional WordPress site&hellip; . hmmmmm well, actually, it isn't quite functional yet, is it?

One of the things with cloud hosting, with any provider, is that while you get a fast server you don't have things like email functionality automatically configured and provided by the hosting provider.

Right now, your WordPress site can't send the transactional emails it requires to be fully functional.

But don't worry, Alibaba Cloud has you covered with their DirectMail transactional email service. I will cover how to set that up and configure it to work as your WordPress SMTP transactional mail service in another tutorial.

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now