How to Install WordPress using a Managed Database on CentOS 7

WordPress is a simple content management system based on PHP and MySQL. It is estimated that about one-third of the sites on the internet run on WordPress. WordPress has many features which simplify the setup and customization of a website or blog, which is part of what makes it so popular. In addition to it’s ease of use, there are many plugins that can add additional features. This guide will walk you through a quick and easy install of WordPress, but in this case we’ll be using a SpinUp Database separate from our web server, to allow us to scale easily in the future.

Prerequisites: 
• A Cloud Server Running CentOS 7
• SSH Access as root or equally privileged user

 


Create the SpinUp Database

1) When logged into the SpinUp control panel, select ‘DATABASES’ from the left hand panel and click ‘Create’ on the next page.

2) You should now see a page with many fields and options for creating the database. For the configuration of WordPress, we recommend using MySQL 5.7, but all other options are subject to your needs. The more traffic your site will receive, the larger your database may need to be. Finish filling out the require information and press ‘SPINUP SINGLE INSTANCE’ at the bottom of the page.

3) Your database should now build and enter an ‘Active’ status when it has finished. Take note of the IP address provided, this is the IP address that we’ll direct the web server to to connect to the database.

4) At the bottom of the page you have the tab ‘Databases’. Under this tab we’ll need to create the individual databases that our WordPress site will connect to. For this article, we’ll create a database called ‘wordpress’. 

5) Next to ‘Databases’ there should be an option for ‘Users’. Select this and create a user for your database. This is the user wordpress will use to log into the database. For this guide, I’m using the username ‘wpadmin’.


Configure Web Server

This article assumes that while you have SSH access as root to your CentOS 7 VM, you’ve not made any other changes. So we’ll be going through the process of creating a sudo user, setting up LAMP, installing and configuring WordPress, and adjusting our firewall rules.

Creating the sudo user

Sudo users are beneficial because they would allow normal users to operate within the server as if they were a root user. This section of the guide will take you through setting up a sudo user.

1) Log into your server as the root user over SSH and run the following command to create a new user. Replace ‘spinupbob’ with the username you want created.

# adduser spinupbob

2) Next, run the following command to set the password for our newly created user:

# passwd spinupbob

You’ll be prompted twice for the password, and like when you log into SSH, you’ll see no feedback for characters entered as the password.

3) Once you are returned to the command line, it’s time to add our new user to the group which has sudo privileges. In CentOS there is always a ‘wheel’ group that provides sudo privileges to any users within it.

# usermod -aG wheel spinupbob

4) Now, you can use the su command to switch to our new user so that we can test their sudo access:

# su - spinupbob

You can try running a command now, but when using a sudo user, you will have to prepend all your commands with ‘sudo’. Example:
 

# sudo ls -lah

If this is the first time you’ve used sudo in a session, you’ll be required to enter your password for the account you’re sudo’ing as. Once you’ve entered the password the command should proceed and provide the output.


Setting up the LAMP stack

LAMP is an acronym for Linux, Apache, MySQL, PHP. These are all requirements for a WordPress server, and this part of the guide will go through installing and making the necessary configurations.

1) First, we will need to enable the Remi repository. The Remi Repo is a repository maintained by Remi Collet, and is used to provide a variety of up-to-date packages. Run the following command to install and enable the repository within our server.

# sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

2) WordPress recommends that we use PHP version 7.3 or above, so we will adjust our remi-repo to exclude the older php version(s). The easiest way to do this is to utilize the yum-utils package, which allows easy changes to our repos. Run the following commands to install yum-utils, and then disable php54 in favor of php73:

# sudo yum install yum-utils
# sudo yum-config-manager --disable remi-php54
# sudo yum-config-manager --enable remi-php73

3) Now that we’ve adjusted our repository, we can download all the software required for LAMP and php features that WordPress will require. The following command strings together a list of packages we’ll install.

