Installing LAMP stack on Ubuntu
Table of Contents
LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install LAMP stack (Apache2, PHP and MySQL) on an Ubuntu 16.04 server.
Why I Still Reference This
When I was doing freelance PHP work, spinning up a fresh Ubuntu VPS for a client was something I did every few weeks. Each time I would end up re-Googling the same commands, because the package names and the correct sequence of steps are just different enough across PHP versions that I could never fully commit them to memory.
I wrote this post so I had one reference to point myself — and junior developers on my team — to. The biggest pain point I ran into repeatedly was PHP version conflicts: installing php5 when a project needed 7.x would break the app in subtle ways, and the PPA-based PHP 7 install (via ondrej/php) is not something you want to figure out from scratch while a client is watching their site stay offline.
Two things I learned the hard way: first, always run sudo apt-get update before installing anything — skipping it often causes package-not-found errors that send you down a rabbit hole. Second, after installing mod_php, Apache does not always restart automatically on Ubuntu; the explicit service apache2 restart step is genuinely necessary, not just a formality. I have seen dozens of “it’s not working” messages that turned out to be nothing more than a missing restart.
Apache
To install Apache you must install the Metapackage apache2. This can be done by searching for and installing in the Software Centre, or by running the following command.
sudo apt-get install apache2
MySQL
To install MySQL you must install the Metapackage mysql-server. This can be done by searching for and installing in the Software Centre, or by running the following command.
sudo apt-get install mysql-server
PHP
To install PHP you must install the Metapackages php5 and libapache2-mod-php5. This can be done by searching for and installing in the Software Centre, or by running the following command. Install PHP 5:
sudo apt-get update
sudo apt-get install php5 libapache2-mod-php5
Or Install PHP 7:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.1
# Optional modules
sudo apt-get -y install php7.1-mcrypt php7.1-curl php7.1-cli php7.1-mysql php7.1-gd libapache2-mod-php7.1
# check installed version
php -v
Restart Server
Your server should restart Apache automatically after the installation of both MySQL and PHP. If it doesn’t, execute this command.
sudo service apache2 restart
Check Apache
Open a web browser and navigate to http://localhost. You should see a message saying It works!
Check PHP
You can check your PHP installation by executing any PHP file from within /var/www/. Alternatively you can execute the following command, which will make PHP run the code without the need for creating a file.
php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'
Or:
php -v
Congratulations, you have just Installed a LAMP Server!
I hope that some of this is useful to a few of you. There is always an room for improvements. Leave a note in the comments if you have any cool tricks for the LAMP installation. I’m always on the lookout for fun hacks.
I have wrote another article on How to install PhpMyAdmin on centOS. Checkout related articles section to find more relevant content.