Web Server Ubuntu With Nginx, PHP-FPM and PostgreSQL
-
Install Nginx
- update & upgrade your ubuntu
sudo apt update -y && sudo apt upgrade -y- install nginx with command
sudo apt install nginx -y -
Configure Ubuntu Firewall
- check ubuntu firewall status
sudo ufw status - if inactive, to enable use this command :
sudo ufw enable - allow nginx full for http & https
sudo ufw allow 'Nginx Full' - check your nginx instalation via ip server, if you see this image below your nginx has ben successfully installed

- check ubuntu firewall status
-
Install php fpm with extension
- Ubuntu 24.04 should come with software-properties-common installed, but in case it isn’t, you can install it using the following command. This package provides necessary scripts for adding repositories to your system.
sudo apt install software-properties-common -y - add php repository
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php -ysudo apt update - install php fpm and composer with extension required for laravel with PostgreSQL as database
sudo apt install php7.4 php7.4-{fpm,common,bcmath,curl,mbstring,xml,zip,pgsql} composer -y - verify php instalation
php -v
- Ubuntu 24.04 should come with software-properties-common installed, but in case it isn’t, you can install it using the following command. This package provides necessary scripts for adding repositories to your system.
-
Install PostgreSQL
-
add repository:
sudo apt install -y postgresql-commonsudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -
install postgres:
sudo apt install postgresql-14 -y -
set password postgres
sudo su - postgrespsql\passwordenter your new password -
(optional, you can skip to next step) allow remote access and change port. before making any changes to the configuration, it is a good idea to make a backup first.
sudo cp /etc/postgresql/12/main/postgresql.conf /etc/postgresql/12/main/postgresql.conf.default
sudo nano /etc/postgresql/14/main/postgresql.confUncomment
listen_addressand change it to " * “, change postgresql port to anything you want. Then configure pg_hba.conf. Back up before making any changes.sudo cp /etc/postgresql/12/main/pg_hba.conf /etc/postgresql/12/main/pg_hba.conf.default
sudo nano /etc/postgresql/12/main/pg_hba.confjump into last page and add this line :
host all all 0.0.0.0/0 md5reload postgresql
sudo systemctl restart [email protected] -
allow postgresql remote port in ufw
sudo ufw allow your_postgres_port/tcp -
test your connection

-
-
Configure server block in Nginx
-
remove default configuration in /etc/nginx/site-enable using this command :
sudo rm /etc/nginx/sites-enabled/default -
create new server block from default:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/yourdomain.com -
then open it using nano :
sudo nano /etc/nginx/sites-available/yourdomain.com -
add this configuration
- root -> change to your root project folder, for example /var/www/yourdomain.com - index -> add index.php -> for supporting .php file - server_name yourdomain.com - remove comment for php script fastcgi server like this : location ~ \.php$ { include snippets/fastcgi-php.conf; # With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php8.3-fpm.sock; # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } -
enable server block :
sudo ln -s /etc/nginx/sites-available/youdomain.com /etc/nginx/sites-enabled/ -
test your nginx config
sudo nginx -tIf the nginx block server configuration is correct, the above command will display:
nginx: configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful -
reload nginx service
sudo systemctl reload nginx -
Create an index file in /var/www/yourdomain.com. If the directory doesn’t exist, create it.
sudo touch /var/www/yourdomain.com/index.php -
add to test your serverblock is work
<?php phpinfo();
-