Before installing any software, update your server’s package list:
sudo apt update && sudo apt upgrade -yTo install Nginx, run:
sudo apt install nginx -yAfter 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
Open your browser and visit your server's IP address:
http://YOUR_SERVER_IP
You should see the Nginx default page.
If UFW (Uncomplicated Firewall) is enabled, allow Nginx traffic:
sudo ufw allow 'Nginx Full'Check the firewall status:
sudo ufw statusNginx uses server blocks to manage multiple websites.
1️⃣ Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/example.com2️⃣ 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.comwith your actual domain name and update the PHP version (php8.1-fpm.sockbased on your installed version).
1️⃣ Create the web root directory:
sudo mkdir -p /var/www/example.com/public2️⃣ Set proper permissions:
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com3️⃣ 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 -t5️⃣ Restart Nginx to apply changes:
sudo systemctl restart nginxIf 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 -yEnsure PHP is running:
sudo systemctl status php8.1-fpmIf it's not running, start it:
sudo systemctl start php8.1-fpmIf your application requires a database, install MySQL:
sudo apt install mysql-server -ySecure the MySQL installation:
sudo mysql_secure_installationLog into MySQL:
sudo mysql -u root -pCreate 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;Install Certbot:
sudo apt install certbot python3-certbot-nginx -yObtain and install the SSL certificate:
sudo certbot --nginx -d example.com -d www.example.comTest automatic certificate renewal:
sudo certbot renew --dry-runIf 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/cache3️⃣ Set up the environment file (.env):
cp /var/www/example.com/.env.example /var/www/example.com/.env
nano /var/www/example.com/.envUpdate 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=strongpassword4️⃣ Run database migrations:
php artisan migrate --force5️⃣ Restart Nginx:
sudo systemctl restart nginxIf your Laravel project uses queues, set up Supervisor: 1️⃣ Install Supervisor:
sudo apt install supervisor -y2️⃣ Create a Supervisor configuration:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf3️⃣ 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.log4️⃣ Reload Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*Check Nginx logs:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.logCheck Laravel logs:
tail -f /var/www/example.com/storage/logs/laravel.logRestart services if needed:
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm
sudo systemctl restart mysql🎉 You have successfully set up an Nginx web server with Laravel, PHP, MySQL, and SSL. Your website should now be live and secure.