Notes On Setting LAMP Environment For Web Development On Ubuntu

Most of the time I come across situations where I need to install or configure LAMP environment for web application development. I have done this many time but there are always some things that I need to struggle with.

There are plenty of tutorials and articles already written on these, but I needed some kind of notes that will suit my development style, and that I can update as I learn new things. So here I am documenting common things related to web application development environment setup. Expect them to be updated over time.

Installing Ubuntu OS

Ubuntu is my favorite operating system and it does my all jobs. Long ago I had written an article on Why should you use Ubuntu. I would like to share some of the things that I learned over time.

Size of Your Swap Partition

While installing Ubuntu, keep a simple thumb rule for the swap partition size. Ubuntu community documentation suggests that size of swap partition should be of the same size as your RAM.

Create /home directory in separate partition

When installing Ubuntu, create a separate large partition as your /home directory. This partition will contain home folders of all users. The benefit of creating a separate partition for /home directory is that your data will be separate from your Ubuntu installation.

For unfortunate reasons, if your Operating System gets corrupt, your data and settings will still be available to you even after formatting and installing your OS partition. Only remember to select the same partition as your /home when re-installing Ubuntu.

Size of OS installation partition

There is no fixed rule and requirements, but I found that 20 GB is good enough and my preferred size is 30 GB. I have never run out of space with this.

Installing LAMP

Quickest way to install LAMP on Ubuntu

Over the last few years, I have tried different ways of installing LAMP applications. Many times, I had installed each application separately. During my trial and error learning period, I had to struggle to do many configurations and settings. But I can now set up my environment in very less time.

Essential Software

sudo apt-get install tasksel 
sudo tasksel install lamp-server 
sudo apt-get install phpmyadmin

If you want to install them manually one by one, you can follow instructions on Ubuntu community documentation for Installing Apache, MySQL, PHP.

Optional Software

Sublime text is my favorite text editor. Ubuntu comes with Firefox pre-installed as the default browser. However, I prefer Google Chrome. Also when you are working in a team, we need to access each other’s machines. SSH is the simplest way we can access other machines and OpenSSH is an application you will need.

Sublime Text 3

[http://www.webupd8.org/2013/07/sublime-text-3-ubuntu-ppa-now-available.html]
(http://www.webupd8.org/2013/07/sublime-text-3-ubuntu-ppa-now-available.html)

sudo add-apt-repository ppa:webupd8team/sublime-text-3 
sudo apt-get update sudo apt-get install sublime-text-installer 
Google Chrome

http://www.ubuntuupdates.org/ppa/google_chrome

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'

sudo apt-get update 

sudo apt-get install google-chrome-stable </pre>
Open SSH

https://www.openssh.com

sudo apt-get install openssh-server

Create Separate Development Environment for each user

As time goes on, many projects increase in public_html folder. Also, there might be multiple users using the same machine for development. It’s a good idea to keep things separate.

# Create public_html folder in your home directory
cd mkdir public_html 

Create symlink in /var/www folder

cd /var/www 
sudo ln -s /home/rakesh/public_html rakesh</pre>

Now each project folder (project-folder) kept in public_html folder will be available on the server as http://localhost/rakesh/project-folder

Quick Notes on User Groups and Permissions

In my opinion, this section deserves some attention. Most of the live servers where actual projects are uploaded have Apache server installed. Apache server has it's own user, www-data executing all PHP scripts. It is assumed that www-data user owns your PHP files or have permissions to execute, read or write.

On the live server, you may not need to do any changes. At the most changing ownership of project folder to www-data would be sufficient. But if you are experiencing problems for only some part of your application, such as uploading files, you will need to check permissions correctly.

On our local server, we need to take care that permissions are right. The solution is very simple. First, you need to add yourself to group ‘www-data’ and then change the permission of your public_html or project folder.

# Add your self to group 
www-data sudo adduser your-username www-data 

# Change permission of your project folder or all projects 
sudo chown -R your-username:www-data /path/to/project/folder

Above solution was partially inspired from this discussion on Ask Ubuntu site to suit above development environment. Here the main difference is we are giving ownership to ‘your-username’ instead of user www-data. It allows you to create new files without any restrictions and group www-data allows you to use those files for other server tasks.

Configuring Apache

Enable Rewrite Module

Almost all PHP applications I have used requires Rewrite module to be enabled. This is just a one command task.

Enable Apache Rewrite Module

sudo a2enmod rewrite

You can remember above syntax like “Apache version 2, please ENable MODule named REWRITE“.

Allow .htaccess files to override settings

Most of the project uses .htaccess files to override default server settings. By default .htaccess files cannot override default Apache server settings. To allow override, we need to edit the default configuration file.

# Go to Apache's sites-available folder 
cd /etc/apache2/sites-available/ 

# Create a backup of original configuration file, will create default.backup file 
sudo cp default{,.backup} 

# Edit default file and replace all `AllowOverride None` with `AllowOverride All` 

# your-editor-name (subl|vi|gedit) 
sudo your-editor-name default 

# Restart Apache server 
sudo service apache2 restart

Conclusion

If you like it, please bookmark and share. If you think I should add something to the above post, please changes in comments.

Rakesh Tembhurne

Rakesh Tembhurne

Rakesh is a father, software engineer, author and mentor having more than 8+ years of experience in web apps development.

Read More