Laravel

How to Deploy a Laravel Project on an Ubuntu Server – A Step-by-Step Guide

So, you’ve built a fantastic Laravel application, and now it’s time to take it live! Deploying a Laravel project on an Ubuntu server might seem daunting at first, but don’t worry—I’ve got you covered. In this step-by-step guide, we’ll walk through everything you need to get your Laravel app running smoothly on an Ubuntu server.

Whether you’re a freelancer, a startup, or an enterprise, hosting your Laravel project on Ubuntu gives you full control, flexibility, and cost-efficiency. Let’s get started!


Step 1: Connect to Your Ubuntu Server

Before anything, you need access to your Ubuntu server. If you’re using a cloud provider like AWS, DigitalOcean, or Linode, you’ll be given SSH access details.

🔹 Open your terminal and connect to your server via SSH:

ssh username@your-server-ip

Replace username with your actual server username and your-server-ip with the IP address of your Ubuntu server.

Step 2: Update Your Server

It’s always a good practice to update your system before installing any software. Run:

sudo apt update && sudo apt upgrade -y

This ensures your Ubuntu system is up-to-date with the latest security patches.

Step 3: Install Apache or Nginx (Web Server)

🔹 Installing Apache

sudo apt install apache2 -y

Start and enable the Apache service:

sudo systemctl start apache2
sudo systemctl enable apache2

🔹 Installing Nginx (Alternative)

If you prefer Nginx, install it with:

sudo apt install nginx -y

Enable and start the service:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Install PHP and Required Extensions

Laravel requires PHP, so install it along with necessary extensions:

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

Check if PHP is installed correctly:

php -v

Step 5: Install MySQL Database

Laravel often needs a database. Install MySQL with:

sudo apt install mysql-server -y

Secure your MySQL installation:

sudo mysql_secure_installation

Then, log into MySQL and create a database for your Laravel project:

mysql -u root -p

Inside the MySQL prompt, run:

CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Update your Laravel .env file with these credentials.

Step 6: Install Composer and Laravel

Laravel relies on Composer, so install it:

sudo apt install composer -y

Navigate to your web directory and download Laravel:

cd /var/www/
git clone https://github.com/your-repository.git your-laravel-project
cd your-laravel-project
composer install

Step 7: Set Up Laravel Environment

  1. Copy the environment file:
cp .env.example .env

2. Generate the application key:

php artisan key:generate

3. Set correct permissions:

sudo chown -R www-data:www-data /var/www/your-laravel-project
sudo chmod -R 775 /var/www/your-laravel-project/storage /var/www/your-laravel-project/bootstrap/cache

4. Run database migrations:

php artisan migrate

Step 8: Configure Apache or Nginx for Laravel

🔹 Apache Configuration

Create a virtual host file:

sudo nano /etc/apache2/sites-available/laravel.conf

Add the following content:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/your-laravel-project/public
    <Directory /var/www/your-laravel-project>
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and restart Apache:

sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

🔹 Nginx Configuration

For Nginx, create a new server block:

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

Add this content:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/your-laravel-project/public;

    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/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Enable the site and restart Nginx:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Step 9: Set Up Laravel Queue and Scheduler

To handle background tasks, enable Laravel queues:

php artisan queue:work

For scheduling, open the crontab:

crontab -e

Add this line to run Laravel’s scheduler every minute:

* * * * * php /var/www/your-laravel-project/artisan schedule:run >> /dev/null 2>&1

Step 10: Secure Your Server with SSL

If you’re using a domain, secure it with a free Let’s Encrypt SSL certificate:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com

For Nginx:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com

Set up auto-renewal:

sudo certbot renew --dry-run

And there you have it! Your Laravel application is now live on an Ubuntu server. 🚀

Looking for an automated deployment solution for Laravel projects? CodeHunger can develop a one-click deployment tool that simplifies the process, ensuring smooth Laravel hosting with minimal manual setup.

Need help with Laravel projects? Check out CodeHunger for expert Laravel development solutions! 💡

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button