Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install Laravel with Apache on Ubuntu 24.04

How to Install Laravel with Apache on Ubuntu 24.04

Setting up the Laravel PHP framework with Apache on Ubuntu 24.04 allows developers to build modern, secure, and scalable web applications efficiently. This guide walks you through the entire process, from preparing your server to deploying your Laravel app, ensuring a smooth installation even if you’re new to server management or PHP development. Before starting, ensure you have a fresh Ubuntu 24.04 server with root or sudo privileges. If you haven’t installed a server OS yet, do so first. Additionally, having basic knowledge of terminal commands and web server configuration will help you follow the steps more comfortably.

What You’ll Need

  • Ubuntu 24.04 server with root or sudo access
  • Basic familiarity with terminal commands
  • Domain name pointed to your server’s IP (optional but recommended)
  • SSH client (like PuTTY or terminal access)

Step-by-Step

  1. Update Your System
    First, ensure your server packages are up-to-date to avoid compatibility issues:

    sudo apt update && sudo apt upgrade -y
  2. Install Apache Web Server
    Ubuntu 24.04 typically comes with Apache available in its repositories. Install it with:

    sudo apt install apache2 -y

    After installation, enable and start Apache:

    sudo systemctl enable apache2
    sudo systemctl start apache2
  3. Install PHP and Required Extensions
    Laravel requires PHP 8.0 or higher. Install PHP along with necessary extensions:

    sudo apt install php php-cli php-mbstring php-xml php-bcmath php-curl php-mysql php-zip php-gd php-intl php-xmlrpc php-soap php-bcmath -y

    Confirm PHP installation:

    php -v
  4. Install Composer
    Composer is PHP’s dependency manager, essential for installing Laravel:

    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

    Verify Composer:

    composer --version
  5. Create a Directory for Your Laravel Project
    Choose a location, typically under /var/www:

    sudo mkdir -p /var/www/laravelapp

    Change ownership to your user (replace ‘youruser’ with your actual username):

    sudo chown -R youruser:youruser /var/www/laravelapp
  6. Install Laravel via Composer
    Navigate to your project directory and create a new Laravel project:

    cd /var/www/laravelapp
    composer create-project --prefer-dist laravel/laravel .
  7. Configure Apache for Laravel
    Create a new virtual host configuration:

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

    Paste the following configuration, replacing ‘yourdomain.com’ with your domain or server IP:

    <VirtualHost *:80>
        ServerName yourdomain.com
        ServerAdmin admin@yourdomain.com
        DocumentRoot /var/www/laravelapp/public
    
        <Directory /var/www/laravelapp/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/laravel_error.log
        CustomLog ${APACHE_LOG_DIR}/laravel_access.log combined
    </VirtualHost>

    Enable the site and rewrite module:

    sudo a2ensite laravel.conf
    sudo a2enmod rewrite
    sudo systemctl reload apache2
  8. Set Permissions
    Ensure the storage and bootstrap/cache directories are writable:

    sudo chown -R www-data:www-data /var/www/laravelapp
    sudo chmod -R 775 /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache
  9. Test Your Laravel Installation
    Open a browser and navigate to your server’s IP or domain. You should see Laravel’s welcome page. If not, check your Apache configuration and error logs.

Common Mistakes

  • Not updating package lists before installing new software, leading to outdated or missing packages.
  • Forgetting to enable mod_rewrite in Apache, which causes URL routing issues in Laravel.
  • Incorrect permissions on storage and bootstrap/cache directories, resulting in runtime errors.
  • Using a domain name without properly pointing DNS records to the server’s IP address.
  • Not restarting Apache after changes to configuration files, which prevents new settings from taking effect.

Tips

  • Use SSH keys for secure and easier server access.
  • Back up your server configuration files before making major changes.
  • Consider setting up HTTPS with Certbot for secure connections, especially on production servers.
  • Regularly update your server packages and Laravel dependencies to keep your environment secure.
  • Use version control like Git to manage your Laravel project’s codebase.

Troubleshooting

If your Laravel application does not load correctly or you encounter errors, follow these steps:

  • Check Apache Error Logs: Review logs at /var/log/apache2/error.log for clues about misconfigurations or permission issues.
  • Verify Permissions: Ensure the web server user (usually www-data) has proper read/write permissions on storage and cache directories.
  • Confirm Virtual Host Settings: Make sure your site configuration points to the correct public directory and is enabled.
  • Test PHP Processing: Create a simple PHP info file (e.g., info.php with ) in your document root to verify PHP is working with Apache.
  • Clear Cache: Run commands like php artisan cache:clear and php artisan config:clear within your Laravel directory to refresh configuration cache.
  • Check DNS and Domain Settings: Ensure your domain points correctly to your server’s IP address if you’re using a domain name.

If problems persist, consult Laravel and Apache community forums or documentation for specific error messages.

Frequently Asked Questions

1. Can I install Laravel without a domain name?

Yes, you can access Laravel via your server’s IP address directly, but using a domain name is recommended for easier access and professionalism.

2. Do I need to install MySQL or other databases?

Yes, if your Laravel application requires a database, install MySQL or MariaDB separately and configure your database credentials in Laravel’s .env file.

3. How do I enable HTTPS on my Laravel site?

Use Certbot with Let’s Encrypt to generate SSL certificates and configure Apache to serve your site over HTTPS for secure communication.

4. How can I update Laravel in the future?

Navigate to your project directory and run composer update, but always backup your project before major updates to avoid data loss.

5. Is it safe to run Laravel on a production server?

Yes, but ensure you follow security best practices: use HTTPS, keep software updated, set proper permissions, and configure firewalls.

6. How do I troubleshoot database connection issues?

Check your .env file for correct database credentials, ensure the database server is running, and verify network access permissions.

Related

How to Install Laravel with Apache on Ubuntu 24.04
Scroll to top