Tutorial, Ubuntu 13.04 Apache2 setup public_html (local sites). This tutorial was done using a fresh install of Ubuntu 13.04.
So what is this good for? Well my use case is that I have been doing more and more web development using Drupal and wanted to be able to create multiple development sites on my laptop. For instance, let say you have a couple of websites, site1.com and site2.net and you want to create a development environment locally on your laptop but under your user account. If your user name is John, you want to be able to put your sites inside your home folder like ~/john/public_html/site1 but then be able to just type the address “site1.local” on your browser to access the site. This makes developing with Drupal and Drush, much easier than trying to access the /var/www directory as a standard user.
What do we need?
- Ubuntu 13.04 installed (don’t see why it would not work on 12.10)
- a pinch of patience…
Next, install Apache2:
sudo apt-get install apache2
Next, install MySQL Server (Although this is not really needed for the web server set up, it is needed for Drupal development, so may as well install it!)
sudo apt-get install mysql-server mysql-common mysql-client
Next, install PHP5
sudo apt-get install php5-common php5-gd libapache2-mod-auth-mysql php5-mysql apache2-mpm-prefork libapache2-mod-php5 php5 php5-cli
Next, create the public_html directory in your home directory and two site directories.
mkdir ~/public_html/site1 mkdir ~/public_html/site2
sudo chmod a+x ~/public_html
This will prevent many “Access Denied” or Permissions errors when trying to access your local sites.
Next we need to apply permissions to the public_html directory for the group www-data.
sudo chown -R www-data:www-data /home/$USER/public_html sudo chmod -R 775 /home/$USER/public_html
Then add your user account to www-data group
sudo adduser $USER www-data
Note: leaving “$USER” in the command will automatically use your current user account.
RESTART UBUNTU! For the permissions to take affect…
Next, you are going to create some miscellaneous but helpful files. First, in the new public_html directory, create an empty file called index.html and add the following to it, including the asterisks; You can use nautilus for this, just navigate to your home directory and then to public_html, right click and “New Document > Empty Document”
Inside your ~/public_html directory, create a file called index.html and paste the following in it.
***This is your local website server under ~/public_html***
Inside site1 create another index file called index.html and add the following;
***This is your local <b> Site1</b> website server under <b><u>~/public_html</u></b>***
Inside site2 create another index file called index.html and add the following;
***This is your local <b> Site2</b> website server under <b><u>~/public_html</u></b>***
The reason it is good to have these files in place is in order to verify the web server is correctly configured. You should have three files in place for testing;
Next, enable Apache2 userdir module and then restart Apache2
sudo a2enmod userdir sudo service apache2 restart
By default the user directory module is not enabled in Ubuntu 13.04 and there are some lines that need to be commented out in the php.conf.
Edit the file using gedit:
sudo gedit /etc/apache2/mods-available/php5.conf
Next, you will actually enable your site1 directory to be served by Apache2!
But first, a synopsis of what we are going to do. Apache2 configuration files are kept in /etc/apache2/ and in the Apache2 directory are two directories specific to this task, one is “sites-available” and the other is “sites-enabled”. Inside sites-available is a default configuration file, this file should not be modified unless you know what you are doing. The default file configures the site located in /var/www. But what we need to do is use it as a template. We will copy the file and rename it. Once we do that, then we have to enable the site so that Apache2 knows there is a new site to look for. When we enable the sites, a symbolic link is added to the sites-enabled directory. Files in the sites-enabled directory should not be modified manually! Always restart Apache2 after making any modifications, otherwise they will not take effect until a reboot.
So let’s get started creating SITE1
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1
Next, we need to edit the newly created file and configure it for “site1”.
sudo gedit /etc/apache2/sites-available/site1
- The ServerAdmin can be changed to your email, or one that is for that site or leave it as is.
- Add one line right below the ServerAdmin line:
- This will allow the site to be called from the browser window just by typing site1.local
- DocumentRoot needs to be changed to;
DocumentRoot /home/john/public_html/site1 (no trailing slash)
- <Directory /var/www needs to be changed to;
<directory /home/john/public_html/site1/ (with trailing slash)
Everything else can be left alone…
Note: if you wish to have “Clean URL’s” enabled in Drupal, you need to change “AllowOverride None” to “AllowOverride All” and enable the mod_rewrite module by issuing the following command;
sudo a2enmod rewrite
Next, edit the hosts file to reflect the new site1.local entry in the site config file.
Add the following right below the localhost entry, “127.0.0.1 site1.local”
sudo gedit /etc/hosts
Next, enable the site and restart Apache2
sudo a2ensite site1 sudo service apache2 restart
Now that the system is configured, adding site2 is a breeze!
Here are the steps;
- create new site from template
- add entry to /etc/hosts file
- enable site2
- restart apache2
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site2 sudo gedit /etc/apache2/sites-available/site2 sudo gedit /etc/hosts
Enable site2 and restart apache2
sudo a2ensite site2 sudo service apache2 restart
If anyone catches any typos, please let me know.