Digital Ocean Droplet-Google Domains | Laravel(php 8.1) Nginx Postgres deployment
3 min readFeb 21, 2024
- Login to your digitalocean account : https://cloud.digitalocean.com/login
- Create Project
- Create Droplet
- Open your terminal
- Connect your droplet (Ubuntu 22.04 (LTS) x64) with ssh root@DROPLET_IP
- Run these commands
- sudo apt update
- sudo apt install php-mbstring php-xml php-bcmath php-curl php-pgsql php-mysql
- sudo apt install nginx
- sudo apt install php8.1-fpm
- sudo apt install composer
- sudo apt install -y postgresql postgresql-contrib
- sudo systemctl start postgresql
- sudo systemctl enable postgresql
- sudo -u postgres psql
- Make php fpm owner www-data : nano /etc/php/8.1/fpm/pool.d/www.conf
[default]
security.limit_extensions = .php
listen = /run/php/yourserverhostname.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
user = www-data
group = www-data
pm = dynamic
pm.max_children = 75
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500
- Show databases with this command : \l
- Create database with this command : CREATE DATABASE your_database_name;
- Set default database when connecting: sudo -u postgres psql -d your_db_name;
- Create datatable : CREATE TABLE employees ( employee_id SERIAL PRIMARY KEY, employee_name VARCHAR(100), salary DECIMAL(10,2) );
- Insert data : INSERT INTO employees (employee_name, salary) VALUES (‘John Doe’, 50000.00), (‘Jane Smith’, 60000.50), (‘Bob Johnson’, 75000.75);
- sudo systemctl start php8.1-fpm
- sudo systemctl status php8.1-fpm
- sudo ufw app list
- sudo ufw allow ‘Nginx HTTP’
- sudo ufw enable
- sudo ufw status
- cd /var/www
- adduser YOUR_NAME
- usermod -aG sudo YOUR_NAME
- git clone https://github.com/bartukocakara/YOUR_REPOSITORY_NAME.git
- cd YOUR_REPOSITORY_NAME
- cp .env.example .env
- php artisan key:generate
- sudo nano /etc/nginx/sites-available/YOUR_REPOSITORY_NAME
- sudo ln -s /etc/nginx/sites-available/YOUR_REPOSITORY_NAME /etc/nginx/sites-enabled/
- sudo nginx -t
- sudo systemctl reload nginx
- sudo unlink /etc/nginx/sites-enabled/default
- sudo systemctl reload nginx
- systemctl status nginx
- systemctl enable nginx
- systemctl restart nginx
- tail -f /var/log/nginx/error.log
- sudo nano /etc/nginx/sites-enabled/YOUR_REPOSITORY_NAME
server {
# Example PHP Nginx FPM config file
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/YOUR_REPOSITORY_NAME;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# Nginx php-fpm sock config:
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# Nginx php-cgi config :
# Nginx PHP fastcgi_pass 127.0.0.1:9000;
}
# deny access to Apache .htaccess on Nginx with PHP,
# if Apache and Nginx document roots concur
location ~ /\.ht {
deny all;
}
} # End of PHP FPM Nginx config example
- Create custom name servers from google domains
In Custom Name Servers add 3 entries:
— ns1.digitalocean.com
— ns2.digitalocean.com
— ns3.digitalocean.com
- Create domain from digital ocean projects
1 -)For the first record, use “@” under the hostname:
— Point the hostname “@” to the IP address of your droplet project.
2 -)For the second record, use “www”:
— Assign the hostname “www” to the IP address of your droplet project.
- sudo certbot — nginx -d YOUR_DOMAIN.com -d www.YOUR_DOMAIN.com
- sudo nano /etc/nginx/sites-enabled/YOUR_REPOSITORY_NAME
server {
listen 80;
server_name YOUR_DOMAIN.com www.YOUR_DOMAIN.com;
# Redirect HTTP to HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name YOUR_DOMAIN.com www.YOUR_DOMAIN.com;
# SSL certificate paths
ssl_certificate /path/to/YOUR_DOMAIN.crt;
ssl_certificate_key /path/to/YOUR_DOMAIN.key;
# SSL configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
# Enable OCSP stapling
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Path to your website's files
root /var/www/YOUR_REPOSITORY_NAME;
index index.html index.htm index.php;
# Logs
access_log /var/log/nginx/YOUR_DOMAIN_access.log;
error_log /var/log/nginx/YOUR_DOMAIN_error.log;
location / {
try_files $uri $uri/ =404;
}
# Additional configuration can go here, such as caching rules, gzip compression, etc.
}
- In your Domains A — TTL means time to live and it should be lower for not latening the deployment