# sudo yum install httpd mariadb mariadb-server php php-common php-mysql php-gd php-xml php-mbstring php-mcrypt

You’ll be prompted a few times to enter Yes(y) or No(n) as the packages are installed. Press Y to install them.

4) Now that the installations have finished, we’ll start up and secure our MySQL installation. Use the following commands to start and then begin the securing:

# sudo systemctl start mariadb
# sudo mysql_secure_installation

After running mysql_Secure_installation, you’ll be prompted with instructions for configuring your MySQL install. It will first ask for a root password, and since this is our first time running MySQL we won’t have one, so you can press ENTER and it will prompt you for whether you’d like to add a password for the root user, which is recommended. Continue following along with the instructions in the prompts until you’re returned to the normal server command prompt and then run this command to ensure that MySQL starts automatically on boot up:

# sudo systemctl enable mariadb

5) Open the MySQL port in your firewall so that we can establish a connection with your Cloud Database later:

# sudo firewall-cmd --add-service=mysql --permanent

6) We’ll also need apache, our web service, running in order to display our WordPress site and accept our connection. Use the following commands to start apache and set it to start on boot:

# sudo systemctl start httpd
# sudo systemctl enable httpd

Now our LAMP stack is all setup and ready to serve WordPress once we install it.


Installing and Configuring WordPress

1) To install WordPress, we first need to download the package that contains all the necessary files. It’s recommended that we move to our temporary directory to download the tar file. We’ll then use the wget command to download the package from the URL provided by WordPress:

# sudo cd /tmp
# sudo wget http://wordpress.org/latest.tar.gz

2) Now that we’ve downloaded the package, we will want to extract it and place it in our apache web directory:

# sudo tar -xvzf latest.tar.gz -C /var/www/html

3) Now that we’ve created a wordpress folder at /var/www/html/wordpress, we will want to allow apache ownership of that folder. We’ll use this command to make that change:

# chown -R apache /var/www/html/wordpress

4) While configuring a vhost to serve WordPress is not required, it will it easier to add more sites in the future if necessary. To setup our vhost, we’ll have to open the /etc/httpd/conf/httpd.conf file with a text editor. Once the file is opened, add the following code to the end of your file, but be sure to change the fields to match your own site’s hostname and name log files. In our case, we’ll be using wordpress.spinup.com as the example.


  ServerAdmin username@wordpress.spinup.com
  DocumentRoot /var/www/html/wordpress
  ServerName wordpress.spinup.com
  ServerAlias wordpress.spinup.com
  ErrorLog /var/log/httpd/wordpressspinup-error-log
  CustomLog /var/log/httpd/wordpressspinup-acces-log common

Be sure to save the file upon exit.

5) Now open port 80 for apache so that it can listen for requests over the web:

# sudo firewall-cmd --add-service=http --permanent

Configuring the WordPress Install

Our final task is to configure our WordPress install to use our SpinUp Database. The following steps will quickly guide you through this.

1) Navigate to the wordpress directory:

# sudo cd /var/www/html/wordpress

2) Within this directory, WordPress provides a sample config file, but the easiest thing to do is just to rename it and then edit it to be a working config file. The following command will rename the config file:

# sudo mv wp-config-sample.php wp-config.php

3) Now we need to open the wp-config.php file with our text editor of choice. Once the file is opened you’ll see a following block of code:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

