Setting up Nginx, PHP, PostgreSQL (Part-1)

In this tutorial I will setup Nginx server along with PHP and PostgreSQL. After this tutorial reader will know –

  • How to setup an HTTP server(here Nginx).
  • How to setup Nginx for developing multiple websites locally.
  • Setup PHP and configure Nginx sites to support it.
  • Setup PostgreSQL for database server.
  • Initial setup for PostgreSQL.

I will be using Archlinux on my PC. If reader is running any other Linux it will be ok. If reader is using Windows OS, you can use Virtual Box, VMware etc to install a Linux distro on your Windows OS. And of course you should have internet to install packages.

NOTE : For other Linux distros(i.e Debian, Fedora, Opensuse, Ubuntu etc), use their package management tools like apt-get, apt, yum etc or search google for appropriate tools/commands if you don’t know it.

Setting up Nginx

Open up terminal and type –

		sudo pacman -S nginx

This will install Nginx binaries and default configurations under /usr and systemd service as well. You can also install it by compiling Nginx from source. Just go to Nginx download site and download the latest source tarball. To compile from source you will need gcc along with some other packages, like –

		binutils openssl pcre zlib geoip gd libxpm libxml2 libxslt

If you install via package management tool, the following commands are available to be used –

  • Start Nginx : sudo systemctl start nginx
  • Stop Nginx : sudo systemctl stop nginx
  • Restart Nginx : sudo systemctl reload nginx
  • Autostart at boot-up : sudo systemctl enable nginx

If you’ve installed from source, save the following text as /usr/lib/systemd/system/nginx.service

Description=A high performance web server and a reverse proxy server


ExecStart=/usr/bin/nginx -g 'pid /run/; error_log stderr;'
ExecReload=/usr/bin/kill -HUP $MAINPID


NOTE : Edit the paths according to your installation.

Lets just type sudo systemctl enable nginx for autostarting nginx. After reboot Nginx will start with the default configuration from /etc/nginx/nginx.conf. Before restarting I will configure Nginx for a development site say It will be acitvated as an alias of localhost, a separate directory for the test site will be given as well. So after this setup and rebooting reader can open a browser and go to to see if the site is up and running.

NOTE : For the following steps I will use my username as shimon. Replace shimon with your username.

REMINDER : Do not use this kind of setup for production purpose ! It’s only for development setup.

Let’s create some folders for configuration files. In terminal, type –

export www=/home/shimon/www
mkdir -pv $www/{sites{,-available,-enabled},sites/}

Let’s create a configuration file for Save the following text as /home/shimon/www/sites-available/

server {
	root /home/shimon/www/sites/;

	location / {
	    index index.htm index.html;

	error_page 500 502 503 504 /50x.html;

	location = /50x.html {
	    root html;

Now let’s create a homepage for To do that save the following text as /home/shimon/www/sites/

<!DOCTYPE html>
	<title>Nginx Test Site</title>
	<h1>Welcome ...</h1>
	<article>Hello there ! Welcome to nginx test website. If you are seeing this page that means your nginx setup is ready to roll.</article>

Open up terminal and run –

ln -s /home/shimon/www/sites-available/ /home/shimon/www/sites-enabled/

REMINDER : Don’t forget to replace shimon with your username.

Now as the site, with it’s nginx configuration is ready, we will configure nginx to serve the test website. To do this, first backup the original configuration, run the following command to do this –

sudo mv /etc/nginx/nginx.conf{,.bak}

Now open a new /etc/nginx/nginx.conf as root and copy-paste the following text there.

user shimon;
worker_processes 2;
events {
	worker_connections 1024;
http {
	include mime.types;
	default_type application/octet-stream;
	sendfile on;
	keepalive_timeout 65;
	gzip on;
	include /home/shimon/www/sites-enabled/*;

REMINDER : Don’t forget to replace shimon with your username.

This is the last step(phew !) – open /etc/hosts as root and add the follwing at the end of the file

That’s it for now. Restart your computer, open a browser and go to

Screenshot from 2016-02-20 00-12-15

In part -2 I will setup PHP and use it in this test site.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s