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

Installation

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
</VirtualHost>

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 : 127.0.0.1 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.

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir=/dev/null

 

 Restart Apache

 sudo service apache2 restart

 

PHP convert json to CSV – explained

Sometimes you need to convert a json string to CSV in PHP. For example we want to get the names Norrmalm and Liljeholmen (cities names at Sweden) from the following json string:

  '{"Norrmalm":"502","Liljeholmen":"491"}';

Here is the simplest solution:

$jsonStr='{"Norrmalm":"502","Liljeholmen":"491"}';
$csv = str_replace(array('[',']'), '', json_encode(array_keys(json_decode($jsonStr, true))))
echo $csv;

It will print out :

 "Norrmalm","Liljeholmen"

How it works ?

json_decode($jsonStr, true)
Look at the first inner most parentheses (json_decode), it converts the jsonStr to PHP array like:

 array(
   "Norrmalm"=>"502",
   "Liljeholmen"=>"491",
 );

array_keys(json_decode($jsonStr, true))
Now look at the second inner most parenthesis (array_keys) will return a new array containg only keys of the previous above array, it will look like:

 array(
   "Norrmalm",
   "Liljeholmen",
 );


json_encode(array_keys(json_decode($jsonStr, true)))

Now looking at the third inner most parenthesis (json_encode) will convert the above array to json string, it will add quotation marks arround the values, and will look like:

 
  ["Norrmalm","Liljeholmen"]

str_replace(array(‘[‘,’]’), ”, json_encode(array_keys(json_decode($jsonStr, true))))
Finally looking at the outermost parenthesis (str_replace), because we want to remove the square brackets from the above we need to replace both ‘[‘ and ‘]’ from the above, and this could be achieved by a single str_replace function by providing the search string as an array like array(‘[‘,’]’). So this final function will return the following:

 "Norrmalm","Liljeholmen"