Sunday, November 2, 2014

Apache2 Web Server on Ubuntu 12.04 LTS

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname
hostname -f

Install Apache2
Make sure your package repositories and installed programs are up to date by issuing the following commands:

apt-get update
apt-get upgrade


Enter the following command to install the Apache 2 web server, its documentation and a collection of utilities.

apt-get install apache2 apache2-doc apache2-utils

Install Support for Scripting

The following commands are optional, and should be run if you want to have support within Apache for server-side scripting in PHP, Ruby, Python, or Perl.

To install Ruby support, issue the following command:

apt-get install libapache2-mod-ruby

To install Perl support, issue the following command:

apt-get install libapache2-mod-perl2

To install Python support, issue the following command:

apt-get install libapache2-mod-python

If you need support for MySQL in Python, you will also need to install Python MySQL support:

apt-get install python-mysqldb

Your PHP application may require additional dependencies included in Ubuntu. To check for available PHP dependencies run "apt-cache search php", which will provide a list of package names and descriptions. To install, issue the following command:

apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

Issue the following command to install the php5-suhosin package, which provides additional security to your PHP installation:

apt-get install php5-suhosin

If you're also hoping to run PHP with MySQL, then also install MySQL support:

apt-get install php5-mysql

Configure Name-based Virtual Hosts

There are different ways to set up Virtual Hosts, however we recommend the method below. By default, Apache listens on all IP addresses available to it.

First, issue the following command to disable the default Apache virtual host.

a2dissite default

Each additional virtual host needs its own file in the /etc/apache2/sites-available/ directory. In this example, you'll create files for two name-based virtually hosted sites, "example.net" and "example.org".


File:
/etc/apache2/sites-available/example.net

add


     ServerAdmin webmaster@example.net
     ServerName example.net
     ServerAlias www.example.net
     DocumentRoot /var/www/example.net/public_html/
     ErrorLog /var/www/example.net/logs/error.log
     CustomLog /var/www/example.net/logs/access.log combined


If you would like to enable Perl, add the following lines to the VirtualHost entry above.

File excerpt:/etc/apache2/sites-available/example.net

Options ExecCGI
AddHandler cgi-script .pl


Create required directories for these sites by issuing the following commands:

mkdir -p /var/www/example.net/public_html
mkdir /var/www/example.net/logs


Enable the sites by issuing these commands:

a2ensite example.net
Finally, restart the Apache server to initialize all the changes, with this command:

service apache2 restart

When you create or edit any virtual host file, you'll need to reload the config, which you can do without restarting the server with the following command:

sudo service apache2 reload

You now have Apache2 installed on your Ubuntu 12.04 VPS and have configured the server for virtual hosting.