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
- Update your server: Run
sudo apt update && sudo apt upgrade -yto ensure all packages are current. - 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 - Install Composer: Download and install Composer globally:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer - 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 myprojectReplace
myprojectwith your desired project folder name. - 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 - Configure Nginx: Create a new site configuration file:
sudo nano /etc/nginx/sites-available/myprojectAdd the following configuration, replacing
yourdomain.comwith 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.
- 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 - 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 - Set database credentials: Edit
.envto match your database setup if needed. - Optional: Set up a database: Install MySQL/MariaDB, create a database, and user, then update
.envaccordingly. - Test your Laravel application: Visit
http://yourdomain.comin 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:
- Check Nginx configuration syntax: Run
sudo nginx -t. Fix any errors reported. - Verify PHP-FPM is running: Use
systemctl status php8.1-fpm. Restart if necessary:sudo systemctl restart php8.1-fpm. - Ensure correct permissions: Confirm that the
storageandbootstrap/cachedirectories are writable by the web server. - Check logs: Review Laravel logs in
storage/logsand Nginx error logs at/var/log/nginx/error.log. - Clear cache and configuration cache: Run
php artisan cache:clearandphp artisan config:cache. - 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 Hide Sender Pictures Or Initials In Windows 10 Mail App
- How to Hide Sender Initials and Pictures in Outlook.com
- How to Import Firefox Passwords to Edge in Windows 10