Skip to content

Instantly share code, notes, and snippets.

@imrankabir02
Created February 23, 2025 05:49
Show Gist options
  • Select an option

  • Save imrankabir02/1438feb03710ff6b2c942faabbf92dc2 to your computer and use it in GitHub Desktop.

Select an option

Save imrankabir02/1438feb03710ff6b2c942faabbf92dc2 to your computer and use it in GitHub Desktop.
Step-by-step guide to setting up an Nginx server

Step 1: Update Your Server Packages

Before installing any software, update your server’s package list:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

To install Nginx, run:

sudo apt install nginx -y

Check Nginx Status

After installation, verify that Nginx is running:

sudo systemctl status nginx
  • If Nginx is not running, start it with:
    sudo systemctl start nginx
  • Enable it to start on boot:
    sudo systemctl enable nginx

Test Nginx Installation

Open your browser and visit your server's IP address:

http://YOUR_SERVER_IP

You should see the Nginx default page.


Step 3: Configure Firewall

If UFW (Uncomplicated Firewall) is enabled, allow Nginx traffic:

sudo ufw allow 'Nginx Full'

Check the firewall status:

sudo ufw status

Step 4: Configure Nginx for Your Website

Create a Server Block Configuration

Nginx uses server blocks to manage multiple websites.
1️⃣ Create a new Nginx configuration file:

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

2️⃣ Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/public;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Make sure to replace example.com with your actual domain name and update the PHP version (php8.1-fpm.sock based on your installed version).


Step 5: Enable the Configuration

1️⃣ Create the web root directory:

sudo mkdir -p /var/www/example.com/public

2️⃣ Set proper permissions:

sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com

3️⃣ Enable the Nginx site configuration:

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

4️⃣ Test the Nginx configuration for errors:

sudo nginx -t

5️⃣ Restart Nginx to apply changes:

sudo systemctl restart nginx

Step 6: Install and Configure PHP (For Laravel)

If you are hosting a Laravel project, install PHP and required extensions:

sudo apt install php8.1-fpm php8.1-cli php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-curl php8.1-zip unzip -y

Ensure PHP is running:

sudo systemctl status php8.1-fpm

If it's not running, start it:

sudo systemctl start php8.1-fpm

Step 7: Install MySQL Database (Optional)

If your application requires a database, install MySQL:

sudo apt install mysql-server -y

Secure the MySQL installation:

sudo mysql_secure_installation

Log into MySQL:

sudo mysql -u root -p

Create a new database and user:

CREATE DATABASE example_db;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 8: Secure Nginx with Let's Encrypt SSL (HTTPS)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain and install the SSL certificate:

sudo certbot --nginx -d example.com -d www.example.com

Test automatic certificate renewal:

sudo certbot renew --dry-run

Step 9: Deploy Laravel Application

If you are deploying a Laravel application: 1️⃣ Upload your Laravel project to /var/www/example.com
2️⃣ Set permissions:

sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 775 /var/www/example.com/storage
sudo chmod -R 775 /var/www/example.com/bootstrap/cache

3️⃣ Set up the environment file (.env):

cp /var/www/example.com/.env.example /var/www/example.com/.env
nano /var/www/example.com/.env

Update the database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=example_db
DB_USERNAME=example_user
DB_PASSWORD=strongpassword

4️⃣ Run database migrations:

php artisan migrate --force

5️⃣ Restart Nginx:

sudo systemctl restart nginx

Step 10: Configure Supervisor for Queue Processing (For Laravel)

If your Laravel project uses queues, set up Supervisor: 1️⃣ Install Supervisor:

sudo apt install supervisor -y

2️⃣ Create a Supervisor configuration:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

3️⃣ Add the following:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/example.com/artisan queue:work --tries=3
autostart=true
autorestart=true
numprocs=2
user=www-data
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log

4️⃣ Reload Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

Step 11: Monitor and Debug

Check Nginx logs:

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Check Laravel logs:

tail -f /var/www/example.com/storage/logs/laravel.log

Restart services if needed:

sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm
sudo systemctl restart mysql

🎯 Conclusion

🎉 You have successfully set up an Nginx web server with Laravel, PHP, MySQL, and SSL. Your website should now be live and secure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment