Setting up an Ubuntu Linux Server for Laravel

Configuring a Linux server for Laravel can feel like a hassle—especially if you’re doing it for the first time. We’ve been through the struggle a few times, so we thought we’d share the setup process we use to get our Laravel apps running smoothly on Ubuntu.

Let’s get started.


1. Create a New User for Laravel

Start by logging into your server via SSH as root:

ssh root@your-server-ip

Now add a new user:

adduser dev

Give the new user sudo privileges:

usermod -aG sudo dev

Check if the user was added:

cut -d: -f1 /etc/passwd | grep dev

Then switch to the new user:

su - dev

2. Install and Configure NGINX

Update your system packages:

sudo apt update && sudo apt upgrade -y

Install NGINX:

sudo apt install nginx nginx-extras -y

Start and enable NGINX:

sudo systemctl start nginx
sudo systemctl enable nginx

Allow HTTP traffic through the firewall:

sudo ufw allow 'Nginx Full'
sudo ufw enable

3. Install PHP and Required Extensions

Install PHP and extensions required by Laravel:

sudo apt install php php-fpm php-mysql php-xml php-mbstring php-curl php-bcmath php-zip php-cli php-tokenizer unzip -y

Check PHP version:

php -v

4. Install Composer (PHP Dependency Manager)

cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Check Composer version:

composer --version

5. Deploy Your Laravel Project

Clone Your Repository

Make sure git is installed:

sudo apt install git -y

Clone your Laravel project:

git clone https://github.com/yourusername/your-laravel-app.git
cd your-laravel-app

Copy .env File

If you have an .env file from development or staging, copy it over:

cp .env.example .env

Then update database credentials and any other relevant settings.


Install PHP Dependencies

composer install --no-dev --optimize-autoloader

Generate Application Key

php artisan key:generate

Migrate the Database

Make sure your database is configured and accessible.

php artisan migrate

If you have seeders:

php artisan db:seed

Link the Storage Folder

php artisan storage:link

Set File Permissions

sudo chown -R www-data:www-data /home/dev/your-laravel-app
sudo chmod -R 775 /home/dev/your-laravel-app/storage
sudo chmod -R 775 /home/dev/your-laravel-app/bootstrap/cache

6. Configure NGINX for Laravel

Create a new site configuration:

sudo nano /etc/nginx/sites-available/your-laravel-app

Paste this sample config:

server {
listen 80;
server_name your-domain.com;
root /home/dev/your-laravel-app/public;
add_header X-Frame-Options “SAMEORIGIN”;
add_header X-Content-Type-Options “nosniff”;index index.php index.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; # Adjust if you’re using another PHP version
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

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

Enable the site:

sudo ln -s /etc/nginx/sites-available/your-laravel-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

7. Optional: Set Up SSL with Let’s Encrypt

If your domain is pointing to the server:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com

Follow the prompts to install a free SSL certificate.


Done!

Your Laravel app should now be up and running on your Ubuntu server.