Setting up WordPress on DreamCompute

This tutorial explains how to install WordPress on a DreamCompute instance, including both the application itself and the database it uses. All necessary components will be installed and configured without making use of automatic configuration management systems.

Preparation

This article assumes you've already created a new instance. If not, view the following articles for details on creating a new instance:

This example will use an Ubuntu virtual machine, but you can use whichever flavor of Linux you prefer. As long as you can install Apache, PHP, and database software, WordPress will be able to run.

Whichever machine type you pick, remember it's better to use instances with a volume boot disk as they are permanent (as opposed to instances with ephemeral boot disks).

Step 1 — Point DNS to your instance

First you'll need to set up DNS for your domain to point to the instance. View the following article for details:

Step 2 — Add SSH Access

WordPress users often need SSH access in order to accomplish specific configurations within WordPress. By default, this is disabled, so you will need to edit your SSH config using a text editor, such as vim, emacs, or nano (this one is easiest for beginners). View the following article for further information:

Step 3 — Create a user with sudo privileges

While it is not required to make a separate user on the instance for WordPress, it's strongly recommended for security. View the following article for further information:

This article assumes the new user is named 'exampleuser'. Make sure to assign the user 'sudo' privileges and switch to the new user before continuing. 

Step 4 — Install LAMP

While you certainly can install everything on it's own, the LAMP stack for Ubuntu is perfect for WordPress. A separate DreamHost article on how to configure LAMP is available below, but an overview is also provided here.

[user@server]$ sudo apt-get update
[user@server]$ sudo apt-get install lamp-server^

The second command is interactive, so you will be asked 'Do you want to continue?' in some places.

You will also be asked to set a password for the root user of your SQL software. Create a secure password and save it as you will need this later to set up a database for WordPress.

Enable mod_rewrite

Enable mod_rewrite so that WordPress permalinks (or "pretty" URLs) work.

[user@server]$ sudo a2enmod rewrite

Finally restart Apache.

[user@server]$ sudo service apache2 restart

At this point, you should be able to access your instance in a web browser using its IP address. If not, double-check the security group(s) assigned to your instance to make sure that incoming connections to port 80 are allowed for your IP address. If you need help working with security groups, see DreamHost's article here:

Step 5 — Create a folder for your website

Create a folder for the website.

[user@server]$ sudo mkdir /var/www/example.com

Then give the user you just created ownership of that directory and its contents. You should also assign group ownership of the directory to the default Apache group of www-data.

[user@server]$ sudo chown -R exampleuser:www-data /var/www/example.com/

Step 6 — Configure the site's VirtualHosts file

Next you'll want to configure VirtualHosts so your instance's Apache service knows how to handle the domain. To do this, you need to create a .conf file.

[user@server]$ sudo touch /etc/apache2/sites-available/example.com.conf

It is recommended that you name the file after your domain, so that you can always know what .conf file is for what domain.

Edit that file:

[user@server]$ sudo vim /etc/apache2/sites-available/example.com.conf

Add the following code to it. Make sure to adjust the domain name to your actual site.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com
    <Directory /var/www/example.com>
            AllowOverride all
            Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Once the VirtualHost is added, the site will need to be enabled with a command called a2ensite.

If you want to disable a site, you would use the command a2dissite.

[user@server]$ sudo a2ensite

This will prompt you to pick what site you want to enable. Type it in, hit enter, and you'll be told what's next.

Your choices are: 000-default default-ssl example.com
Which site(s) do you want to enable (wildcards ok)?
example.com
Enabling site example.com.
To activate the new configuration, you need to run:
  service apache2 reload

Remember this command. It's a fast way to enable sites without having to rename or mess with files. Finally reload your Apache service so it reads the changes.

[user@server]$ sudo service apache2 reload

Step 7 — Create the database and users

WordPress absolutely requires a database. To create one, start by accessing the MySQL service installed by LAMP earlier.

[user@server]$ mysql -u root -p
Enter password:

Enter the password that you created for the MySQL root user when installing LAMP.

Your command prompt will change to mysql>. You can now create the database and assign a new user to it with their own unique password.

mysql> CREATE DATABASE wordpressdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

This creates a new database named 'wordpressdb'.

When running the command below, remember to change PASSWORD to a secure password. In this command, 'dbuser' is the new database username for the new database.

mysql> GRANT ALL ON wordpressdb.* TO dbuser@localhost IDENTIFIED by 'PASSWORD';

Exit the mysql prompt.

mysql> exit

You can check that the database was successfully created by running the following command.

[user@server]$ mysql -u dbuser -p wordpressdb

Step 8 — Install WP-CLI

While this is optional, it is strongly recommended. DreamHost includes WP-CLI on all its managed servers due to its usefulness. For more information about WP-CLI, see the project homepage below.

To install, run the following.

[user@server]$ cd ~
[user@server]$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Run the following command to verify that WP-CLI works.