/** MySQL database username */
define( 'DB_USER', 'username_here' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

We’re interested in changed the ‘database_name_here’, ‘username_here’, ‘password_here’, and ‘localhost’ of this block of code. ‘database_name_here’, ‘username_here’, and ‘password_here’ need to be replaced with the matching values that you used when creating your SpinUp database. The ‘localhost’ will need to be replaced with the IP or hostname of your database. 

Once these changes are made, it should resemble something like this:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wpadmin' );

/** MySQL database password */
define( 'DB_PASSWORD', 'Password!' );

/** MySQL hostname */
define( 'DB_HOST', '12.34.56.78' );

Also, add the following lines to the the file as well, and replace http://example.com with your site’s URL. This will prevent you receiving 404 not found and WordPress trying to redirect you to using your server’s IP address.

define('WP_SITEURL', 'http://example.com');
define('WP_HOME', 'http://example.com');

Save the file upon exit. 

The final thing we’ll do is restart apache, and reload our firewall rules:

# sudo systemctl restart httpd
# sudo firewall-cmd --reload

Now, when you navigate to http://your.ip.address.here/wp-admin/ you should be greeted with with a WordPress Login page. Enter the username of the user you created on your server, and their password. You should now be passed into the WordPress Dashboard where you can begin configuring and customizing your WordPress site.

Apache vhost Setup on CentOS 7

A vhost stands for virtual host. Vhosts are used to serve multiple domains without the need for additional Internet Protocol (IP) addresses. With vhosts, the different pages are displayed according to what has been set in the host file for the particular site requested. With vhosts, you can add as many sites as you need to your server without needing additional IP addresses. This article provides instructions for creating vhosts on CentOS® specifically.

In this article, we’ll be using the placeholder of ‘domain.com’, but you’d want to replace this with whatever domain you’re setting your vhost up for.

Prerequisites:
• A CentOS 7 Server
• Apache installed (sudo yum install httpd)
• DNS pointing the site to the server’s IP
• SSH Access to the server
• Firewall configured to allow traffic on port 80

1) The first step is to create a new directory. This directory will be used to store the website’s content. This is known as the Document Root in your Apache vhost configuration file. Using the -p flag in the command will automatically add the parents of your new directory. Create the directory with the command shown below:

sudo mkdir -p /var/www/vhosts/domain.com/public_html

2) Now you need to set the permissions for the new directory. Replace vhostuser in username:vhostuser from our example below, with the user on your server that should have access to the new directory. The command is as follows:

sudo chown -R username:vhostuser /var/www/vhosts/domain.com/public_html

Users will also need read access to the directory, so be sure to update the permissions with the command shown here:

sudo chmod -R 755 /var/www/vhosts/

3) Next we will need to open the Apache configuration file. You can open and edit it with whichever text editor you are comfortable with, for this guide we will use ‘vi’

sudo vi /etc/httpd/conf/httpd.conf

At the end of the file, add the following line and save your change:

