Setting Up WordPress On Apache2

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 12

Set Up Apache to Run Multiple WordPress Sites

Create Your Site Databases and Users


You will need a MySQL database for each instance of WordPress you intend to run. An example of a
two-WordPress setup is shown below. Replace example1 and example2 with your respective
website names.

Domain Database Username Password


example1.com example1_wordpress example1_wpuser password1
example2.com example2_wordpress example2_wpuser password2
1. Log in to the MySQL command line as the root user:
sudo mysql -u root

2. Create the WordPress databases:


CREATE DATABASE example1_wordpress;
CREATE DATABASE example2_wordpress;

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';

4. Grant the users privileges for their respective database:


GRANT ALL PRIVILEGES ON example1_wordpress.* TO 'example1_wpuser';
GRANT ALL PRIVILEGES ON example1_wordpress.* TO 'example2_wpuser';

5. Exit MySQL:
quit

Install Multiple WordPress Instances


1. Create the directories that will host your websites and WordPress source files. In this guide, the
home directories /var/www/html/example1.com/ and
/var/www/html/example2.com/ are used as examples.
sudo mkdir -p /var/www/html/{example1.com,example2.com}/public_html

2. Create a src directory to hold the WordPress tarball and files:


sudo mkdir /var/www/html/src/

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

5. Copy the WordPress files to your site’s public_html folders:


sudo cp -R /var/www/html/src/wordpress/*
/var/www/html/example1.com/public_html/
sudo cp -R /var/www/html/src/wordpress/*
/var/www/html/example2.com/public_html/

6. Give Apache ownership of your WordPress sites’ home directories:


sudo chown -R www-data:www-data /var/www/html/{example1.com,example2.com}/

Configure Apache Virtual Hosts


In this section, you will configure the Apache virtual hosts file so that a visitor to example1.com
will be served the content in /var/www/html/example1.com/public_html and the MySQL
database example1_wordpress. Visitors to example2.com will be served content in
/var/www/html/example2.com/public_html/ and its corresponding MySQL database.

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

# Block .svn, .git


RewriteRule \.(svn|git)(/)?$ - [F]

# Catchall redirect to www.example1.com


RewriteCond %{HTTP_HOST} !^www.example1\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) https://www.example1.com/$1 [L,R]

# Recommended: XSS protection


<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
</VirtualHost>

• 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

# Block .svn, .git


RewriteRule \.(svn|git)(/)?$ - [F]

# Catchall redirect to www.example2.com


RewriteCond %{HTTP_HOST} !^www.example2\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) https://www.example2.com/$1 [L,R]

# Recommended: XSS protection


<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
</VirtualHost>

3. Enable the site:


sudo a2ensite example2.conf
You can repeat Steps 1 and 2 for each WordPress site that you want to host on your Linode.
4. If the rewrite_module module is not enabled, you will need to enable it before reloading
Apache to have your configurations take effect. To check which Apache modules are enabled,
run the following command:
sudo apache2ctl -M

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

5. For the new configurations to take effect, reload Apache:


sudo systemctl reload apache2

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

You should see WordPress’ set up page:

How To Set Up Apache Virtual Hosts on Ubuntu


Introduction
The Apac
he HTTP server is a popular open-source web server that offers flexibility, power, and widespread
support for developers. Apache server configuration does not take place in a single monolithic file, but
instead happens through a modular design where new files can be added and modified as needed.
Within this modular design, you can create an individual site or domain called a virtual host.
Using virtual hosts, one Apache instance can serve multiple websites. Each domain or individual site
that is configured using Apache will direct the visitor to a specific directory holding that site’s
information. This is done without indicating to the visitor that the same server is also responsible for
other sites. This scheme is expandable without any software limit as long as your server can handle the
load.
In this guide, you will set up Apache virtual hosts on an Ubuntu 20.04 server. During this process,
you’ll learn how to serve different content to different visitors depending on which domains they are
requesting by creating two virtual host sites.

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.

Step 1 — Creating the Directory Structure


The first step is to create a directory structure that will hold the site data that you will be serving to
visitors.
Your document root, the top-level directory that Apache looks at to find content to serve, will be set to
individual directories under the /var/www directory. You will create a directory here for each of the
virtual hosts.
Within each of these directories, you will create a public_html directory. The public_html
directory contains the content that will be served to your visitors. The parent directories, named here as
your_domain_1 and your_domain_2, will hold the scripts and application code to support the
web content.
Use these commands, with your own domain names, to create your directories:
sudo mkdir -p /var/www/your_domain_1/public_html
sudo mkdir -p /var/www/your_domain_2/public_html
Be sure to replace and with your own respective domains. For example, if one of your domains was
example.com you would create this directory structure:
/var/www/example.com/public_html.

Step 2 — Granting Permissions


You’ve created the directory structure for your files, but they are owned by the root user. If you want
your regular user to be able to modify files in these web directories, you can change the ownership with
these commands:
sudo chown -R $USER:$USER /var/www/your_domain_1/public_html
sudo chown -R $USER:$USER /var/www/your_domain_2/public_html

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.

Step 3 — Creating Default Pages for Each Virtual Host


With your directory structure in place, you can start focusing on each individual virtual host site and
the content within that site. Start by creating an index.html page for your first site
your_domain_1.

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.

Step 4 — Creating New Virtual Host Files


Virtual host files are the files that specify the actual configuration of your virtual hosts and dictates how
the Apache web server will respond to various domain requests.
Apache comes with a default virtual host file called 000-default.conf. You can copy this file to
create virtual host files for each of your domains.
Copy the default configuration file over to the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-
available/your_domain_1.conf

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

With comments removed, the file will be similar to this:


/etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
...
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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>

Save and close the file.


Create your second configuration file by copying over the file from your first virtual host site:
sudo cp /etc/apache2/sites-available/your_domain_1.conf /etc/apache2/sites-
available/your_domain_2.conf

Open the new file in your preferred editor:


sudo nano /etc/apache2/sites-available/your_domain_2.conf

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>

Save and close the file when you are finished.

Step 5 — Enabling the New Virtual Host Files


Now that you have created your virtual host files, you must enable them. Apache includes some tools
that allow you to do this.
You’ll be using the a2ensite tool to enable each of your sites. If you would like to read more about
this script, you can refer to the a2ensite documentation.

Use the following commands to enable your virtual host sites:


sudo a2ensite your_domain_1.conf
sudo a2ensite your_domain_2.conf

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

Next, test for configuration errors:


sudo apache2ctl configtest

The should receive the following output:


Output
. . .
Syntax OK

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.

Step 6 — (Optional) Setting Up Local Hosts File


If you haven’t been using actual domain names that you own to test this procedure, and have been
using example domains instead, you can still test the functionality of your virtual host sites by
temporarily modifying the hosts file on your local computer. This will intercept any requests for the
domains that you configured and point them to your Virtual Private Server (VPS), just as the DNS
system would do if you were using registered domains. This will only work from your local computer
and is only for testing purposes.
Make sure you are operating on your local computer for these steps and not your VPS server. You will
need to know the computer’s administrative password or otherwise be a member of the administrative
group.
If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:
sudo nano /etc/hosts

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.

Step 7 — Testing Your Results


Now that you have your virtual hosts configured, you can test your setup by going to the domains that
you configured in your web browser:
http://your_domain_1

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.

You might also like