[user@server]$ php wp-cli.phar --info
PHP binary:     /usr/bin/php7.0
PHP version:    7.0.22-0ubuntu0.16.04.1
php.ini used:   /etc/php/7.0/cli/php.ini
WP-CLI root dir:        phar://wp-cli.phar
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /home/ubuntu
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.4.1

And if it does move it so everyone can use it.

[user@server]$ chmod +x wp-cli.phar
[user@server]$ sudo mv wp-cli.phar /usr/local/bin/wp

That will make it accessible for all users.

Step 9 — Install WordPress

Switch to the site directory you created and use WP-CLI to download the latest version of WordPress.

[user@server]$ cd /var/www/example.com/
[user@server]$ wp core download

If you visit your domain in a browser now, you'll get a WordPress setup/configuration page.

Installing using WP-CLI

You can also install WordPress using the WP-CLI with the following commands.

In the first command, enter your MySQL database and user credentials.

[user@server]$ wp core config --dbname=wordpressdb --dbuser=exampleuser --dbpass=PASSWORD
Success: Generated 'wp-config.php' file.

In the next command, create a new username for WordPress with a new password.

If you use secure passwords like cWG8j8FPPj{T9UDL_PW8 then you MUST put quotes around them.

In this example, the option to skip-emails has been chosen since all the necessary information is specified directly in the WP-CLI command.

The backslash character \ is used when entering a long command. When you enter the \, the terminal command continues to the next line until you finish entering it.

You could also remove the \ character and put every parameter into a single line.

[user@server]$ wp core install --url=http://example.com --title=DreamComputePress \ 
--admin_user=YOURUSERNAME --admin_password=PASSWORD --admin_email=admin@example.com --skip-email
sh: 1: /usr/sbin/sendmail: not found Success: WordPress installed successfully.

If you visit your site now, you'll see a new WordPress site has been created.

Step 10 — Set permissions

Setting directory and file permissions

To secure your website folder, make sure all directories are set to 755 permissions and files to 644.

[user@server]$ sudo find /var/www/example.com -type d -exec chmod 755 {} \;
[user@server]$ sudo find /var/www/example.com -type f -exec chmod 644 {} \;

Set the 'setgid' bit for all directories

Setting the 'setgid' bit for all directories causes newly created files to inherit the group permissions of the parent directory. This ensures permissions will remain correct in the future.

[user@server]$ sudo find /var/www/example.com -type d -exec chmod g+s {} \;

Allow the wp-content directory and its subdirectories to be writable.

[user@server]$ sudo find /var/www/example.com/wp-content -type d -exec chmod g+w {} \;

Next, to secure your installation of WordPress, modify the permissions of 'wp-config.php' so that only users on your instance can access it.

[user@server]$ chmod -v 640 wp-config.php
mode of 'wp-config.php' changed from 0644 (rw-r--r--) to 0640 (rw-r-----)

Change ownership of all WordPress site files to your username and the group to www-data so that the Apache server can access the files.

[user@server]$ sudo chown -R exampleuser:www-data /var/www/example.com

Step 11 — Editing the wp-config.php file

You must now add a line to the bottom of the wp-config.php file.

[user@server]$ sudo vim /etc/www/example.com/wp-config.php

Add the following line to the bottom of the file:

define('FS_METHOD', 'direct');

You can now install Themes and Plugins within the WordPress dashboard.

Additional configurations

Installing Themes and Plugins

When you install a Theme or Plugin, the permissions default to www-data:www-data. For security purposes, it's recommended to switch the user back to your own username when you're finished installing new software.

[user@server]$ sudo chown -R exampleuser:www-data /var/www/example.com

Upgrading WordPress

When upgrading WordPress, you need to manually change the permissions back to the Apache www-data group so that the Apache webserver can access the files.

[user@server]$ sudo chown -R www-data:www-data /var/www/example.com

When finished updating, switch your permissions back to your user.

[user@server]$ sudo chown -R exampleuser:www-data /var/www/example.com

Make PHP Better

If you plan to use a lot of media, install the following system packages to improve how PHP processes images.

[user@server]$ sudo apt install php-imagick php7.0-gd

Make sure to restart Apache so that PHP starts using those packages.

[user@server]$ sudo service apache2 restart

Troubleshooting

If permalinks aren't working, make sure you installed rewrite.

[user@server]$ sudo a2enmod rewrite && sudo service apache2 restart

And make absolutely sure you have AllowOverride set to All in your Virtual Host.

<Directory /var/www/example.com>
    AllowOverride all
    Require all granted
</Directory>

Permalinks will not work without it.

Further WordPress configurations

If you're interested in using HTTPS with this WordPress installation, check out the following articles from DreamHost's Knowledge Base for assistance setting up Let's Encrypt on your instance and configuring WordPress to use HTTPS.

For further WordPress customizations, please review the WordPress category in DreamHost's knowledge base below.

See also

Did this article answer your questions?

Article last updated PST.