Skip to content
Alavps Blog
Menu
  • Home
  • Hosting
    • Shared Hosting
    • Cloud Hosting
    • VPS Hosting
  • Domains
  • Blog
  • Contact
Menu

How to Deploy Laravel on Ubuntu With Apache: A Comprehensive Guide

Posted on October 14, 2024 by alavpsblog

Did you know that Laravel, a PHP framework with a mere 12% market share, powers some of the world’s most popular websites like Twitch and 9GAG? Let’s dive into how you can harness this powerhouse by deploying Laravel on Ubuntu with Apache.

Prerequisites

Table of Contents

  • Prerequisites
  • Step 1: Install Required Software
  • Step 2: Configure MySQL
  • Step 3: Clone or Create Your Laravel Application
  • Step 4: Install Dependencies and Set Permissions
  • Step 5: Configure Laravel Environment
  • Step 6: Configure Apache Virtual Host
  • Step 7: Final Touches and Testing
  • Troubleshooting Common Issues

Before we jump in, make sure you’ve got:

  • An Ubuntu server (Ubuntu 20.04 or later is ideal)
  • SSH access to your server
  • A user with sudo superpowers
  • A stable internet connection (no dial-up, please!)
  • Basic command line know-how

Step 1: Install Required Software

Let’s get your server decked out with the necessary software:

  1. Update your package list:
    sudo apt update
    
  2. Install Apache:
    sudo apt install apache2
    

  3. Set up MySQL:
    sudo apt install mysql-server
    
  4. Get PHP and its sidekicks:
    sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-json php-mbstring php-xml php-zip
    
  5. Bring in Composer, the PHP dependency wizard:
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    
  6. Don’t forget Git:
    sudo apt install git
    

Step 2: Configure MySQL

Time to make MySQL play nice with Laravel:

  1. Lock down MySQL:
    sudo mysql_secure_installation
    
  2. Create a cozy database for your Laravel app:
    sudo mysql -u root -p
    CREATE DATABASE laravel_db;
    EXIT;
    

Step 3: Clone or Create Your Laravel Application

Let’s get your Laravel app on the server:

  1. Head to Apache’s web root:
    cd /var/www/html
    
  2. Clone your existing Laravel treasure trove or start fresh:
    # For existing projects:
    git clone https://github.com/your-username/your-laravel-repo.git
    # Or for a brand new Laravel adventure:
    composer create-project --prefer-dist laravel/laravel your-project-name
    
  3. Step into your project directory:
    cd your-project-name
    

Step 4: Install Dependencies and Set Permissions

  1. Get those PHP dependencies sorted:
    composer install
    composer dumpautoload
    
  2. Set permissions so Laravel can do its thing:
    sudo chown -R www-data:www-data storage bootstrap/cache
    sudo chmod -R 775 storage bootstrap/cache
    

Step 5: Configure Laravel Environment

  1. Create your .env file:
    cp .env.example .env
    
  2. Generate a shiny new app key:
    php artisan key:generate
    
  3. Open up .env and tweak your database settings:
    nano .env
    

    Update these lines with your MySQL info:

    DB_DATABASE=laravel_db
    DB_USERNAME=your_mysql_username
    DB_PASSWORD=your_mysql_password
    

Step 6: Configure Apache Virtual Host

  1. Create a new Apache config file for your Laravel app:
    sudo nano /etc/apache2/sites-available/laravel.conf
    
  2. Add this config, swapping your-domain.com with your actual domain or server IP:
    <VirtualHost *:80>
     ServerName your-domain.com
     ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html/your-project-name/public
     <Directory /var/www/html/your-project-name>
     AllowOverride All
     </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  3. Enable your new site:
    sudo a2ensite laravel.conf
    
  4. Show the default Apache site the door:
    sudo a2dissite 000-default.conf
    
  5. Turn on Apache’s rewrite module:
    sudo a2enmod rewrite
    
  6. Restart Apache to make it all stick:
    sudo systemctl restart apache2
    

Step 7: Final Touches and Testing

  1. Optimize Laravel for prime time:
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    
  2. One last permissions check:
    sudo chown -R www-data:www-data /var/www/html/your-project-name
    sudo find /var/www/html/your-project-name -type f -exec chmod 644 {} \;
    sudo find /var/www/html/your-project-name -type d -exec chmod 755 {} \;
    
  3. Fire up your browser and visit your domain or server IP to see your Laravel masterpiece in action.

Also Read: How To Install LAMP On Ubuntu 16.04 With A Single Command

Troubleshooting Common Issues

Hit a snag? Don’t sweat it. Here are some common hiccups and how to fix them:

  1. HTTP ERROR 500: Usually a permissions problem. Double-check those permissions from Step 4 and Step 7.
  2. “No input file specified” error: Make sure your Apache config is pointing to the right spot – the public directory of your Laravel app.
  3. Database connection woes: Triple-check your .env file for correct database credentials and ensure MySQL is up and running.
  4. Blank page or PHP errors: Take a peek at the Apache error logs (/var/log/apache2/error.log) for clues and tackle them one by one.

Following this guide should have your Laravel app up and running on Ubuntu with Apache faster than you can say “Artisan command”. Remember to keep your server and Laravel app updated to stay ahead of the game.

For more in-depth information on running Laravel locally on Ubuntu, check out this guide on running Laravel locally on Ubuntu using Apache virtual host. If you’re looking to deploy your Laravel app on a production server, this StackOverflow thread offers some great insights.

Want to dive deeper into Laravel deployment? This comprehensive guide covers everything from prerequisites to troubleshooting. And if you’re hungry for more Ubuntu-specific deployment tips, this detailed tutorial has got you covered.

Now go forth and Laravel like a pro!

Posted in How tos

Post navigation

How to Install Calibre Server & Calibre Web on Ubuntu 22.04
Brazil VPS Hosting – What You Need to Know Before Spending Your Money

Related Post

  • How to Create Studio Ghibli Style AI Images on ChatGPT for Free
  • How to Install Dual OS Windows 10 and Linux?
  • How To Use SMTP Ports: 25, 465, and 587
  • How CPU Cores Impact Your Website's Speed and Reliability? How CPU Cores Impact Your Website’s Speed and Reliability?
  • How to Install Calibre Server & Calibre Web on Ubuntu 22.04 How to Install Calibre Server & Calibre Web on Ubuntu 22.04
  • How To Install LAMP On Ubuntu 16.04 With A Single Command
  • How to Install cPanel on a VPS Server Using PuTTY? How to Install cPanel on a VPS Server Using PuTTY?
  • How To Install Third-Party Apps on My Windows VPS? How To Install Third-Party Apps on My Windows VPS?
  • How To Partition and Format Storage Devices in Linux

Leave a Reply Cancel reply

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

Categories

  • Hosting Advice
  • How tos
  • security tips
  • SSL
  • Tutorials
  • VPS Hosting
  • VPS Tips
  • Website Security
  • Website Tips
  • WordPress

Recent Posts

  • How to Create Studio Ghibli Style AI Images on ChatGPT for Free
  • 5 Free GPU Server for Students in 2025
  • How to Install Dual OS Windows 10 and Linux?
  • How To Use SMTP Ports: 25, 465, and 587
  • How CPU Cores Impact Your Website’s Speed and Reliability?
  • Brazil VPS Hosting – What You Need to Know Before Spending Your Money
  • How to Deploy Laravel on Ubuntu With Apache: A Comprehensive Guide
  • How to Install Calibre Server & Calibre Web on Ubuntu 22.04
  • How To Install LAMP On Ubuntu 16.04 With A Single Command
  • How to Install WordPress on a VPS Server (Manually)

Table Of ContentToggle Table of ContentToggle

  • Prerequisites
  • Step 1: Install Required Software
  • Step 2: Configure MySQL
  • Step 3: Clone or Create Your Laravel Application
  • Step 4: Install Dependencies and Set Permissions
  • Step 5: Configure Laravel Environment
  • Step 6: Configure Apache Virtual Host
  • Step 7: Final Touches and Testing
  • Troubleshooting Common Issues
Blog Update By Alavps