Quickly setup a localhost environment in Ubuntu

Although, I use Ubuntu, this should apply to a large number of linux distros. Every developer uses a local environment to develop locally before testing it live. I kinda avoid that root by mounting my FTP webspace in Ubuntu and directly working on live files (there is no need of download and upload as you might think), but I wanted to do some CPU intensive job for a client for which I needed to setup a localhost environment, so I thought I would share this with everyone on my blog.

Install LAMP stack

Fire up a terminal (Applications > Accessories > Terminal) and enter this command:

sudo apt-get install lamp-server^

lamp-server

Enter y for yes when it asks to continue after estimation of how much data will be downloaded and how much will be used on disk.

It will install quickly and can take a few minutes depending on your internet speed, and then it will ask you to setup the password for MySQL user root. It is usually left blank on localhost machines as they are used only for development purposes but last time I left it blank, I had to deal with the issues that it didn’t change the password to blank but has something else and I had to turn off the password prompt for root access. Avoid doing all that by just selecting “root” or anything you want as the password of the root user of MySQL.

mysql-root-password

Confirm it once (Type the password and press Tab key).

mysql-root-password-confirm

and you are done.

Test Apache Webserver

Just open http://localhost/ in your browser and it will show up a message “It works!” which means Apache is working fine.

Test PHP

Create a file named phpinfo.php in /var/www/ to check if PHP is working fine.

sudo nano /var/www/phpinfo.php

Enter the content as <?php phpinfo(); ?> and save the file by hitting Ctrl + X and then y (for yes) and return key (enter).

Restart Apache webserver by the following command – sudo /etc/init.d/apache2 restart

Now open http://localhost/phpinfo.php and it will show up a page with lots of php related information, if you can see it, PHP is working fine.

Install phpMyAdmin

Enter this command in terminal – sudo apt-get install libapache2-mod-auth-mysql phpmyadmin

phpmyadmin-install

Select Apache as the web server by pressing Space and then Tab key and press enter key.

select-apache

Press Yes to configure database for phpmyadmin.

configure-phpmyadmin

Provide password which we set as “root” earlier.

phpmyadmin1

Again, enter “root“.

phpmyadmin2

Confirm it, and then you are done.

phpmyadmin3

Test phpMyAdmin

Now open http://localhost/phpmyadmin/ in your brower to access phpmyadmin and you can login with username and password both as root.

Pretty quick & easy!

Everything is done, you have your web root at /var/www/ where you will need super user permissions to write files, I will do a follow up post on how you can make this easier by keeping your files in your Home directory instead and a bit of extra which will make it totally complete.

Subscribe so that you don’t miss updates! RSS/Email options are in the sidebar.


Comments

4 responses to “Quickly setup a localhost environment in Ubuntu”

  1. Mashkel Avatar
    Mashkel

    Brilliant! thanks for sharing!

  2. Dennis Avatar
    Dennis

    Perfect info. Install went great.

    Thanks a lot!

  3. You may do all this from a single command line:
    sudo apt-get install phpmyadmin

    It will install everything needed (a full LAMP stack)

    Best

    1. Incorrect! That command will only install phpmyadmin, only if all dependencies are met i.e. any required libraries are available.