How-To: Add a LAMP Server to Your Linux Desktop for Development

phpinfo()

Lately I’ve been in the mood to do some PHP/MySQL development for my website. In the past, like most newbie or part-time developers, I would work on some code on my desktop and then uploaded the changes to the remote server and hit refresh — not very efficient. This time around I wanted to see how easy or hard it would be to get a production Linux/Apache/MySQL/PHP (LAMP) server working on my Debian-based Ubuntu desktop (currently running Feisty Fawn 7.04) so I can do all the development and testing locally without disturbing my LIVE remote site. Below are the steps I took to reach my objective. Notice: The instructions require basic to intermediate knowledge of Linux, how to use a Terminal, sudo, apt-get, and understanding of PHP and MySQL interaction.

LAMP INSTALL
Install a complete LAMP stack using Taskel via the Terminal — this sets off the same processes as if you were installing the Server version of Ubuntu on your system. Note: Doing it this way also installs the latest release of ALL the software. So if you want PHP4 instead of PHP5, then install each component manually (see resource list in the footer for instructions).

$ sudo tasksel install lamp-server

MYSQL SETUP
Next, set your MySQL root password

mysql -u root

mysql> SET PASSWORD FOR ‘root’@'localhost’ = PASSWORD(’enteryourpasswordhere’);

If successful you’ll see: Query OK, O rows affected (0.00 sec)

Exit MySQL prompt

mysql> \q

To create a database and an associated user, do the following:

Sign in as root — after hitting enter, type in your password

mysql -u root -p

Create database

mysql> CREATE DATABASE database1;

Create and add user to database1

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON database1.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword';

VIRTUAL HOST
Now you want to create a virtual host (or vitural standalone site directory). By default Apache sets you up with a site called “default” that points to /var/www/, however, we want to create a new site that we will more conveniently manage from a “public_html” folder in our home directory.

Next, copy the config file for “default” and create a new config file called “mysite”

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

And then edit mysite’s config file and change the “DocumentRoot” and “Directory” directive from /var/www/ to /home/yourusername/public_html

$ sudo gedit /etc/apache2/sites-available/mysite

Next you’ll need to disable “default” and enable “mysite” in Apache2

$ sudo a2dissite default && sudo a2ensite mysite

Now restart Apache2 for the changes to take affect

$ sudo /etc/init.d/apache2 restart

Ok, now lets create the public_html directory that we pointed Apache to via the Terminal (don’t use sudo) OR you can create it using your File Browser.

$ mkdir /home/yourusername/public_html

Next, open a text editor (gedit is good) and type in <?php phpinfo(); ?> and then save the file as index.php in public_html.

Now, type in http://localhost in your web browser and you should see a PHP config/environment table (cool). Welcome to your new site!

OPTIONAL - INSTALL PHPMYADMIN
For easy browser-based MySQL database managment install phpMyAdmin

$ sudo apt-get install phpmyadmin

Next, we need to add a link to our mysite config file so phpMyAdmin can be accessed on our new site at http://localhost/phpmyadmin. To do this:

$ sudo gedit /etc/apache2/sites-available/mysite

And then add the following line to the bottom of the page before the </Virtualhost> tag:

Alias /phpmyadmin /var/www/phpmyadmin

* Tip: Your phpMyAdmin username and password is root and the password you set for MySQL OR the details of an associated MySQL user you’ve previously created.

And lastly restart Apache2

$ sudo /etc/init.d/apache2 restart

Your done!!!

Resources:
Ubuntu Community Docs - ApacheMySQLPHP
Zaphu - Ubuntu LAMP Server Guide - Configure Apache, mySQL, and cgi-bin
Apache HTTP Server Version 2.2 Documentation
5.8.2. Adding New User Accounts to MySQL
phpMyAdmin Documentation

Popularity: 42% [?]

Tags: , , , , , ,


Comments

3 Responses to “How-To: Add a LAMP Server to Your Linux Desktop for Development”

  1. How-To: Install Django on Your Linux Server or Development Desktop : Alexander Grundner on September 11th, 2007 11:49 pm

    […] Related: How-To: Add a LAMP Server to Your Linux Desktop for Development […]

  2. Stasigr on October 29th, 2007 2:05 am

    Hello, very nice site, keep up good job!
    Admin good, very good.

  3. Restructure! on July 12th, 2008 10:06 pm

    Note:

    At SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(’enteryourpasswordhere’);, copying and pasting the line from the browser will generate an error, because the apostrophes are not straight apostrophes. You would have to delete the left and right apostrophes and replace them with straight apostrophes.

Leave a Reply