Skip to Content

PHP + Nginx + PostgreSQL

Posted on

As a requirement to host web applications like Tiny Tiny RSS on my home server I install PHP, the lightweight proxy server Nginx, and the PostgreSQL database.

Let’s go!

Setup: Netbook with IP ADDRESS 192.168.1.88 running the latest stable release of Debian.

0. PHP

Install …

$ sudo apt install php php-fpm php-apcu php-curl php-cli php-pgsql php-gd php-mcrypt php-mbstring php-fdomdocument php-intl

Improve security by editing /etc/php/*/fpm/php.ini and modifying pathinfo to 0

cgi.fix_pathinfo=0                                                              

Restart PHP …

$ sudo systemctl restart php7.0-fpm

1. Nginx

Installing the above PHP packages will pull in apache packages that are not required. Remove …

$ sudo apt --purge remove *apache*

Install …

$ sudo apt install nginx-common
$ sudo apt install nginx                                                    
$ sudo systemctl start nginx                                                  

Verify web server is running by opening a browser and navigating to http://192.168.1.88. If you see Welcome to nginx! the server is installed correctly.

2. Host multiple domains

Nginx is capable of serving up multiple web domains or server blocks (virtual hosts):

  • For (username foo) I create /home/foo/www to hold block content in subfolders
  • Create block configs in /etc/nginx/sites-available
  • A block is made active by setting a symbolic link in /etc/nginx/sites-enabled to its block config

Example: Setup a ttrss server block to host an RSS feed reader at http://ttrss.lan.

Create a new root directory to hold the contents of ttrss in the home directory …

$ mkdir -p /home/foo/www/ttrss

Create a sample ttrss/index.html

<html>
<head>
<title>My RSS Reader</title>
</head>
<body bgcolor="red" text="white">
<center><h1>Coming soon -- The future home of the Tiny Tiny RSS Reader!</h1></center>
</body>
</html>

Create a new server block configuration /etc/nginx/sites-available/ttrss

server {
        listen 80;
        listen [::]:80;

        root /home/foo/www/ttrss;
        index index.html;

        access_log /var/log/nginx/ttrss_access.log;
        error_log /var/log/nginx/ttrss_error.log info;

        server_name ttrss.*;

        location / {
            index           index.html;
        }
}

Activate the new server block …

$ cd /etc/nginx/sites-enabled
$ sudo ln -s ../sites-available/ttrss
$ sudo systemctl restart nginx

2.1 Add hostname to HOSTS

On the Linux client device, modify /etc/hosts by adding the ttrss.lan address …

192.168.1.88    ttrss.lan

… then navigate to http://ttrss.lan` to see the new webpage.

3. PostgreSQL

Install …

$ sudo apt install postgresql postgresql-contrib
$ sudo systemctl start postgresql

Launch the PostgreSQL interactive console front-end psql as the postgres user and set a new password …

$ sudo -u postgres psql postgres
postgres=# \password postgres
Enter new password: [newpasswd]
Enter it again: [newpasswd]
postgres=# \quit

Example: Create a new username www-data and database ttrss1

$ sudo -u postgres psql postgres
postgres=# CREATE USER "www-data" WITH PASSWORD 'newpasswd';  
postgres=# CREATE DATABASE ttrss WITH OWNER "www-data";                         
postgres=# GRANT ALL PRIVILEGES ON DATABASE ttrss to "www-data";                
postgres=# \quit

Reload server and check status …

$ sudo systemctl restart postgresql
$ systemctl status postgresql

4. Helpful resources

🐧 Part of the Linux Home Server project.

Happy hacking!

Notes


  1. PostgreSQL maintains its own users and passwords, which are separate from the Linux user accounts. It is not required that your PostgreSQL usernames match the Linux usernames. However, the default setup for PostgreSQL is to give access to system users (e.g. postgres, www-data). See Practical PostgreSQL database. [return]