Setting Up Your Own Home LAMP Machine for PHP Development -Setting Apache

| Comments

Hello there!!
Well am back here after a long time. Writing blog posts is really boring when you have something as awesome as writing code, and writing code for web applications is what is red hot in the skies right now. So here’s this post to help us setting up our own LAMP (Linux - which all of us already have, Apache - the HTTP server, MySql - the Database Management System, and PHP - the sexy thing we’ll write our web apps in). 
Php Development in Linux
Yea I know it is dead easy to just use the LAMP (like people use WAMP on Windows), but what’s the meaning of using something as awesome as Linux if we are gonna use those tools(no offenses here). I hate using LAMP anyways.
So what we gonna do is installing Apache, Mysql and PHP on our machine and setting it up for awesome developing PHP apps.

Installing Apache

Well, it can’t be done easier on our Ubuntu Machine.
sudo apt-get install apache2
Installing apache is easy enough, but I don’t think we gonna use our machine for web development all the time, are we? By default apache puts itself into the startup applications list, i.e. it starts up everytime we boot up. That’s a kind of overkill for our development environment. But it is easy to remove it from startup.

Removing Apache from Startup

Just launch the terminal console (alt+ctrl+T on Gnome) and execute the following command
sudo update-rc.d apache2 disable
Now Apache will not start on the startup. We’ll need to start it manually everytime we gonna use it. Just use following command to start apache http server: 
sudo /etc/init.d/apache2 start OR 
sudo service apache2 start

Changing www (default directory) in Apache

By default Http server runs and show those files in http:/localhost which are located in the /var/www directory. But many times we need to change that directory to something else like ~/www or anything so as to run the server with that site as root. 
To achieve this in apache, we need to follow these steps:

  • Go to /etc/apache2/sites-available directory
  • Copy the file named default into the same (/etc/apache2/sites-available) directory and rename it to whatever (say ‘www’ this time) OR use this command via terminal
    cp /etc/apache2/sites-available/default /etc/apache2/sites-available/www

  • Open up the ‘www’ file we created in text editor OR use this command via terminal
    kate (or gedit) /etc/apache2/sites-available/www
  • Find and replace “/var/www” with the path we wanna use as our server root, say “/home/dante/www”
And that’s all for the tough part. Now we have created a different site which we can enable or disable via command line anytime. This give us the power to use multiple server roots and switch between them easily.
To enable our new site (i.e. to change the server root dir), enter following command in terminal
sudo a2ensite www
But before that we need to disable already enabled default site, to disable it run
sudo a2dissite default
Simple enough, huh!
Well I can’t agree more that this is quite a hassle to just changing server root dirs. But here’s an alternative.
Alternatively, we can create a virtualhost.
For this, we’ll edit the default  file in /etc/apache2sites-available/ and also the /etc/hosts file. Then we’ll give different projects different URIs (that will be accessible only to your computer) and use those URIs instead of 127.0.0.1 or something. For example, for a project called ‘php-project’, we’ll assign phpproject.dev (can be anything) and enter http://phpproject.dev in browser to see our project.
Here’s how we do it.

CREATE VIRTUAL HOST:

  • Change directory to /etc/apache2/sites-available
    cd /etc/apache2/sites-available
  • Open ‘default’ in editor
    sudo gedit 
    /etc/apache2/sites-available/default
  • Enter following at the end of file 
      DocumentRoot “/home/[username]/phpProject/”
      ServerName phpproject.dev     


Here, DocumentRoot is the location of your project ([username] is your username, like ‘channi’), and phpproject.dev is the name you want to access the project with (its local browser address)

EDIT /etc/hosts
  • Open /etc/hosts in editor
    sudo gedit /etc/hosts
  • Enter following line at the end
    0.0.0.0  phpproject.dev

    (phpproject.dev can be anything like iamawesome.com or whatever, but keep it same as ServerName in /etc/apache2/sites-available/defautl file)
And that’s it. You can now simply open up your browser and go to phpproject.dev, and your project is there for you.


Wish there was an easy way. If you are thinking the same, then congrats actually, php5.4 comes with an inbuilt server script which can solve many of our problems and provide a nice and easy way to debug php apps. We’ll talk about it shortly.  

Comments