Ubuntu 14.04 LTS install Apache2-PHP-MySQL and configure Virtual Hosts


1. Installing MySQL 5

sudo apt-get install mysql-server mysql-client

2. Installing Apache2

sudo apt-get install apache2

3. Installing PHP5

sudo apt-get install php5 libapache2-mod-php5

sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

sudo apt-get install php5-xcache

4. Restart Apache

sudo /etc/init.d/apache2 restart

Configure Virtual Host

1. Make Directory structure

i. Inside /var/www create a directory with the name of virtual host, e.g. if VH is site.loc then create a directory inside /var/www/site.loc

ii. Keep all files inside /home/user/projects e.g. all files for site.loc are located under /home/user/projects/site_loc

iii. Point a link to your code directory i.e make a link inside /var/www/site.loc as follows:

sudo ln -s /home/user/projects/site_loc/root/frontend/www public_html

iv. Grant Permissions
Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

    sudo chown -R $USER:$USER /var/www/site.loc/

The $USER variable will take the value of the user you are currently logged in as when you press “ENTER”.

We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

    sudo chmod -R 755 /var/www

 2. Create Virtual Host file

i. Enter the following command to create the file:

    sudo nano /etc/apache2/sites-available/site.loc.conf

And enter this inside the above file, save and exit.

<VirtualHost *:80>
        ServerAdmin admin@yahoo.com
        ServerName site.loc
        ServerAlias www.site.loc
        DocumentRoot /var/www/site.loc/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

ii. Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable each of our sites like this:

    sudo a2ensite site.loc.conf

iii. Edit host file.

    sudo nano /etc/hosts

and enter : site.loc

Other Configurations

i. Enable mode rewrite

sudo a2enmod rewrite

ii. Enable allowOverride

sudo nano /etc/apache2/apache2.conf

and find a section like :

       <Directory /var/www/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride None

and change AllowOverride None to AllowOverride All

iii. Enable .htaccess

For Yii framework:

   RewriteEngine on

   # if a directory or a file exists, use it directly
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d

   # otherwise forward it to index.php
   RewriteRule . index.php

And then run :

    sudo a2enmod rewrite

 Configure xDebug

    sudo apt-get install php5-xdebug
sudo service apache2 restart
sudo gedit /etc/php5/apache2/conf.d/20-xdebug.ini

and change add the following lines.



 Restart Apache

 sudo service apache2 restart


Leave a Reply

Your email address will not be published. Required fields are marked *