Post

Setup WordPress on VPS

If you’re into WordPress and you want to do your own hosting, and the idea of cPanel makes you feel a little bit sick, then configuring your own Ubuntu server to host multiple WordPress websites is both cost-effective and versatile.

Prerequisites.

  • A VPS (virtual private server) with Ubuntu 20 installed.
  • Login as a non root user with sudo privileges
  • wpcli installed

Switch to a non-root user.

Once you login you will see updates available on your server. Its best practice to perform these server maintenance updates before installing your new wordpress website.

1
su nonrootuser

Create a new site directory.

Create new directory for site /var/www/example.com

1
sudo mkdir /var/www/example.com

Create and enable virtual hosts file.

1
sudo vim /etc/apache2/sites-available/example.com.conf

Configure the virtual hosts file.

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example
    ServerAlias www.example
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the virtual hosts file.

1
sudo a2ensite example.com.conf

Reload apache2.

1
systemctl reload apache2

Add user and database to MySQL and grant privileges.

Now time to create the WordPress MYSQL database. Unfortunately WpCli does not have the power to create the database for you. So follow the below steps to create the MySQL database manually.

Login to mysql.

1
sudo mysql -u root

Create database and database user.

Run each line as individual commands. It’s best practice to copy the below snippet to an an external text file and edit it there. when done copy/paste each line into the command prompt for mysql.

Create database.

1
CREATE DATABASE dbname;

Create User.

Create a user with a username and password. These user access details will be used by Wordpress to access the database.

1
CREATE USER ''@'localhost' identified by '';

Grant privileges.

Grant your user privileges to access the database you created.

1
GRANT ALL PRIVILEGES ON dbname.* to 'username'@'localhost';

Flush privileges.

1
FLUSH PRIVILEGES;

Exit MySQL.

1
exit;

Install WP using WpCLI.

In this section we are going to download the latest version of WordPress files into your current working directory and run some commands to ensure the permissions are correct.

1
cd /var/www/example.com

Change permissions on your destination folder.

Incorrect file permissions are often the problem when something goes wrong with the installation. It’s important to run the permission commands to ensure correct permissions.

update your directory permissions

Change directory permissions.

1
sudo find /var/www/example.com -type d -exec chmod 775 {} \;

Change ownership.

Change ownership of your site directory to the current logged in user.

1
sudo chown -R $USER:www-data /var/www/example.com

Download Wp core files

1
wp core download

Generate wp config file

Using WPCLI we Generate the wp-config file. WpCLI will check your database access details are correct. You will get access denied if your enter Incorrect database credentials. The database credentials where created in the the previous step create database and user.

1
wp core config --dbname=db_name --dbuser=db_user --dbpass='db_password' --dbprefix=db_prefix;

Install WordPress

1
wp core install --url=https://example.com --title='example' --admin_user=admin_username --admin_password=admin_pw --admin_email=example@example.com

Update wp-config file.

1
2
3
4
5
6
/** Allow Direct Updating Without FTP */
define('FS_METHOD', 'direct');
/** Disable Editing of Themes and Plugins Using the Built In Editor */
define('DISALLOW_FILE_EDIT', 'true');
/** Allow Automatic Core Updates */
define('WP_AUTO_UPDATE_CORE', 'true');

Update site file permissions

1
2
3
sudo chown -R $USER:www-data /var/www/example.com
sudo find /var/www/example.com/ -type d -exec chmod 775 {} \;
sudo find /var/www/example.com/ -type f -exec chmod 664 {} \;

Now install SSL Certificate

Use certbot to install an ssl cert and make your site load from https

install ssl with certbot

Secure WPConfig file

Update permissions

1
chmod 660 wp-config.php

Block Access

Add to .htaccess file to deny access to wp-config file

1
2
3
4
<files wp-config.php>
order allow,deny
deny from all
</files>
This post is licensed under CC BY 4.0 by the author.