Skip to main content

Setting Up a Web Server with Linux, Apache, MySQL, PHP (LAMP) on Ubuntu 14.04

· 11 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction

A "LAMP" stack is like a set of tools for building websites and web apps. It's made up of different free software that work together on a computer server. Let's look at what each part does:
  1. Operating System: This is like the main software that runs everything on the computer. It's the foundation of the stack.
  2. Web Server (Apache): Apache acts like a traffic controller for the web. It takes requests from people's browsers and sends back the right web pages.
  3. Database (MySQL): This is where all the website's data is stored, like usernames, passwords, and other information. It's like a digital filing cabinet.
  4. Server-side Language (PHP): PHP is a language that helps create dynamic web pages. It's what makes things like logins, forms, and shopping carts work smoothly.
Now, let's learn how to set up this LAMP stack on a computer running Ubuntu 14.04. Ubuntu is just another type of operating system and it's perfect for this task.

Note: Installing a LAMP stack on your Droplet can be made super easy! Just include this special script when you're setting up your Droplet. It's like giving your computer a set of instructions to follow right from the start. If you're not sure how to do this, check out the tutorial on Droplet User Data. It'll walk you through the process step by step.

Getting Started
Before you start following this guide, make sure you have a special user account set up on your server. This account shouldn't be the main "root" one, but it should have similar privileges. If you're not sure how to set up this user account, you can check out steps 1-4 in the beginner's guide for setting up your server with Ubuntu 14.04. It'll walk you through the process in easy-to-follow steps.

Let's Get Your Website Running: Installing Apache
Apache is like the engine that powers many websites on the internet. It's super popular and reliable, making it a great choice for hosting your site.To get Apache, we'll use something called Ubuntu's "package manager." It's like an app store for your server!

Here's how we do it: We just tell the package manager what we want, and it goes and gets it for us. Easy, right? If you want to learn more about how it works, you can check out this guide on using the package manager.
sudo apt-get update
sudo apt-get install apache2

When we use a command with "sudo," it means we're doing something important with extra powers. It might ask for your regular password just to make sure you're sure about what you're doing.

Once we're done with that, our web server is good to go!

To make sure everything worked like it should, you can quickly check it by going to your server's public IP address in your web browser. If you're not sure what that is, don't worry, we'll show you how to find it in the next part.
<pre> http://<span class=“highlight”>your_server_IP_address</span> </pre>

You'll be greeted with a special webpage made by Apache on Ubuntu 14.04. It's there to give you some basic info and to help you check if everything is working fine. It might look a bit like this:
small_apache_default
How to See Your Server's Public IP Address: If you're not sure what your server's public IP address is, there are a few simple ways to find it. This is the address you use to connect to your server using SSH.

One way to find it is by using commands on the command line. You can do this by typing a specific command, like this:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

After running the command, you'll see one or two lines with numbers on them. Both lines show the correct addresses, but your computer might only be able to use one of them. So, you can try using each one to see which works.

Another way is to ask a different computer outside of your network to tell you what it sees as your server's IP address. You can do this by reaching out to a specific server and asking it for the information.
curl http://icanhazip.com

No matter how you find your IP address, you can simply type it into your web browser's address bar to reach your server.
Setting Up MySQL Database Management System
Let's set up MySQL now that our web server is running. MySQL is like a storage system for our website. It helps organize and manage databases where our site can keep information.

We'll use a tool called "apt" to get and install MySQL, just like before. This time, we'll also install some extra packages that help different parts of our setup talk to each other:
sudo apt-get install mysql-server php5-mysql

Note: You don't need to run "sudo apt-get update" again before installing MySQL because we already did that when installing Apache. Your computer already has the latest information about available packages.

While installing MySQL, your server will ask you to pick and confirm a password for the MySQL "root" user. This is like an administrator account in MySQL, with special powers. It's similar to the main account for the server itself, but just for MySQL.

After MySQL is installed, we have to do a few more things to make sure it's set up safely.

First, we need to tell MySQL to create the place where it will keep all its information. You can do this by typing:
sudo mysql_install_db

After that, we'll run a simple security script to make sure our database system is safer. To begin this script, just run:
sudo mysql_secure_installation

You'll be prompted to enter the password you set for the MySQL root account. Then, it will ask if you want to change that password. If you're happy with it, just type "n" for "no".

For the other questions, just press "ENTER" to accept the default values. This will remove some sample users and databases, disable remote root logins, and apply these new rules immediately to MySQL.
Now your database system is all set up and we can move forward.
Installing PHP for Dynamic Website Content
Let's install PHP, which is a crucial part of our setup. PHP is responsible for processing code to show dynamic content on our website. It can run scripts, communicate with our MySQL databases to fetch information, and then deliver the processed content to our web server for display.

