Recent Posts

cakephp

(Nerd) How to: Setup Virtual Hosts on Ubuntu 9.10 using Apache2 + MySQL5.X + PHP5

I have been working on a couple projects that are hosted on gitHub. As the scale and scope of the projects grow, I ended up needing to setup multiple virtual hosts on my development machine. It wasn’t hard, but the information online that I found was fairly dated, so I created this tutorial.

This entire tutorial is done after entering the ‘sudo -i’ command which allows us to act as the SUPER-USER for the entirety of our terminal session. You’ve been warned.

sudo -i

NOTICE!!! I am aware that we could use the ‘a2ensite’ and related commands. But for the sake of understanding the entire process, I will not be using it.

1) Install the required modules from the command line

apt-get install apache2 mysql-server mysql-client php5 php5-cli php5-mysql

2) Change the directory to /etc/apache2/sites-available

cd /etc/apache2/sites-available

3) If you run the ‘ls’ command while in the sites-available directory you should see the following

ls

Output

default  default-ssl

4) Copy the ‘default’ config to a site specific config. For this tutorial I am using dev.acloudtree.com.

cp default dev.acloudtree.com.conf

5) Make the application directory

mkdir /var/dev.acloudtree.com

6) Open the file with an editor of your choosing. I prefer VIM.

vi dev.acloudtree.com.conf

The output below is the entire file but I will discuss certain parts that we will need to edit.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
 
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
 
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog /var/log/apache2/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog /var/log/apache2/access.log combined
 
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
 
</VirtualHost>

7) Add the name of the server. This will be the name that you type in the URL field of your web browser (IE: Firefox)

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
	ServerName dev.acloudtree.com
...

8 ) Point the Virtual host to the correct directory

...
        DocumentRoot /var/dev.acloudtree.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
...

9) Also make the change here.

...
        <Directory /var/dev.acloudtree.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
...

10) So this is what your dev.acloudtree.com.conf file should look like when you are done. Write/Quite the file and we will move on.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
	ServerName dev.acloudtree.com
 
        DocumentRoot /var/dev.acloudtree.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/dev.acloudtree.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
 
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog /var/log/apache2/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog /var/log/apache2/access.log combined
 
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
 
</VirtualHost>

10a) IMPORTANT! Make a symbolic link in the sites-enabled directory

ln -s /etc/apache2/sites-available/dev.acloudtree.com.conf /etc/apache2/sites-enabled/000-dev.acloudtree.com.conf

11) We need to edit our /etc/hosts file

vi /etc/hosts

Output

127.0.0.1       localhost 
127.0.1.1       servername
 
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

12) Right beneath the ‘localhost’ definition, add the following

127.0.0.1      dev.acloudtree.com

13) The complete file looks like this.

127.0.0.1       localhost
127.0.0.1       dev.acloudtree.com
127.0.1.1       servername
 
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

14) Create an index.php file in /var/dev.acloudtree.com for testing purposes.

vi /var/dev.acloudtree.com/index.php

Contents of .php file.

<?php
 
        echo 'Jared Folkins\' tutorial on acloudtree.com really works!';
        phpinfo();
 
?>

15) Reboot the apache2 process.

/etc/init.d/apache2 restart

16) Now open up your web browser and enter dev.acloudtree.com into the URL bar and it should work!

UPDATE: I forgot to mention that in this environment, I like to configure log files for each virtual host. This is our current dev.acloudtree.com file.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
	ServerName dev.acloudtree.com
 
        DocumentRoot /var/dev.acloudtree.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/dev.acloudtree.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
 
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog /var/log/apache2/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog /var/log/apache2/access.log combined
 
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
 
</VirtualHost>

Just change the ErrorLog and CustomLog names to match the virtual host.

...
        ErrorLog /var/log/apache2/dev.acloudtree.com_error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog /var/log/apache2/dev.acloudtree.com_access.log combined
...
</VirtualHost>

Beginning Cakephp – Errata : Pg 106

The findByYear() function is messed up in the book. Thought I would post the correction to remind myself.

Correct!

function findByYear($year=null) {      
	$date = $year.'-01-01 00:00:00';
	$end_date = $year.'-12-31 23:59:59';     
	return $this->find('all',        
		array(
			'conditions' => 
			array( 
				'DATE(Post.date) >' => $date,
				'DATE(Post.date) <' => $end_date          
			)       
		 )
	);   
}


Incorrect!

function findByYear($year=null) {
	$date = $year.'-01-01 00:00:00';
	$end_date = $year.'-12-31 23:59:59';
	return $this->find('all', 
		array(
			'conditions' => 
			array(
				'DATE(Post.date)' => '>'.$date,
				'DATE(Post.date)' => '<'.$end_date
			)
		)
	);
}

Ubuntu + VIM + CakePHP 1.2 + .ctp syntax highlighting

Found this blog post concerning setting up the Cakephp view extension (.ctp) to use the same highlighting as (.php).

blog

I didn’t find his solution that helpful, but one of the comments said to add this to my ‘.vimrc’ file in my home directory.


if has(”autocmd”)
autocmd BufEnter *.ctp set syn=php
endif

And since we are on the topic here is how my .vimrc is setup

set tabstop=2
set shiftwidth=2
set expandtab

if has("autocmd")
autocmd BufEnter *.ctp set syn=php
endif

syn on

-later

Cakephp + Ubuntu 8.04 + Apache2 Mod_rewrite

This tutorial assumes that you installed the basic LAMP stack that ubuntu comes with. It is also assumed that this is a development installation of CakePHP

1) Download CakePHP. My directory after I uncompress it is called ‘cake_1.2.1.8004′

*optional
2) Rename ‘cake_1.2.1.8004′ to ‘cake’

3) Move the ‘cake’ directory into ‘/var/www/’ so your full path becomes ‘/var/www/cake/’

4) Enable mod_rewrite

username@ubuntu:/$ sudo a2enmod rewrite
Enabling module rewrite.
Run '/etc/init.d/apache2 restart' to activate new configuration!

5) Enable appropriate rules in ‘/etc/apache2/sites-available/default’

username@ubuntu:/$ sudo vi /etc/apache2/sites-available/default

5a) Change the following

Directory /
Options FollowSymLinks
AllowOverride None
/Directory

To


Directory /
Options FollowSymLinks
AllowOverride All
Directory

5b) Change the following

Directory /var/www/
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
Directory

To


Directory /var/www/
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
/Directory

6) Restart apache2

username@ubuntu:/$ sudo /etc/init.d/apache2 restart

default screenshot