How to install Apache, PhP, Postgresql (LAPP) on Ubuntu 16.04

Valentin
Valentin
Nov 2, 2017 · 3 min read

In one way or another, you will need to install a web server locally on your machine.
I always hate that part because I never use the same OS or the same version of it. And so the packages, the way to install and configure it, are never the same.
I also never remember the things that I should do to get a nice workspace.

So in this guide, I will share with you 10 steps for installing a nice web server locally on Ubuntu 16.04.

It also should works on different version or distribution but I did not test them all. Just comment this article if you tried it on an other version than mine.

I also disable the autostart of apache and postgresql to avoid using resources for nothing.

1 : Update your machine

$ sudo apt update && sudo apt upgrade

2 : Install apache2

$ sudo apt install apache2 apache2-utils

3 : Install PhP

$ sudo apt install php php-pgsql libapache2-mod-php

4 : Install Postgresql

I prefer Postgresql, but I’m pretty sure that you can replace that part by MySQL or your favorite database easily.

$ sudo apt install postgresql libpq5 postgresql-9.5 postgresql-client-9.5 postgresql-client-common postgresql-contrib

5 : (Optional) Install PhpPgAdmin

$ sudo apt install phppgadmin

6 : (Optional) Install PgAdmin3

$ sudo apt install pgadmin3

7 : Initialize Postgresql

$ sudo -i -u postgres
$ psql

8 : (If you installed PhpPgAdmin) Configure PhpPgAdmin

$ sudo vim /usr/share/phppgadmin/conf/config.inc.php

9 : Configure your VirtualHost

Just start by creating a directory where you will put all your local websites

$ mkdir ~/web

Then, we create a new conf file for our VirtualHost

$ sudo vim /etc/apache2/sites-available/webserver.dev.conf

Add to this file the following block. Of course, adapt the path, the email address and the server name url to what please you.

<VirtualHost *:80>
ServerAdmin YOUR_EMAIL_ADDRESS
ServerName webserver.dev
DocumentRoot /home/user/web
<Directory /home/user/web>
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.html index.php
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

Once you’ve done this, run these two commands. It gonna prepare your conf file and reload apache2 in order to consider this new configuration.

$ sudo a2ensite webserver.dev.conf
$ sudo service apache2 reload

Finally, open your hosts file.

$ sudo vim /etc/hosts

And simply, add this line.

127.0.0.1    webserver.dev

10 : Disabling autostart

I don’t want my webserver start each time I boot my computer. So I deciding to disable the autostart and create a little script that gonna start it or not.

First, disable apache2 and postgresql on start.

$ sudo update-rc.d -f  apache2 remove
$ sudo update-rc.d -f postgresql remove
$ sudo systemctl disable postgresql

I want something easy to use to start my webserver. So create a new script file

sudo vim ~/webserver-launcher.sh

Add the following block to it. It simply runs the commands one after the other that start, stop or restart this services.

#!/bin/bash

if [ "$1" != "" ]; then
cmd="sudo service apache2 $1; sudo service postgresql $1"
eval $cmd
else
echo "No option was provided"
fi

Add the permission to execute this script and open your .bashrc.

$ sudo chmod u+x ~/webserver-launcher.sh
$ vim ~/.bashrc

Now, create an alias with the link to your script

alias webserv='~/webserver-launcher.sh '

Source it.

$ source ~/.bashrc

Et voilà !
You can now stop, start or restart easily your webserver.

$ webserv stop | start | restart

Open your favorite browser and visit :

http://webserver.dev/

Thanks for reading !

Valentin

Written by

Valentin

Developper interrested in too many things. Tech Jungler https://dlbk.fr