Host multiple WordPress instances using a single public IP on a single machine.
Go To Solution: How to configure Name Based Virtual Hosting for WordPress
Since WordPress requires port 80 within the IP panel detailed in my last post, we can’t point to the jail with port forwarding.
Part 1: Port Based NAT
Originally, my plan was to set up multiple jails with independent static IPs. Then, using port forwarding on my router, I can point the DNS to the single public IP of my home along with the port number and route to a jail’s IP.
|Jail||Internal IP||External Port||Port Forwarding Rule on internal Router|
|Jail 1||192.168.4.11||991||Listen Port 991, Redirect to 192.168.4.11:80|
|Jail 2||192.168.4.12||992||Listen Port 992, Redirect to 192.168.4.12:80|
|Jail 3||192.168.4.13||993||Listen Port 993, Redirect to 192.168.4.13:80|
This option did work for websites created from scratch – text editor, regular HTML and CSS. I was able to forward a domain to my FreeNAS box, along with another website we created for school. I could repeat this process however many times I want, but that would mean a large number of port forwarding rules on the router. Furthermore, as explained above, WordPress requires resources to be accessible with the A NAME. A NAMEs in the DNS can’t specify ports, and as a result, this form of port-based NAT cannot work with WordPress.
Part 2: Virtual Hosting
This solution requires us to either start all our websites from scratch or to migrate our existing content over. This means we’d have to reimport our plugins, themes, content, and FAMP stack base. The FAMP stack only needs to be reinstalled once.
Virtual hosting allows Apache (web server component) to redirect visitors to a specific part of the web server jail. There are a variety of ways to configure Virtual hosting:
- Name Based
- IP Based
- Port Based
Since the original scenario is that we only have a single IP, and Port based NAT doesn’t work for WordPress, our only option is to use Name-based NAT. By retrieving the URL received by Apache, it can specify which directory to serve back to the visitor.
The main steps to host multiple WordPress sites under one IP are
- Create folders
- setup permissions
- Adding <VirtualHost> directives in httpd.conf
- Give access to the directories to the webserver in httpd.conf
- Creating prerequisite databases
- Install WordPress in each folder
To setup Virtual hosting, follow these steps within SSH to the jail:
Access the jail.
#jls #jexec [jail #]
Create the individual directories
Create folders within the apache24 folder.
#mkdir /usr/local/www/apache24/website1 #mkdir /usr/local/www/apache24/website2 #mkdir /usr/local/www/apache24/website3
Set permissions and ownership to the www user and group; repeat for all other directories you just created.
#chmod 755 /usr/local/www/apache24/website[...] . #chown www www /usr/local/www/apache24/website[...]
Configuring Virtual hosting
Edit the httpd.conf to setup virtual hosting
Under the listening port, add these directives. it should look something like this
Listen 80 <VirtualHost *:80> ServerName 127.0.0.1 DocumentRoot /usr/local/www/apache24/data </VirtualHost> <VirtualHost *:80> DocumentRoot /usr/local/www/apache24/data ServerName www.domainname.com ServerAlias domainname.com </VirtualHost> <VirtualHost *:80> DocumentRoot /usr/local/www/apache24/website1/ ServerName www.website1domain.com ServerAlias website1domain.com </VirtualHost> <VirtualHost *:80> DocumentRoot /usr/local/www/apache24/website2/ ServerName www.seconddomain.ca ServerAlias website2domain.ca </VirtualHost>
This lets apache know, when the browser requests for domainname.com, it will point to the files within the data folder. When the browser requests for website1domain.com, it will direct to files in the website1 folder we created earlier. Of course, we can change the bolded text as we see fit.
We know have to allow external users to access each folder, so within the same httpd.conf file, we need to add the following directives.
Look for the <Directory> directive that is already setup. It should look something like this:
<Directory /> AllowOverride none Require all denied </Directory>
Add the following under that Directory directive, replacing the bold with the folders you created in the previous steps:
<Directory /usr/local/www/apache24/data/> AllowOverride none Require all granted </Directory> <Directory /usr/local/www/apache24/website[...]/> AllowOverride none Require all granted </Directory>
Add as many directory directives replacing the bold if you want more hosted sites.
The “denied” attribute denies all access to the root folder (we want this for security reasons) and the granted allows access to the individual folders, otherwise we get a 403 Forbidden error.
Install WordPress into each folder
To get multiple WordPress instances working, we need to create separate databases for each instance.
Log in to mysql
mysql -u root -p
Create Database for each website
CREATE DATABASE wordpressforwebsite1;
CREATE DATABASE wordpressforwebsite2;
Create a user for WordPress to use. You can create multiple users to specify permissions for each website.
CREATE USER wordpressuser@localhost IDENTIFIED BY 'password';
Give the user privileges to alter the database
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;
Flush to apply
Exit out of MySQL
To finalize the WordPress installation into each folder, follow the steps in in Phase two of this post.
Basically, we are directing apache to serve files in a certain folder. Within those folders would have configured wordpress ini files that specify which database, user, and passwords to use.