Include vhost.d/*.conf

This line tells Apache to read all files ending in .conf within the /etc/httpd/vhost.d directory.

4) Now that we have that line added in, we need to create the directory for the vhost configuration file. Execute the following command to create this directory.

sudo mkdir /etc/httpd/vhost.d/

5) Next we will create a default template which will allow us to make additional vhosts more easily in the future. The following command creates and opens our new file:

vi /etc/httpd/vhost.d/default.template

6) Now that we’ve created the file and have it open, we need to drop the actual vhost configuration into the file. Paste the following into the file and save the change:


  ServerName domain.com
  ServerAlias www.domain.com
  DocumentRoot /var/www/vhosts/domain.com/public_html
  
           Options Indexes FollowSymLinks MultiViews
           AllowOverride All
   
CustomLog /var/log/httpd/domain.com-access.log combined
ErrorLog /var/log/httpd/domain.com-error.log
   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.

   LogLevel warn


#
#        ServerName example.com
#        DocumentRoot /var/www/vhosts/domain.com/public_html
#        
#                Options Indexes FollowSymLinks MultiViews
#                AllowOverride All
#        
#        CustomLog /var/log/httpd/example.com-ssl-access.log combined
#        ErrorLog /var/log/httpd/example.com-ssl-error.log
     # Possible values include: debug, info, notice, warn, error, crit,
     # alert, emerg.

#        LogLevel warn
#        SSLEngine on
#        SSLCertificateFile    /etc/ssl/certs/domain.crt
#        SSLCertificateKeyFile /etc/ssl/certs/domain.key
#

7) Now that we have our template in place, we can easily create new vhosts using the following steps. First, create a vhost configuration file for our site using the following command, remember to replace domain.com with your own site:

sudo cp /etc/httpd/vhost.d/default.template /etc/httpd/vhost.d/domain.com.conf

8) Now open the new file we created called domain.com.conf and change the mentions of domain.com within the file to your site’s name. Once you’ve made those changes, save the file.

9) Now we need to restart apache. using the following command:

sudo service httpd restart

When restarting, you’ll expect to see ” Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 0000:0000:0000:0000:0000:0000:0000:0000 for ServerName”. This indicates that our vhost is setup and ready for use. But in order for anything to be displayed, you’ll need to add content to the Directory Root that we created back in Step 1. If you just want to see a test page, you can place the following in an file called ‘index.html’ in your Document Root:



  
    
    vhost test for domain.com
  
  
    

Success! domain.com vhost!

Apache vhost Setup on Ubuntu 18.04

A vhost stands for virtual host. Vhosts are used to serve multiple domains without the need for additional Internet Protocol (IP) addresses. With vhosts, the different pages are displayed according to what has been set in the host file for the particular site requested. With vhosts, you can add as many sites as you need to your server without needing additional IP addresses. This article provides instructions for creating vhosts on Ubuntu® specifically.

In this article, we’ll be using the placeholder of ‘domain.com’, but you’d want to replace this with whatever domain you’re setting your vhost up for.

Prerequisites:
• Server running Ubuntu 18.04
• Apache installed (sudo apt-get install apache2)
• DNS pointing the site to the server’s IP
• SSH Access to the server
• Firewall configured to allow traffic on port 80

1) The first step is to create a new directory. This directory will be used to store the website’s content. This is known as the Document Root in your Apache vhost configuration file. Using the -p flag in the command will automatically add the parents of your new directory. Create the directory with the command shown below:

sudo mkdir -p /var/www/vhosts/domain.com/public_html

2) Now you need to set the permissions for the new directory. Replace vhostuser in username:vhostuser from our example below, with the user on your server that should have access to the new directory. The command is as follows:

sudo chown -R username:vhostuser /var/www/vhosts/domain.com/public_html

Users will also need read access to the directory, so be sure to update the permissions with the command shown here:

sudo chmod -R 755 /var/www/vhosts/

4) Now that we have permissions taken care of and our Document Root setup, we need to create the configuration file for our vhost site. Create the following file and open it using the command below:

vi /etc/apache2/sites-available/domain.com.conf

Paste the following into the file you’ve created, be sure to replace the domain.com examples with your own site hostname:


    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin admin@domain.com
    DocumentRoot /var/www/vhosts/domain.com/public_html

    
        Options -Indexes +FollowSymLinks
        AllowOverride All
    

    ErrorLog ${APACHE_LOG_DIR}/domain.com-error.log
    CustomLog ${APACHE_LOG_DIR}/domain.com-access.log combined

Once you’re finished making the changes, save the file.

5) Now that our vhost configuration file is in place, we need to enable the new virtual host file. To do this, we have to create a symbolic link from the virtual host file to the sites-enabled directory which will be read by Apache during it’s startup. There are a few ways to do this, but the easiest and quickest is to use the a2ensite command as seen below:

sudo a2ensite domain.com

6) Once you’ve enabled the site, it’s always best to check for any issues. You can do this by running the following command to test your configuration:

sudo apachectl configtest

If you get a response which reads: “Syntax OK”, this indicates that our vhost is setup and ready for use. So, the final change would be to restart Apache by issuing the following command:

sudo systemctl restart apache2

Now your site should be working as intended. However, in order for anything to be displayed when navigating to the site, you’ll need to add content to the Directory Root that we created back in Step 1. If you just want to see a test page, you can place the following in an file called ‘index.html’ in your Document Root:



  
    
    vhost test for domain.com
  
  
    

Success! domain.com vhost!