Setting Up WordPress On Apache2
Setting Up WordPress On Apache2
Setting Up WordPress On Apache2
3. Create the database users, replacing example1_wpuser and password with a username
and password of your own:
CREATE USER 'example1_wpuser' IDENTIFIED BY 'password1';
CREATE USER 'example2_wpuser' IDENTIFIED BY 'password2';
5. Exit MySQL:
quit
3. Download and extract the latest version of WordPress to the src folder:
cd /var/www/html/src/
sudo wget http://wordpress.org/latest.tar.gz
4. Extract the tarball. To store a backup of the original source files, rename latest.tar.gz to
wordpress followed by the date. This will be useful if you install new versions in the future
and need to revert back to a previous release.
sudo tar -zxvf latest.tar.gz
sudo mv latest.tar.gz wordpress-`date "+%Y-%m-%d"`.tar.gz
1. Create a virtual hosts configuration file for example1.com and add the example virtual host
block into /etc/apache2/sites-available/example1.com. Be sure to replace all
instances of example1.com with your own domain.
File: /etc/apache2/sites-available/example1.conf
<VirtualHost *:80>
# The primary domain for this host
ServerName example1.com
# Optionally have other subdomains also managed by this Virtual Host
ServerAlias example1.com *.example1.com
DocumentRoot /var/www/html/example1.com/public_html
<Directory /var/www/html/example1.com/public_html>
Require all granted
# Allow local .htaccess to override Apache configuration settings
AllowOverride all
</Directory>
# Enable RewriteEngine
RewriteEngine on
RewriteOptions inherit
• Enable the site. This will create a symlink to the example1.com Apache conf file in
/etc/apache2/sites-enabled/:
sudo a2ensite example1.conf
• Create a virtual hosts configuration file for your second WordPress site, example2.com. Be
sure to replace all instances of example2.com with your own domain.
File: /etc/apache2/sites-available/example2.conf
<VirtualHost *:80>
# The primary domain for this host
ServerName example2.com
# Optionally have other subdomains also managed by this Virtual Host
ServerAlias example2.com *.example2.com
DocumentRoot /var/www/html/example2.com/public_html
<Directory /var/www/html/example2.com/public_html>
Require all granted
# Allow local .htaccess to override Apache configuration settings
AllowOverride all
</Directory>
# Enable RewriteEngine
RewriteEngine on
RewriteOptions inherit
Verify that you see rewrite_module in the list. If you do not see the module, enable it with
the following command:
sudo a2enmod rewrite
Configure WordPress
Follow the Configure WordPress section of our Install WordPress on Ubuntu 18.04 guide.
If you do not yet have registered domains to use, you can still perform the WordPress installation using
your Linode’s IP address. For example:
1. Verify your WordPress installation by using your Linode’s IP address to load the WordPress
installations in your browser:
http://203.0.113.15/example1.com/public_html
http://203.0.113.15/example2.com/public_html
Prerequisites
Before you begin this tutorial, you will need:
• An Ubuntu 20.04 server with a non-root user with sudo privileges. You can use our Initial
Server Setup with Ubuntu 20.04 guide to set this up.
• Apache installed on the server. You can learn how by completing steps 1-3 on our How To
Install the Apache Web Server on Ubuntu 20.04 tutorial.
If you are using DigitalOcean, you can learn how to set up domains by following our product
documentation, How to Add Domains.
In order to successfully complete this tutorial, you will need two domains with:
• An A record with your_domain pointing to your server’s public IP address.
• An A record with www.your_domain pointing to your server’s public IP address.
For other providers, please refer to their relevant product documentation.
Note: If you do not have domains available at this time, you can use test values locally on your
computer. Step 6 of this tutorial will show you how to test and configure your test values. This will
allow you to validate your configuration even though your content won’t be available to other visitors
through the domain name.
The $USER variable will take the value of the user you are currently logged in as when you press
ENTER. By doing this, the regular user now owns the public_html subdirectories where you will
be storing your content.
You should also modify your permissions to ensure that read access is permitted to the general web
directory and all of the files and folders it contains so that the pages can be served correctly:
sudo chmod -R 755 /var/www
Your web server now has the permissions it needs to serve content, and your user should be able to
create content within the necessary folders. The next step is to create content for your virtual host sites.
Open and create the index.html file with your preferred text editor. This example uses nano:
nano /var/www/your_domain_1/public_html/index.html
Within this file, create an HTML file that indicates to visitors which site they are connected to:
/var/www/your_domain_1/public_html/index.html
<html>
<head>
<title>Welcome to your_domain_1!</title>
</head>
<body>
<h1>Success! The your_domain_1 virtual host is working!</h1>
</body>
</html>
To save and close the file in nano, start by pressing CTRL+X. Press Y when prompted to save the file,
then press ENTER when you are finished to exit.
Next, copy this file to use as the base for your second site by typing:
cp /var/www/your_domain_1/public_html/index.html
/var/www/your_domain_2/public_html/index.html
Then open this new file and modify the relevant pieces of information using your text editor like
before:
nano /var/www/your_domain_2/public_html/index.html
/var/www/your_domain_2/public_html/index.html
<html>
<head>
<title>Welcome to your_domain_2!</title>
</head>
<body> <h1>Success! The your_domain_2 virtual host is working!</h1>
</body>
</html>
Save and close this file. You now have one page for each site that you can use to test the virtual host
configuration.
Be aware that the default Ubuntu configuration requires that each virtual host file end in .conf.
Open the new file in your preferred text editor with root privileges:
sudo nano /etc/apache2/sites-available/your_domain_1.conf
Within this file, customize the items for your first domain and add some additional directives. This
virtual host section matches any requests that are made on port 80, the default HTTP port.
First, change the ServerAdmin directive to an email that the site administrator can receive emails
through:
/etc/apache2/sites-available/your_domain_1.conf
ServerAdmin admin@your_domain_1
After this, add two additional directives. The first, called ServerName, establishes the base domain
for the virtual host definition. The second, called ServerAlias, defines further names that should
match as if they were the base name. This is useful for matching additional hosts you defined. For
instance, if you set the ServerName directive to example.com you could define a ServerAlias
to www.example.com, and both will point to this server’s IP address.
Add these two directives to your configuration file after the ServerAdmin line:
/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
...
ServerAdmin admin@your_domain_1
ServerName your_domain_1
ServerAlias www.your_domain_1
DocumentRoot /var/www/html
...
</VirtualHost>
Next, change your virtual host file location for the document root for this domain. Edit the
DocumentRoot directive to point to the directory you created for this host:
/etc/apache2/sites-available/your_domain_1.conf
DocumentRoot /var/www/your_domain_1/public_html
Here is an example of the virtual host file with all of the adjustments made above:
/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
...
ServerAdmin admin@your_domain_1
ServerName your_domain_1
ServerAlias www.your_domain_1
DocumentRoot /var/www/your_domain_1/public_html
...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
...
</VirtualHost>
You now need to modify all of the pieces of information to reference your second domain. When you
are finished, it should look like this:
/etc/apache2/sites-available/your_domain_2.conf
<VirtualHost *:80>
...
ServerAdmin admin@your_domain_2
ServerName your_domain_2
ServerAlias www.your_domain_2
DocumentRoot /var/www/your_domain_2/public_html
...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
...
</VirtualHost>
There will be output for both sites, similar to the example below, reminding you to reload your Apache
server:
Output
Enabling site example.com.
To activate the new configuration, you need to run:
systemctl reload apache2
Before reloading your server, disable the default site defined in 000-default.conf by using the
a2dissite command:
sudo a2dissite 000-default.conf
Output
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
When you are finished, restart Apache to make these changes take effect.
sudo systemctl restart apache2
Optionally, you can check the status of the server after all these changes with this command:
sudo systemctl status apache2
Your server should now be set up to serve two websites. If you’re using real domain names, you can
skip Step 6 and move on to Step 7. If you’re testing your configuration locally, follow Step 6 to learn
how to test your setup using your local computer.
If you are on a Windows machine, open the Command Prompt and type:
notepad %windir%\system32\drivers\etc\hosts
The details that you need to add are the public IP address of your server, followed by the domain you
want to use to reach that server. Using the domains used in this guide, and replacing your server IP for
the text, your file should look like this:
/etc/hosts
127.0.0.1 localhost
127.0.1.1 guest-desktop
your_server_IP your_domain_1
your_server_IP your_domain_2
This will direct any requests for your two domains on your computer and send them to your server at
the designated IP address.
Save and close the file.
You can also visit your second host page and view the file you created for your second site:
http://your_domain_2
If both of these sites work as expected, you’ve successfully configured two virtual hosts on the same
server.
Note: If you adjusted your local computer’s hosts file, like in Step 6 of this tutorial, you may want to
delete the lines you added now that you verified that your configuration works. This will prevent your
hosts file from being filled with entries that are no longer necessary.
Conclusion
You now have a single server handling two separate domain names. You can expand this process by
following the steps we outlined above to add additional virtual hosts. There is no software limit on the
number of domain names Apache can handle, so feel free to make as many virtual hosts as your server
is capable of handling.