Welcome to the fourth installment of the CentOS 7 LAMP Server Tutorial: Modernized and Explained series.In Part 1 and Part 2 we configured a LAMP server with PHP-FPM running PHP 7.3 and a modern version of MariaDB. Then we configured a single VirtualHost for hosting a website, and in Part 3 configured that VirtualHost to run securely with a free Let’s Encrypt SSL certificate.
In this bit, we’re going to put our shiny new server to practical use by installing WordPress. We’re also going to learn how to use a tool that’ll allow us to install, configure, and maintain WordPress right from the command line.
The WordPress Command Line Interface
As its name clearly states, the WordPress Command Line Interface (or wp-cli) is a command line based administration for WordPress. Why would you want such a thing? Good question!
Linux server administrators are known for preferring to do things without having to leave the command line. Switching back and forth between a command line to a GUI or web interface is seen as inefficient at best. But there’s more to it than just basic efficiencies. The command line interface can be automated with shell scripts that allow us to do otherwise tedious tasks without ever having to click a mouse button.
Furthermore, there are tasks that can be done via wp-cli that would otherwise require editing the WordPress database directly or by using third party plugins. Password resets, database search and replaces, and much much more are available. Let’s get it installed!
Installing wp-cli
Before we install wp-cli, we need to satisfy one pre-requisite. In the first article in this LAMP tutorial series, we set up PHP 7.3 to work from the command line with
scl enable php73 bash
What we didn’t do was set up the associations needed so that PHP 7.3 would work the same from the command line as via Apache. In its current state, PHP cannot access MySQL databases. Let’s resolve that now. Paste in the following commands:
echo #!/bin/bash >> /etc/profile.d/php73.sh echo source /opt/remi/php73/enable >> /etc/profile.d/php73.sh echo export X_SCLS="`scl enable php73 'echo $X_SCLS'`" >> /etc/profile.d/php73.sh
Now we’re good to go. Installing wp-cli couldn’t be simpler. Log in to your server as root and paste in the following commands:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar mv wp-cli.phar /usr/local/bin/wp
You can test your new wp-cli installation by simply typing the command “wp” at the command line:
The help documentation will be loaded inside of a Linux program called less so that you can scroll through it with the up and down arrows on your keyboard. Go ahead and take a look at the page and get a feel for the capabilities that wp-cli has. Press q to exit.
Installing WordPress on our VirtualHost
You’ll recall that we created a VirtualHost called lowend-tutorial.tld with the username “lowend”. We’re now going to create a database that matches that username for no other reason that to associate the two in our heads. We’re also going to create a MySQL user with a unique password so that the database can be accessed by a non-root user. This is an extremely important step. Your databases should never be accessed as root! You’ll want to substitute your own information of course.
Creating the Database and User
To create the database, we’re going to need to switch to the MySQL (MariaDB) interface by simply typing
mysql -u root -p
We’re logging in as root here because only the root user can add databases and database users. Edit the following command to match your own username and chosen password, and paste it in:
create database lowend; GRANT ALL PRIVILEGES ON lowend.* TO 'lowend'@'localhost' IDENTIFIED BY 'Your-DB-Password'; flush all privileges;
In these three commands we created the database, created a user, and gave that user full access to the “lowend” database and all its tables. Then we flushed the privileges to enact the changes. This is how it looked on our VPS:
Make sure to keep track of the database name, username, and password. We’ll need it to install WordPress. Exit the MySQL interface by simply typing “exit” and pressing Enter.
Installing WordPress with wp-cli
The typical method of installing WordPress involves downloading the files to the public_html directory and then going to the website in a browser to finish the installation. The wp-cli program lets us skip all of that and perform the installation all from the command line.
First, switch to your VirtualHosts user and then to its public_html directory.
su - lowend cd ~/public_html/
This is the same public_html folder that we used to verify that PHP was working in Part 2 with the “test.php” file. If that file still exists, it would be good to remove it.
rm test.php
With the public_html directory now empty, we’ll begin the WordPress installation. First we need to use wp-cli to download the core WordPress files:
wp core download
If you now type “ls” you will see all of the core WordPress files. Now we need to configure WordPress. WordPress’ main configuration file is wp-config.php, and it contains all of the information that WordPress needs to talk to the database, and more. You’ll see that it doesn’t exist yet. We’re going to use wp-cli to create it and then change its permissions so that they are correct. Edit the following command to match your configuration, then paste it in:
wp core config --dbhost=localhost --dbname=lowend --dbuser=lowend --dbpass=Your-DB-Password chmod 644 wp-config.php
Now WordPress knows how to talk to its database, but that database is still empty. WordPress now needs an identity and its first administrator. We’re going to configure those using wp-cli and set some permissions. Again, edit the following so that it matches your own configuration, and paste it in. Be sure to use your real email address and set a strong password! You can be sure that as soon as you get WordPress installed, there will be bots trying to log into it. It’s also recommended that you do not use “wordpress_admin” as your username for the administrative user. Choose something unique.
wp core install --url=https://lowend-tutorial.tld --title="LowEnd Tutorial Site" \ --admin_name=wordpress_admin --admin_password=Your-DB-Password \ --admin_email=your-email-address@gmail.com chmod 755 wp-content/uploads
Here is how this looked on our server:
Now, we can test to see our new website is working by simply visiting it in a browser: https://lowend-tutorial.tld. Check it out and come back when you’re done.
Does it work? Great! If not, check for any errors along the way, and examine them for any typos or errors.
It’s time to get logged in by going to https://lowend-tutorial.tld/wp-admin. Log in using the username and password you created with wp core install. Once you’re logged in, navigate to Settings > Permalinks > select Post name, and then Save Changes as shown below:
Changing the Permalinks structure (the way that WordPress maps a URL to a page or blog post) finishes things off by writing the .htaccess file that’s needed for many other WordPress functions. Reconfiguring the permalinks can be done via wp-cli, but when we tested it, it didn’t write the .htaccess file. Your WordPress installation is now ready to be turned into an awesome website. Well done!
Wait, that’s it?
Yep, you did it! You built a LAMP server with PHP 7.3, secured it with an automatically renewing Let’s Encrypt SSL certificate, and installed WordPress. Be assured that we aren’t just stopping here, however.
A few more things…
In this series, you installed one user, one VirtualHost, one SSL certificate, and one website. But, if you create a second user, a second VirtualHost, and a second PHP-FPM configuration file, you can host a second website on your server! And a third, and a fourth, and… as many as your server can handle! The process can even be automated with some simple shell scripts.
Next up we’ll be doing fun things like configuring Redis and then switching over to Nginx instead of Apache. You’re ready to continue Part 5: Speeding up WordPress with Redis
Related Posts:
- CentOS 7 LAMP Server Tutorial Part 6: Moving to NGINX - July 15, 2019
- CentOS 7 LAMP Server Tutorial Part 5: Speeding up WordPress with Redis - July 1, 2019
- CentOS 7 LAMP Server Tutorial Part 4: WordPress and wp-cli - June 15, 2019
Leave a Reply