This blog is about C++, CakePHP, Linux, PEAR, PHP, Pyrus, Apache, and MySQL and related topics.

Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Saturday, November 21, 2009

How To Create Name-Based Virtual Hosts In Apache If You Are Using WAMP

This article will show you how to create name-based virtual hosts (vhosts) in Apache if you are running Apache as your web server on a Windows machine using Wampserver 2.0i (WAMP). (Wampserver installation is very easy!)


If you would like a more in-depth discussion of virtual hosts, refer to Apache Virtual Host Documentation


If you are using the Zend Framework and/or Ubuntu you may prefer to follow this article: Appendix A. Creating A Local Domain Using Apache Virtual Hosts from Surviving the Deep End!,a free book about the Zend Framework for the PHP language


Virtual hosts lets us create new domains for each project and removes the need to maintain everything as a set of subdirectories under localhost. 1


Step 1: Edit httpd.conf


In the httpd.conf file, located at c:\wamp\bin\apache\apache2.2.11\conf\httpd.conf, locate the following line and make sure it is uncommented, meaning there is no '#' at the beginning of the line. This is how it should look:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Step 2: Edit httpd-vhosts.conf

The httpd-vhosts.conf file is located at c:\wamp\bin\apache\apache2.2.11\ conf\extra\httpd-vhosts.conf
Here is a sample httpd-vhosts.conf file:
# Setup Listening Port
NameVirtualHost *:80

# Ensure "localhost" is preserved
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "C:\wamp\www"
</VirtualHost>

# Setup "helloworld" Virtual Host
<VirtualHost *:80>
    ServerName helloworld.tld
    DocumentRoot "C:\projects\helloworld\public"

    <Directory "C:\projects\helloworld\public">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
Explanation:


Any line that begins with '#' is ignored by Apache. This is a comment-line.


The NameVirtualHost directive specifies the IP-address (and possibly the port) that will be accepting requests for the hosts. '*' is a wild-card meaning that any and all IP-addresses on the web-server should be used. The colon ':' separates the IP-address from the port number. '80' means the web-server will accept requests on port 80.


Next, there are two <VirtualHost> blocks, one for each domain. The argument for the <VirtualHost> directive should be the same as the argument to the NameVirtualHost directive (ie, an IP address, or * for all addresses). Inside each <VirtualHost> block, you will need at minimum a ServerName directive to designate which host is served and a DocumentRoot directive to show where in the filesystem the content for that host lives.


If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.2


For the first block we are just making sure that localhost (existing host discussed above) is preserved:

# Ensure "localhost" is preserved
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "C:\wamp\www"
</VirtualHost>
 In the second <VirtualHost> block we create the "helloworld" virtual host:




# Setup "helloworld" Virtual Host
<VirtualHost *:80>
    ServerName helloworld.tld
    DocumentRoot "C:\projects\helloworld\public"

    <Directory "C:\projects\helloworld\public">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>


The final part is to add a Directory configuration for the new Virtual Host's Document Root. This ensures Apache can serve requests from this directory by supplying appropriate permissions. Apache will not serve files from a directory without the correct access permissions. By default, options are severely limited to prevent unauthorised access from the web, so be sure to add this to allow files to be served from the Document Root. The options I'm using here are the usual ones Apache applies to localhost's Document Root which are fairly liberal and allow for the use of .htaccess files so users can access Apache configuration options at runtime.3>


Step 3: Configuring the Local HOSTS file.


The new domain must also be 'mapped' to the correct local IP. This is easily resolved by adding the new domain name to the local HOSTS file which recognises and maps local domains to IP addresses


In Windows this file is located at  C:\Windows\System32\drivers\etc\hosts


This is what the file looks like:




# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
127.0.0.1       helloworld.tld 
Sources:






Apache Virtual Host Documentation http://httpd.apache.org/docs/1.3/vhosts/


Apache Name-Based Virtual Hosts
http://httpd.apache.org/docs/1.3/vhosts/name-based.html


Footnotes:
1 and 3 :From "Creating A Local Domain Using Apache Virtual Hosts"


2. From "Using Name-based Virtual Hosts"

Thursday, December 18, 2008

How to install CakePHP on Linux Ubuntu 8.10 (Step 3.3)

In this blog post I am going to show how I installed CakePHP on my Ubuntu Linux computer. CakePHP does have instructions on how to do this but even with the instructions there are several points where you can run into problems, so I'm writing this to help anyone with a similar setup and also to remember what I did!

I intend this to be a companion to the installation guide that can be accessed here (cake guide).

My setup:
(step 3.1)

Linux Ubuntu 8.10
Apache 2.2.9-7ubuntu3
PHP 5.2.6-2ubuntu4
MySQL 5.0.67-0ubuntu6

(To keep things simple I will not list all the modules installed)

mod_rewrite

To enable mod_rewrite I typed the following in the Terminal:

sudo a2enmod rewrite


Get CakePHP: (Step 3.2.1)

Getting CakePHP was easy I just went to the main website and downloaded the latest version which at the time of this article is version 1.2.0.7692 RC3.

There are four different files to choose from, I chose cake-1.2.0.7692-rc3.tar.gz

I downloaded the tar.gz file to my desktop

Installation Options: (Step 3.3)

There are three main installation types, Development, Production, and Advanced. Development is the easiest option, but is less secure. Production allows an entire domain to act as a single CakePHP application, is very secure, and has clean URL's. The Advanced option is more complicated and requires more configuration.

I decided that Production is the way to go.

Switching to the Terminal I created a cake_install directory as follows:

cd /var
sudo mkdir cake_install

Next, I went to the desktop and extracted the files there (right click). Then using the Terminal I copied the contents into the cake_install directory like this:

cd /home/jon/Desktop/cake_1.2.0.7692-rc3
sudo cp -r * /var/cake_install

Now my directory structure looks like this:

/var/cake_install/app/
/var/cake_install/cake/
/var/cake_install/docs/
/var/cake_install/vendors/
/var/cake_install/index.php

/app/tmp permissions: (Step 3.2.2)

Once I got here I went back to step 3.2.2 to make the /app/tmp folder writable by the web server user.

In the Terminal now, I navigated to the app folder to verify the current permissions:

cd /var/cake_install/app/
ls -ld tmp

and this is the output:

drwxr-xr-x 6 www-data www-data 4096 2008-12-18 11:30 tmp

If you see root instead of www-data then you need to type this command in the terminal:

sudo chown www-data:www-data /var/cake_install/app/tmp


Almost done. The next step was to change the DocumentRoot to /var/cake_install/app/webroot

(how to create a new site in Apache is described here)

In the Terminal I typed:

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite
sudo nano /etc/apache2/sites-available/mysite

I changed the DocumentRoot to point to: /var/cake_install/app/webroot

Then I saved the file. (like this: Ctrl-X to exit, Y to save, then enter to confirm address of file.)

Then I had to deactivate the old site and activate the new one.

In the Terminal I typed:

sudo a2dissite default && sudo a2ensite mysite

then to reload Apache so the new configuration will be in effect, I typed:

/etc/init.d/apache2 reload

To verify it is working correctly you type www.example.com in your browser where example is your domain. If you are running as localhost then type http://localhost/.

If your web server is configured correctly, you should now find your Cake application accessible there.

If there are any errors in your installation they will show up on this screen.

I had the following errors to fix before I could consider the job complete:

Warning 512: /var/cake_install/app/tmp/cache/ is not writable [CORE/cake/libs/cache/file.php, line 262]

Notice 1024: Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your application [CORE/cake/libs/debugger.php, line 541]

Your database configuration file is NOT present.
Rename config/database.php.default to config/database.php


I found the solution to Warning 512 , on SitePoint's forums which references a post on the CakePHP Google Group. To fix this you go to the app directory like this:

cd /var/cake_install/app/
then as administrator you fix the permissions like this:
sudo chmod -R 0777 tmp

I reloaded the browser and the error is now gone.

Next to fix Notice 1024 I found this solution.


The solution says to open the core.php file at app/config and change one digit in the salt. So first I navigated to the right folder:

cd /var/cake_install/app/config

Then to change the permissions so that I could edit the file I did this:

sudo chown jon:jon /var/cake_install/app/config/core.php

To open and edit the file I typed:

gedit core.php

I scrolled down to Security.salt. Then I changed one character in the salt, saved, then closed the file. I reloaded the browser. Problem fixed!

Two down one to go.


Now my last little problem is this one:

Your database configuration file is NOT present.
Rename config/database.php.default to config/database.php

I used the Terminal to rename the file. For info, type man mv in the Terminal or refer to this.

sudo mv /var/cake_install/app/config/database.php.default /var/cake_install/app/config/database.php

Problem fixed!

A new message pops up that "Cake is NOT able to connect to database" and I'll deal with that seperately, I'm just glad to make it this far.





Tuesday, December 16, 2008

How to install Smarty template

My first attempt to install the Smarty Template Engine was to follow the basic installation instructions from the Smarty manual. The very first sentence says to: "Install the Smarty library files which are in the /libs/ sub directory of the distribution." What? I was lost right from the get-go. I'm one of those guys that likes clarity. This is not clarity.

So, I decided to go the easy route. Since I have Ubuntu Linux 8.10 running on my computer, the easy way would be to install the Smarty Template Engine package using the Synaptic Package Manager (System > Administration > Synaptic Package Manager). I did a quick search for Smarty and sure enough it was there so I installed it. This was very easy to do.

So, going back to the basic installation instructions I created the following script and tried to run it.

// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
?>


And this was the result:

Warning
: require_once(Smarty.class.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/smartytest.php on line 3

Fatal error: require_once() [function.require]: Failed opening required 'Smarty.class.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/smartytest.php on line 3

I was prepared for this since the instructions warned me this might happen and it also gave four possible solutions.

Again I decided to go with the easy solution and set the include_path as follows:

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; *nix: "/path1:/path2"
include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/"

; Windows: "\path1;\path2"
include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"

This didn't work either, I got the same error message. So I figured it must be in a different folder and all I have to do is find the folder. Going back into the Synaptic Package Manager I located Smarty again and by right clicking Smarty and selecting properties I was able to see the location of the installed files. I thought that with the correct path I would get Smarty up and running. Nope.

I searched in the forums at ubuntuforums.org and didn't find anything useful.

I searched the Smarty forums and found this thread. There lokie gives four steps to installing Smarty but again I found myself lost because it wasn't 100% clear what to do.

That's when I decided I would have to write this post because there seems to be two kinds of PHP programmers out there. Those who don't need very detailed explanations and those that do. I'm in the second category and judging by the messages I see in the forums I am not the only one.

To get Smarty up and running this is what I did:

1. I went to the download page on Smarty's website, located at: http://www.smarty.net/download.php

2. I downloaded the most recent stable version which at the time of writing is: Smarty 2.6.20 (.tar.gz) (.zip) August 15th, 2008

3. I created a folder Smarty at /var/www/smarty to temporarily hold my files

4. I extracted the contents of the .tar.gz file into the smarty folder.

5. Inside the Smarty folder is one folder called Smarty-2.6.20, and inside this folder there are four folders and ten files.

The folder were demo, libs, misc, and unit_test.
The files were BUGS, Changelog, COPYING.lib, FAQ, INSTALL, NEWS, QUICK_START, README, RELEASE_NOTES, and TODO.

6. I opened and read QUICK_START and decided to follow these instructions as close as possible.

7. I made my directory structure as follows: /usr/local/lib/php/Smarty/. I started out with only the following: /usr/local/lib/ so I had to create two directories which I did using the terminal, like so:

$> cd /usr/local/lib/
$> mkdir /usr/local/lib/php/
$> mkdir /usr/local/lib/php/Smarty/


Once the directories are set up now we will copy the library files from the download and put the copies in the Smarty folder. I did this using the terminal:


$> sudo cp -r /var/www/smarty/Smarty-2.6.20/libs/* /usr/local/lib/php/Smarty


Now my directory and file structure for Smarty should look like this:


/usr/local/lib/php/Smarty/
Config_File.class.php
debug.tpl
internals/
plugins/
Smarty.class.php
Smarty_Compiler.class.php


Now going back to the QUICK_START instructions. The next step is to create four directories that Smarty needs to work.

my document root is var/www/website so I decided to put the Smarty files under /var/www/website/smarty. Using the terminal again I create the directories as follows:
$> cd /var/www/
$> mkdir smarty
$> mkdir smarty/templates
$> mkdir smarty/templates_c
$> mkdir smarty/cache
$> mkdir smarty/configs
$> sudo chown user:pass smarty/cache
$> sudo chown user:pass smarty/templates_c
$> chmod 775 smarty/templates_c
$> chmod 775 smarty/cache
The next step now is to create the Smarty PHP script:

$> cd /var/www/
$> mkdir myapp
$> gedit index.php
The gedit text editor opens and you cut and paste the following and save the file at index.php inside the myapp directory.

template_dir = '/web/www.domain.com/smarty/templates';
$smarty->compile_dir = '/web/www.domain.com/smarty/templates_c';
$smarty->cache_dir = '/web/www.domain.com/smarty/cache';
$smarty->config_dir = '/web/www.domain.com/smarty/configs';

$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');

Change the values for your own specific situation. My webroot is located at /var/www and therefore my values were:

template_dir = '/var/www/smarty/templates';
$smarty->compile_dir = '/var/www/smarty/templates_c';
$smarty->cache_dir = '/var/www/smarty/cache';
$smarty->config_dir = '/var/www/smarty/configs';

$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');

Save as index.php in the myapp folder.

Now we setup the Smarty Template, here is our template:

(click image to enlarge)





Save this as index.tpl in this folder: /var/www/smarty/template/

Finally, it's time to test if the installation is working. Go into your browser and type http://localhost/myapp/index.php

If you see "Hello, Ned!" displaying on the screen it means you have successfully installed the Smarty Template Engine.

Once you succeed at this it's time to take the Crash Course on the Smarty website.

Followers