Home > Article > Backend Development > Easy Laravel Deployment on Ubuntu: A Beginners Guide with LEMP Stack
Deploying a Laravel application on Ubuntu with the LEMP stack (Linux, Nginx, MySQL, PHP) can seem daunting, but breaking it down step by step makes it manageable. This guide will walk you through the process from server setup to deploying a Laravel application.
Start by ensuring that your server is up to date.
sudo apt update && sudo apt upgrade -y
Nginx will serve your application.
sudo apt install nginx -y
Once installed, you can start and enable Nginx to run on boot:
sudo systemctl start nginx sudo systemctl enable nginx
You can verify that Nginx is running by visiting your server’s IP address in a browser.
Next, we’ll install the MySQL database server.
sudo apt install mysql-server -y
Secure the MySQL installation:
sudo mysql_secure_installation
This will prompt you to set up a root password and remove insecure defaults.
Laravel requires PHP, so let's install it along with some necessary extensions:
sudo apt install php-fpm php-mysql php-cli php-xml php-mbstring php-curl php-zip -y
Verify the PHP installation:
php -v
You should see something like:
PHP 7.x.x (cli) (built: ...)
Log in to the MySQL console as the root user:
sudo mysql
Create a new database and user for the Laravel application:
CREATE DATABASE laravel_app; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password'; GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES; EXIT;
Ensure that the new database user can connect:
mysql -u laravel_user -p
You’ll be prompted for the password, then enter:
SHOW DATABASES;
You should see laravel_app in the list.
Laravel uses Composer as its dependency manager. Install Composer:
sudo apt install composer -y
Navigate to the directory where you want to install Laravel (e.g., /var/www/):
cd /var/www/ composer create-project --prefer-dist laravel/laravel laravel_app
Laravel requires some directories to be writable by the web server:
sudo chown -R www-data:www-data /var/www/laravel_app sudo chmod -R 775 /var/www/laravel_app/storage sudo chmod -R 775 /var/www/laravel_app/bootstrap/cache
In the Laravel project root, open the .env file and configure the database settings:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=laravel_user DB_PASSWORD=strong_password
We'll create an Nginx configuration file for the Laravel project.
sudo nano /etc/nginx/sites-available/laravel_app
Add the following configuration to the file:
server { listen 80; server_name your_domain_or_ip; root /var/www/laravel_app/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.x-fpm.sock; # Change this to the correct PHP version. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
Replace your_domain_or_ip with your actual domain name or server IP address.
Enable the new Nginx configuration by creating a symbolic link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/laravel_app /etc/nginx/sites-enabled/
Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
If everything is fine, restart Nginx:
sudo systemctl reload nginx
Run the Laravel migrations to set up the database:
cd /var/www/laravel_app php artisan migrate
You should now be able to access the Laravel application by navigating to your server’s IP or domain in the browser. You’ll see the default Laravel welcome page.
If you have a domain, secure your site with Let's Encrypt:
sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d your_domain
Follow the instructions to install an SSL certificate. Certbot will automatically configure Nginx to redirect HTTP traffic to HTTPS.
Queues handle tasks like sending emails or processing jobs in the background.
php artisan queue:work
Use Laravel's task scheduling feature for tasks like clearing caches, sending daily emails, etc.
sudo crontab -e
Add the following line:
* * * * * php /var/www/laravel_app/artisan schedule:run >> /dev/null 2>&1
You’ve successfully deployed a Laravel application on an Ubuntu server using the LEMP stack. From here, you can continue to develop your application, secure it, and monitor it for performance.
If you encounter any issues, check the Nginx error logs at /var/log/nginx/error.log or Laravel logs at /var/www/laravel_app/storage/logs/laravel.log.
With these steps, you've completed a full hands-on Laravel deployment!
The above is the detailed content of Easy Laravel Deployment on Ubuntu: A Beginners Guide with LEMP Stack. For more information, please follow other related articles on the PHP Chinese website!