Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install Laravel with Nginx on Ubuntu 24.04

How to Install Laravel with Nginx on Ubuntu 24.04

Setting up the Laravel PHP framework with Nginx on Ubuntu 24.04 provides a robust, high-performance environment for developing modern PHP applications. Laravel offers elegant syntax and powerful features, while Nginx ensures fast web serving and scalability. This guide walks you through each step to get Laravel running smoothly on your Ubuntu 24.04 server, assuming you have basic familiarity with Linux command line operations and server management. Before starting, ensure your server is up-to-date, and you have root or sudo privileges.

What You’ll Need

  • Ubuntu 24.04 server with root or sudo access
  • Domain name pointed to your server’s IP (optional but recommended)
  • Basic knowledge of terminal commands and server management
  • SSH client to connect to your server

Step-by-Step

  1. Update your server: Run sudo apt update && sudo apt upgrade -y to ensure all packages are current.
  2. Install necessary dependencies: Install PHP, Composer, Nginx, and other required packages:
    sudo apt install php php-cli php-fpm php-mbstring php-xml php-bcmath php-curl php-zip php-mysql unzip curl git -y
  3. Install Composer: Download and install Composer globally:
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
  4. Create a Laravel project: Navigate to your web directory and create a new Laravel project:
    cd /var/www
    sudo composer create-project --prefer-dist laravel/laravel myproject

    Replace myproject with your desired project folder name.

  5. Set permissions: Ensure web server can access the files:
    sudo chown -R www-data:www-data /var/www/myproject
    sudo chmod -R 775 /var/www/myproject/storage /var/www/myproject/bootstrap/cache
  6. Configure Nginx: Create a new site configuration file:
    sudo nano /etc/nginx/sites-available/myproject

    Add the following configuration, replacing yourdomain.com with your domain or server IP:

    server {
        listen 80;
        server_name yourdomain.com;
        root /var/www/myproject/public;
    
        index index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.(?!well-known).*\{ 
            deny all; 
        
        }
    }

    Save and exit the editor.

  7. Enable the site and restart Nginx: Link your configuration and reload Nginx:
    sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
  8. Configure environment variables: Copy the example environment file and generate an app key:
    cd /var/www/myproject
    cp .env.example .env
    php artisan key:generate
  9. Set database credentials: Edit .env to match your database setup if needed.
  10. Optional: Set up a database: Install MySQL/MariaDB, create a database, and user, then update .env accordingly.
  11. Test your Laravel application: Visit http://yourdomain.com in your browser. You should see the Laravel welcome page or your app if configured.

Common Mistakes

  • Not updating server packages before starting, leading to dependency issues.
  • Incorrect permissions on storage and bootstrap/cache directories, causing errors.
  • Forgetting to restart Nginx after changing configuration files.
  • Using the wrong PHP-FPM socket path; ensure it matches your installed PHP version.
  • Not setting the correct server_name in Nginx config, leading to site not resolving.

Tips

  • Always back up your configuration files before making changes.
  • Use a domain with SSL for production environments; consider setting up Let’s Encrypt.
  • Optimize your Laravel app with caching commands like php artisan optimize.
  • Regularly update your server and PHP packages for security and performance.
  • Monitor your logs (/var/log/nginx/error.log and Laravel logs) for issues.

Troubleshooting

If your Laravel site is not loading properly or you encounter errors, follow these steps:

  1. Check Nginx configuration syntax: Run sudo nginx -t. Fix any errors reported.
  2. Verify PHP-FPM is running: Use systemctl status php8.1-fpm. Restart if necessary: sudo systemctl restart php8.1-fpm.
  3. Ensure correct permissions: Confirm that the storage and bootstrap/cache directories are writable by the web server.
  4. Check logs: Review Laravel logs in storage/logs and Nginx error logs at /var/log/nginx/error.log.
  5. Clear cache and configuration cache: Run php artisan cache:clear and php artisan config:cache.
  6. Verify DNS and server name: Ensure your domain points correctly to your server’s IP and your Nginx server_name matches.

Following these steps should help you identify and resolve common issues during the setup process.

Frequently Asked Questions

1. Can I install Laravel without a domain name?

Yes, you can access Laravel using your server’s IP address, but it’s recommended to set up a domain for easier access and SSL configuration.

2. How do I enable HTTPS for my Laravel site?

Use Let’s Encrypt with Certbot to obtain free SSL certificates and configure your Nginx server for HTTPS.

3. Can I run multiple Laravel sites on the same server?

Yes, by creating separate server blocks (virtual hosts) for each site in Nginx, each with its own root and server_name.

4. How do I update Laravel or its dependencies later?

Navigate to your project directory and run composer update. Remember to clear caches afterward.

5. What are the best practices for deploying Laravel in production?

Use environment-specific configs, enable caching, set correct permissions, use HTTPS, and regularly update your server and application dependencies.

Related

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