Web Server Ubuntu With Nginx, PHP-FPM and PostgreSQL

  1. Install Nginx

    1. update & upgrade your ubuntu

    sudo apt update -y && sudo apt upgrade -y

    1. install nginx with command

    sudo apt install nginx -y

  2. Configure Ubuntu Firewall

    1. check ubuntu firewall status

      sudo ufw status

    2. if inactive, to enable use this command :

      sudo ufw enable

    3. allow nginx full for http & https

      sudo ufw allow 'Nginx Full'

    4. check your nginx instalation via ip server, if you see this image below your nginx has ben successfully installed Nginx isntalation status
  3. Install php fpm with extension

    1. 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

    2. add php repository

      LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php -y sudo apt update

    3. 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

    4. verify php instalation

      php -v

  4. Install PostgreSQL

    1. add repository:

      sudo apt install -y postgresql-common sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

    2. install postgres:

      sudo apt install postgresql-14 -y

    3. set password postgres

      sudo su - postgres psql \password enter your new password

    4. (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.conf

      Uncomment listen_address and 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.conf

      jump into last page and add this line :

      host all all 0.0.0.0/0 md5

      reload postgresql

      sudo systemctl restart [email protected]

    5. allow postgresql remote port in ufw

      sudo ufw allow your_postgres_port/tcp

    6. test your connection PostgreSQL remote connection test

  5. Configure server block in Nginx

    1. remove default configuration in /etc/nginx/site-enable using this command :

      sudo rm /etc/nginx/sites-enabled/default

    2. create new server block from default:

      sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/yourdomain.com

    3. then open it using nano :

      sudo nano /etc/nginx/sites-available/yourdomain.com

    4. 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;
          }
      
    5. enable server block :

      sudo ln -s /etc/nginx/sites-available/youdomain.com /etc/nginx/sites-enabled/

    6. test your nginx config

      sudo nginx -t

      If 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
      
    7. reload nginx service

      sudo systemctl reload nginx

    8. 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

    9. add to test your serverblock is work

          <?php
      
              phpinfo();
      

comments

comments powered by Disqus