PHP, Mysql, and Apache Multiple/Virtual Hosts Setup on Ubuntu

This article contains the basic information to setup Mysql and Apache on an Ubuntu Linux VPS or Dedicated Server. This setup includes instructions on a virtual hosts setup with two main domains, one of which that has two subdomains. These instructions will likely need to be tailored to your local environment,


but the basics are here.

If you haven’t yet setup your server, please read our Ubuntu VPS/Diedicated Server Setup article to help get you started.

These instructions should work on any linux distribution and the apache configuration information should be nearly universal. Some of the install lines will change depending on your linux distribution but for debian based distro’s these should work just fine.

1. Login in to your server

2. Installation of Apache
sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
sudo nano /etc/apache2/apache2.conf
add ServerName yourserver
sudo apache2ctl graceful (domain name error is now gone)
sudo nano /etc/apache2/sites-available/default
uncomment - RedirectMatch ^/$ /apache2-default/
sudo /etc/init.d/apache2 reload

3. Installation of PHP
sudo aptitude install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd php5-imagick php5-mcrypt php5-memcache php5-mhash php5-mysql php5-pspell php5-snmp php5-sqlite php5-xmlrpc php5-xsl

sudo /etc/init.d/apache2 reload

4. Installation of MySQL
sudo aptitude install mysql-server and set server when asked "+++"

Now you have Apache, PHP, and MySQL installed and ready to be configured. Following is some basic Apache 2 Configurations for 2 Main Hosts, 1 with 2 Virtual Hosts. The file locations, etc. are all up to personal preference, but the following file locations are just as good as any. The apache settings in particular will need to be fine tuned for various situations, we use Drupal extensively - so these are sufficient for most small to medium sized Drupal (or other database driven CMS’s) sites.

5. Apache Configurations

sudo nano /etc/apache2/apache2.conf
  Timeout 300 -> 100
  MaxKeepAliveRequests 100 -> 200
  KeepAliveTimout 15 -> 5
  ServerTokens Full -> Prod
  ServerSignature On -> Off  

6. Apache2 Virtual Hosts Setup (using /srv/domain.com as document root - you can use anything else)
mkdir -p /srv/domainname.com/{public,private,logs,cgi-bin,backup}
mkdir -p /srv/sub.domainname.com/{public,private,logs,cgi-bin,backup}

Add the following to the bottom of /etc/apache2/apache2.conf

NameVirtualHost *:80

  <IfModule mod_ssl.c>
        NameVirtualHost *:443
  </IfModule>

sudo nano /etc/apache2/sites-available/default
Remove NameVirtualHost and change to listen to port 80
So you should have:
<VirtualHost *:80>
Server….
at the top of your file

7. Setup First Virtual Host (replace “domain1″ with your hostname)
sudo nano /etc/apache2/sites-available/domain1.com
Should look generally like:

++++Start File++++
# Place any notes or comments you have here
# It will make any customization easier to understand in the weeks to come

# domain: domain1.com
# public: /srv/domain1.com/

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@domain1.com
ServerName domain1.com
ServerAlias www.domain1.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /srv/domain1.com/public

# Custom log file locations
LogLevel warn
ErrorLog /srv/domain1.com/logs/error.log
CustomLog /srv/domain1.com/logs/access.log combined


++++End File++++

8. Setup Second Virtual Host (which also a domain alias and two independent Subdomains - replace hostname2., hostname2alias, sub1, and sub2 with your hostname, your top level domain alias, subdomain1 name, and subdomain2 name respectively)
sudo nano /etc/apache2/sites-available/hostname2.com
Should look generally like:

++++Start File++++
# Place any notes or comments you have here
# It will make any customisation easier to understand in the weeks to come

# domain: hostname2.com
# public: /srv/hostname2.com/

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@hostname2.com
ServerName hostname2.com
ServerAlias www.hostname2.com
UseCanonicalName Off
ServerAlias hostname2alias.com
ServerAlias www.hostname2alias.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /srv/hostname2.com/public


Options Indexes FollowSymLinks +Includes
AllowOverride All
Order allow,deny
allow from all

# Custom log file locations
LogLevel warn
ErrorLog /srv/hostname2.com/logs/error.log
CustomLog /srv/hostname2.com/logs/access.log combined

ScriptAlias /cgi-bin/ /srv/hostname2.com/cgi-bin/

# Admin email, Server Name (domain name) and any aliases
ServerName sub1.hostname2.com
ServerAdmin webmaster@hostname2.com
ServerAlias www.sub1.hostname2.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /srv/hostname2.com/subdomains/sub1/public

# Custom log file locations
LogLevel warn
ErrorLog /srv/hostname2.com/subdomains/sub1/logs/error.log
CustomLog /srv/hostname2.com/subdomains/sub1/logs/access.log combined

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@hostname2.com
ServerName sub2.hostname2.com
ServerAlias www.sub2.hostname2.com
ServerAlias sub2.hostname2alias.com
ServerAlias www.sub2.hostname2alias.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /srv/hostname2.com/subdomains/sub2/public

# Custom log file locations
LogLevel warn
ErrorLog /srv/hostname2.com/subdomains/sub2/logs/error.log
CustomLog /srv/hostname2.com/subdomains/sub2/logs/access.log combined


++++End File++++

Now you can enable your sites - this command simply makes a symbolic link from sites-enabled to the configuration files you just setup in sites-available.

9. Enable Sites
sudo a2ensite domain1.com
sudo a2ensite hostname2.com
Then Restart Apache:
sudo /etc/init.d/apache2 reload

The next section involves enabling the apache module mod_rewrite (and any other modules you require for your particular setup)

10. Enable mod_rewrite
sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload