Sunday, October 19, 2008

Post 3 : APACHE Features and MySQL installation

Hi Guys,

After enabling PHP and APACHE on your Mac, its time to work on database. In this post we will discuss MySQL installation and will discuss few points about APACHE server.

As I had mentioned the topic of discussion in the last post, we will start from there only.

1. DATA SECURITY :

Showing the World

For security purposes, you should consider that anything you put in your WebServer/Documents folder will be available across the web. If you have information that you want to keep private, think twice about putting it there, unless you know how to protect it.
But, if you want people to see the pages that you are sharing, there can be a few obstacles. You can give out the URL that is listed in the sharing control panel under “Your computer’s website.” However, if you are behind a NAT router, such as I am, this IP address based url will only work for other computers on your network and not for the internet as a whole. You may have to configure network router or firewall in order to discover your true ip address and to route web server requests to that IP to your computer. Doing this is beyond the scope of this tutorial.
Additionally, IP address based urls don’t make good urls to share. IP addresses can change. If you plan to host a permanent web site, you may want to purchase a domain name and point it to your Mac.  

Perhaps the best option is to purchase both a domain name and professional hosting. Apache based PHP Hosting is widely available and cheap. You can get support from a good host on uploading your files to the remote server. I’m going to presume that you will use one of the many excellent PHP hosting options and are only configuring PHP on your own machine for education, testing or development purposes.

2. ENABLING A PERSONAL WEBSITE 

If you clicked on the URL under “Your Personal Website,” you might have gotten a page that says forbidden. This is because in the default configuration in Leopard, unlike in Tiger, does not allow Apache to serve documents from home directories. If you want to enable this feature, you have to create a new Configuration file.
Create a new file with the following contents and save it to /etc/apache2/users/navdeepmalik.conf.
 

  Options Indexes MultiViews FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all

 
Replace “navdeepmalik” with your user name, which is also the name of your home directory. Exact capitalization is imporant. This tells the Apache server that it is ok to serve web content out of the ~navdeepmalik directory. You will have to restart Apache for this to take effect.

You may also have to create a Sites folder in your home directory to hold the files you want to serve. Leopard will automatically bless this folder with a special Icon.


3. VIRTUAL HOSTING

If you want to experiment with or work on more than one site at a time, the single directory in WebServer Documents and the Personal Websites configuration don’t work well. Projects collide and files outside of your home directory can be harder to work with. The answer to this is to setup virtual hosting. Lets turn our Personal Website sharing solution into a virtual hosting solution that allows us to work with multiple websites as subdirectories of our Sites folder.
So, lets create a sample site, called mysite. We’ll create a folder called “mysite” as a sub folder of our Sites folder. Capitalization is important.
Now, we are going to want to access our site with an easy to use domain name, so that our url is http://mysite/. There is an easy way to create new domain names that are only for personal use. To do this, we can add it to our /etc/hosts file. Add the following lines at the end of this file:

# My local aliases
127.0.0.1 mysite

127.0.0.1 is a special IP address designation that never changes and corresponds to localhost to mean this computer. We are telling our Mac that the name mysite is hosted on the local computer. This rule is only in effect on the same machine. If you go to a different machine, you cannot use the http://mysite/ url.
Now we need to configure apache for virtual hosting. We are going to have to edit our /etc/apache2/users/navdeepmalik.conf file. Change the contents of this file to the following:
 
  Options Indexes MultiViews FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all
 
NameVirtualHost *:80
 
DocumentRoot /Users/navdeepmalik/Sites/mysite
  ServerName mysite


Remember to replace “navdeepmalikwith your user name. Place your info.php test file into the mysite directory and rename it to index.php. Now, restart your apache server. When you visit http://mysite/, you should now see the familiar php logo and information page.
If you want to add another site, just add a second line in your hosts file, another subdirectory of Sites and append the following to your apache configuration file:
 
DocumentRoot /Users/navdeepmalik/Sites/myothersite
ServerName myothersite

Showing the World, Part II :

Sharing your virtual hosted sites with the world is more complicated if you don’t have a domain name setup. You can, however, add your hosts files entries to other computers that you want to share with. However, you have to change the 127.0.0.1 IP address to the IP address of your computer, taking into account any NAT.

There is a special case of this. If you are using parallels, perhaps for test viewing your pages in internet explorer, you may want your virtual hosted sites to be available. The good news is that Windows also supports a hosts file. Here is how to edit your windows hosts file. The big problem is knowing what IP address to use. You can’t use 127.0.0.1 on the windows side because that is the virtual windows machine, not your Mac’s address. You can use the IP address shown on your network system preferences panel, 192.168.1.100 for me. But, this number is subject to change and you will have to re-edit your hosts file on the windows side.

If you are using Parellels, be sure to upgrade to the new beta version for Leopard, build 5540. Once you’ve done that, if you visit the network panel in system preferences and select the “Parallels Host-Guest” network, you will see the IP address that parallels assigns to your host machine. (assuming you are using Shared Networking.) You can then use this IP address in your windows hosts file. You may also be able to change “Using DHCP” to “Using DHCP with Manual address” and re-entering this number if you have a problem with the number changing. Here, my number is 10.37.129.3:



4. INSTALLING MYSQL

MySQL has a binary distribution for Mac OS X. They also have reasonably good documentation on installing MySQL on Mac OS X for their distribution. Note that Leopard specific packages for MySQL have not been created yet.

Starting MySQL

So far, the MySQL preferences panel from the Tiger release is broken and does not correctly start and stop MySQL (bug report. You can do this from the terminal window with

sudo /usr/local/mysql/support-files/mysql.server start

To shutdown the server type:
sudo /usr/local/mysql/support-files/mysql.server stop

If you find this tedious to type, you can download WebDevCP, which is a small AppleScript application that I made. Launching WebDevCP launches both Apache and MySQL. Quitting the application shuts them both down. usually. Launching and quitting requires a password. No warranty on this thing. It was just something I was using personally and figured others might find useful.

Bring the mysql.sock to PHP

One problem that has come about with MySQL and Leopard is the location of the mysql.sock file. Previously, the default location for this file was in the /tmp directory. That location has now moved to the /var/mysql directory. PHP will look for it there. Unfortunately, the default location from the MySQL will still place it in the old location. We can fix this by creating a my.cnf configuration file in the /etc directory. Save a file with the following contents to /etc/my.cnf:

[client]
socket = /var/mysql/mysql.sock
[mysqld]
socket = /var/mysql/mysql.sock


In the terminal window, type the following commands to create the directory for the sock file:
sudo mkdir /var/mysql
sudo chown _mysql /var/mysql


One drawback to this is that if you have installed the MySQL GUI tools, they will look for the mysql.sock file at the old location. You can enter the new socket in the connection dialog under More Options, there is a box labeled “connect using socket.” Just enter /var/mysql/mysql.sock.

Another solution is to change the php.ini file to expect the socket in a different location. I’m going with the my.cnf option because I expect the MySQL will have a Leopard version out in a few days that changes the default location.

Pear - Where is it..?

OS X has traditionally had problems with PEAR. Many point updates would overwrite the included version of PEAR with an older, and perhaps insecure version. Sadly, Apple has fixed this by not including PEAR at all in their OS. This is a big inconvenience for people wanting to use Apple’s default version of PHP, versus a third party distribution. So, lets get PEAR installed. Type the following in the terminal window to download the PEAR installer:
curl http://pear.php.net/go-pear > go-pear.php
after that, type
sudo php -q go-pear.php

To run it. Hit enter to select the default locations. PEAR will be installed, but it won’t be ready to use until we modify our php.ini file.

PHP .ini configuration

Now we need to make some changes to our php configuration file. Leopard has an empty configuration file by default, but provides a file which you can use as a template. From the terminal window, type:

sudo cp /etc/php.ini.default /etc/php.ini

Now, edit the /etc/php.ini file. Find the include_path setting:

;include_path = ".:/php/includes"

And change it to

include_path = ".:/usr/share/pear"

This enables our PEAR installation. You may also want to make some changes which will improve your ability to debug PHP. FInd the line that says

log_errors = Off
and change it to
log_errors = On
You have to then restart Apache for these PHP changes to go into effect.

5. ERRORS AND OMISSIONS  

Thats all there is to using the version of PHP delivered with OS X. If you find this confusing, you are probably better off with something like XAMPP or MAMP. I’ll probably end up compiling my own versions of PHP, but that is a different blog post. I’ve already had problems with this configuration when I tried to install XDebug via PECL. One last thing, if you run into problems, you can check the apache2 error_log file using the Console application.

For support, try the Apple’s Discussion Forums.

This is it. Now when you have successfully completed the toughest part , i.e installation of various components, you can start working in PHP with simple programs. In the next post we will start the with the PHP programming.

Thanks guys. 

Saturday, October 18, 2008

Post 2 : Setting Up PHP & APACHE on MAC OS X


Hi Guys,

Now when we are familiar with PHP, its time to move ahead and install the application. In this post we will focus on installation of PHP and APACHE on MAC and in next few posts we will learn More about APACHE and MYSQL installation .

MAC OS X is a great platform for working with PHP. Leopard comes with Apache, PHP and many other development tools,such as subversion already installed. This is a guide for setting up a PHP development envoirnment under 10.5 using the version of PHP that ships with leopard.

You may refer to use one of the 3rd party distributions of PHP, such as MAMP, XAMPP or Marc Liyanage. This is a guide to using the version of PHP that comes with 10.5.

Enable Developer Tools

These steps may not be strictly necessary for this process, but I find it useful to do them.

First, enable your root password.

You may also want to install XCode Tools from your Leopard disk(or grab the latest from Apple developer tools). The tools are required as you are going to compile any extension for PHP.

Editing Configuration Files

We will haeve to edit several configuration file that exists as a part of the unixy underpinnings of OS X. I am going to recommend the free text editor, TextWrangler for this purpose. Normally. the finder hides the configuration files from view. However, in the finder, you can use the "Goto Folder .." option under the "Go" ( Go>Goto Folder) . This option if available via command-shift-G. Actually, this option is available in any file open in dialog in OS X via command-shift-G. In addition, TextWrangler will allow you to browse these files with its "open hidden.." option. But, much easier option is selecting "open file by name.." (command-D) and just typing the full path and filename. To save many of these files, you need to enter your root password. Be careful.

Enabling PHP

PHP is installed in Mac OS X by default, but not enabled. To enable it, we must edit the apache 2 configuration file, which is located at /etc/apache2/httpd.conf. Find the line which loads PHP 5 module, which looks like this :

# LoadModule php5_module libexec/apache2/libphp5.so

The line is currently commented out. All we have to do is remove the comment symbol, #, so the line looks like this:

LoadModule php5_module libexec/apache2/libphp5.so

Save.

Starting Apache

Go to the sharing panel in system preferences and enable " Web Sharing". This will start the apache server.

Another way to do this is to type the following in the terminal application:

sudo apachectl start

You will be prompted to enter your root password. After that, your apache server should now be running. If you need to restart the server from the terminal, you can type this:

sudo apachectl restart

If you find this tedious to type, there is a script that can download to do this later in this post.

Visiting our Web Site

In Web browser, go to url http://localhost/ . localhost is a special name that means " My comuter". If your web server is working , you should see a page titled "Test Page for Apache Installation:. If you go to http://localhost/manual/ , you can read an Apache 2.2 manual, hosted from your own server. But, this don't tell you that PHP is working.  For that, we will write simple php program. Create a new file in textWrangler and type the following:

< ? php phpinfo(); ?>

(Don't copy and paste this. There is no space between < ? . It's because of WordPress software.)

Save this using file name info.php in the /Library/WebServer/Documents/ directory. Now you should be able to visit the PHP page that you have just created by visiting http://localhost/info.php . You should see the PHP logo and a big table of configuration info.

In next post we will discuss following topics :

  1. Data Security
  2. Enabling a personal Website
  3. Virtual Hosting
  4. MYSQL Installation

Friday, October 17, 2008

Post 1 - Introdution to PHP .


Hi Guys ,

WELCOME to PHP!

You all must be thinking that why I chose PHP , since there are many languages on which we can focus. First of all I would like to tell you about how PHP evolved and then why we are concentratin on it.

PHP is server-side scripting language usually written in HTML context. PHP stands for PHP: Hypertext Preprocessor .

The first version of PHP was created by RASMUS LERDORF in 1994 as a set of Web publishing macros. These were released as the Personal Home Page Tools and later rewritten and extended to include a package called the Form Interpreter (PHP/FI). (PHP/FI) was an attractive proportion from a usre's perspective,as a result its popularity grew steadily. By 1997, a team of developers was working on the project.

Next release - PHP3 . It was a collaborative of interested developers. In this entirely new parser was created by ZEEV SURASKI and ANDI GUTMANS , also had new syntax and new features.Zend is a scripting engine that sits below the PHP-specific modules. It is optimizedto significantly improve performance.

In mid 90's , the approach in Web publishing changed. Ealier it was based on hard coding HTML and large sites to manage content. PHP had huge advantage over other programming languages in managing large sites and data retrival from databases.  Latest PHP5 is available in the market having lot new features and functionalities.

NOW, WHY PHP ..?

1. OPEN SOURCE (Best Thing in this whole world [:)] ) 

Q: What is Open Source..?

Most of us think that "open source" simply means free. Yes, you are right .PHP is free and you don't have to pay amount for its personal /commercial use. You can download it from official PHP site at http://www.php.net/

2. PERFORMANCE : PHP4 compares wel with ASP benchmark tests because of powerful Zend engine.

3. PORABILITY : It can run on many operationg systems and works with many servers and databases.

4. SPEED OF DEVELOPMENT : The growth and popularity of PHP is tremendeous . Also in terms of future prospects PHP has upper hand.

PHP performs exceptionally well with world's best OPEN SOURCE products.

1. LINUX

2. APACHE SERVER

3.MYSQL

It means you just need a system and few open source product to start your web development.

So, get ready with all these products and we will install PHP in next post.

Thanks for your patience.