We'll use the apt system again to install PHP along with some additional helper packages.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

Installing PHP should be smooth without encountering any issues. We'll verify this shortly.

Usually, we'll want to adjust how Apache handles files when someone requests a directory from our server. Right now, if a user asks for a directory, Apache checks if there's a file called "index.html" inside. However, we prefer to use PHP files, so we'll tell Apache to look for an "index.php" file first.

To make this change, follow these steps - Open the "dir.conf" file in a text editor with special permissions. You can do this by typing:
sudo nano /etc/apache2/mods-enabled/dir.conf

Here's what the file will look like:
<IfModule mod_dir.c>
  DirectoryIndex index.html index.cgi index.pl <span class=“highlight”>index.php</span> index.xhtml index.htm
</IfModule>

We want to move the highlighted PHP index file to the first position after the DirectoryIndex specification, like this:
<IfModule mod_dir.c>
  DirectoryIndex <span class=“highlight”>index.php</span> index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Once you've made this change, save and close the file by pressing "CTRL-X". Confirm the save by typing "Y" and then hit "ENTER" to confirm the file save location.

After that, we need to restart the Apache web server to make our changes take effect. You can do this by typing:
sudo service apache2 restart

Enhancing PHP Functionality with Additional Modules
We can make PHP even more powerful by adding extra modules. These modules provide additional features and tools that we can use in our PHP code.

To check out the available options for PHP modules and libraries, you can do this:
apt-cache search php5-

The list shows extra parts you can add if you want. Each one comes with a quick explanation:
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
. . .

To learn more about what each module does, you have two options:
  1. Search online.
  2. Check the detailed description in the package itself. You can do this by typing:
apt-cache show <span class=“highlight”>package_name</span>

This will show a lot of information, but focus on the "Description-en" field, which gives a longer explanation of what the module does.

For instance, to find out about the php5-cli module, you can type:
apt-cache show php5-cli

In addition to many other details, you'll see something like this:
. . .
SHA256: 91cfdbda65df65c9a4a5bd3478d6e7d3e92c53efcddf3436bbe9bbe27eca409d
Description-en: command-line interpreter for the php5 scripting language
This package provides the /usr/bin/php5 command interpreter, useful for
testing PHP scripts from a shell or performing general shell scripting tasks.
.
The following extensions are built in: bcmath bz2 calendar Core ctype date
dba dom ereg exif fileinfo filter ftp gettext hash iconv libxml mbstring
mhash openssl pcntl pcre Phar posix Reflection session shmop SimpleXML soap
sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader
xmlwriter zip zlib.
.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
Description-md5: f8450d3b28653dcf1a4615f3b1d4e347
Homepage: http://www.php.net/
. . .

If, after checking, you decide you want to add a package, you can use the "apt-get install" command just like we did for our other software.

For example, if you've decided you need "php5-cli", you would type:
sudo apt-get install php5-cli

If you want to install multiple modules, you can do that by listing each one, separated by a space, after the "apt-get install" command, like this:
sudo apt-get install <span class=“highlight”>package1</span> <span class=“highlight”>package2</span> <span class=“highlight”>…</span>

Now, your LAMP stack is all set up and ready to go. However, we should still check if PHP is working correctly.
Checking PHP Functionality on Your Web Server
To make sure our system is set up correctly for PHP, let's create a simple PHP script.

We'll name this script "info.php". For Apache to find and display the file properly, it needs to be saved in a specific folder called the "web root".

In Ubuntu 14.04, this folder is located at "/var/www/html/". You can create the file in that location by typing:
sudo nano /var/www/html/info.php

This will open an empty file. Now, we'll add some simple PHP code inside it. Just copy and paste the following text into the file:
<?php
phpinfo();
?>

Once you're done, save and close the file.

Now, let's check if our web server can show content generated by a PHP script. To do this, simply open your web browser and visit the following page. You'll need your server's public IP address again.

The address you want to visit is:
http://<span class=“highlight”>your_server_IP_address</span>/info.php

When you go to this page, you should see something similar to this:
This page shows you server information from PHP's perspective, useful for checking your setup. If everything looks good, your PHP is working as it should.

If everything went well and you see the expected information, it means your PHP is working correctly.
However, you should consider removing this file after the test because it could potentially expose information about your server to unauthorized users. To delete it, you can do the following:
sudo rm /var/www/html/info.php

If you ever need to check that information again, you can always recreate this page.

Conclusion:
Now that you've set up your LAMP stack, you're ready to do a lot of cool stuff! Basically, you've got everything you need to create different types of websites and web software on your server.There are so many possibilities! Just choose what interests you the most and start exploring.