If you have an Apache2 and you want to serve up multiple sites with different domain names from that same server, this article is for you!
This process is super simple but assumes a few things beforehand (these are all covered in separate blog posts):
- You have a basic understanding of how to use the Linux command line.
- You have a non-root account that is in the sudoers group to use to setup and configure everything. You do NOT want to do these steps as root, but use sudo in conjunction with this working account. The steps to set this account up can be found HERE.
- You have the ability to SSH into or access the console of your web host environment.
- You are running a server that has Apache2 and PHP installed.
- You already have a working mySQL database setup and have access to it either via command line or a web app such as Adminer of phpMyAdmin.
- You have access to and understand how to manipulate your domain(s) DNS records to point to your server.
OK, with that out of the way, lets get on with it!
**NOTE** For reference, in the code areas where you see the $ sign, this is the beginning of the command line in Linux. You DO NOT type that in when entering the code. This means:
$ sudo nano somefile.confis typed as
sudo nano somefile.conf**NOTE 2** You should begin by opening up mySQL either via the command line or in your browser with something like Adminer or phpMyAdmin and create a database that will be used for WordPress. Be sure to make note of your mySQL username, password and the name of the database you are creating for WordPress.
- The first step is to log into the command line on your host via SSH
- Next, you’ll use a command line editor (I like nano, but vim or others work. I will be using nano for this tutorial) and create a configuration file for your virtual host. This is done for every single host you decide to add:
$ sudo nano /etc/apache2/sites-available/<FDQN>.conf FDQN is the “Fully Qualified Domain Name.” Examples would be foo.com, mydomain.net, etc. Subdomains can also be used as virtual hosts. A subdomain is something like myblog.mysite.com.
Change the FDQN in the code above to your domains FDQN.
You will now paste the following into the nano editor (again, change the FDQN accordingly):
<Directory /var/www/html/<FDQN>/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName <FDQN>
ServerAlias www.<FDQN>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/<FDQN>/public_html
ErrorLog /var/www/html/<FDQN>/logs/error.log
CustomLog /var/www/html/<FDQN>/logs/access.log combined
</VirtualHost>Use the command CTRL-X and then choose Y to save modified buffer. It should show Filename to write: <FDQN>.conf and then hit ENTER.
3. Next, we are going to create the necessary directories for serving up your WordPress site (again, change the FDQN accordingly):
$ sudo mkdir -p /var/www/html/<FDQN>/{public_html,logs}4. Now, let’s enable the site:
$ sudo a2ensite <FDQN>.conf5. If this is the very first time you have enabled a site, you need to do the following to remove the default Apache2 site configuration. You only need to do this once. All future virtual hosts can be added without this step:
$ sudo a2dissite 000-default.conf6. Now let’s reload the Apache2 service:
$ sudo systemctl reload apache27. This next section, like removing the default Apache2 site, only needs to be done once. You can skip this step when adding future virtual hosts:
$ sudo a2enmod rewrite
$ sudo apache2ctl configtest
$ sudo systemctl restart apache28. Now we are going to setup WordPress. This next block will download the latest version, set file and folder permissions and ownership, and copy the WordPress files to the appropriate location. Again, use the appropriate FDQN and for username choose the non-root account you are using currently.
$ cd /tmp
$ sudo curl -O https://wordpress.org/latest.tar.gz
$ sudo tar xzvf latest.tar.gz
$ sudo touch /tmp/wordpress/.htaccess
$ sudo chmod 660 /tmp/wordpress/.htaccess
$ sudo cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
$ sudo mkdir /tmp/wordpress/wp-content/upgrade
$ sudo cp -a /tmp/wordpress/. /var/www/html/<FDQN>/public_html
$ sudo chown -R <yourusername>:www-data /var/www/html/<FDQN>/
$ sudo find /var/www/html/<FDQN> -type d -exec chmod g+s {} \;
$ sudo chmod g+w /var/www/html/<FDQN>/public_html/
$ sudo chmod -R g+w /var/www/html/<FDQN>/public_html/9. Next, we are going to setup the WordPress wp-config.php file to prepare for installation. This first line you need to copy the results to an open text file or somewhere that you have access to so that when you open nano you can copy and paste this information into the wp-config.php file:
$ curl -s https://api.wordpress.org/secret-key/1.1/salt/ Now open the wp-config.php file (yet again changing the FDQN accordingly):
$ sudo nano /var/www/html/<FDQN>/public_html/wp-config.phpNow you edit the following lines to match your database settings:
define(‘DB_NAME’, ‘<yourdatabasename>’);
define(‘DB_USER’, ‘<yourusername>’);
define(‘DB_PASSWORD’, ‘<yourpassword>’);Next, you will replace the “Authentication Unique Keys and Salts” definitions. Scroll to the first “define” key and use CTRL-K to delete that line. Do that for all the “define” keys.
Now, you will go to where you saved the results from the $ curl -s https://api.wordpress.org/secret-key/1.1/salt/ command and copy them. Then go back to where you deleted the “define” keys and paste then new results in.
It should then look something like this:
define('FS_METHOD','direct');10. And you are done! All that is left is to open your browser and navigate to your <FDQN> and follow the steps for setting up WordPress.
If you found this tutorial useful, or see any errors, typos, etc. please leave me a comment! Thanks and enjoy!

Comments
Post a Comment