To Deploy a Laravel 11 application to a server involves several steps. Let’s go through a practical example tutorial on how to do this:
Step 1: Prepare your Laravel application
First, make sure your Laravel application is ready for deployment. Ensure that you have:
- Set up your Laravel application locally and tested it.
- Included a
.env
file with the appropriate environment configurations. - Configured your database settings.
Step 2: Set up your server
For this example, we’ll use a server running Ubuntu 20.04 and Nginx.
- SSH into your server:
ssh user@your_server_ip
- Update your server:
sudo apt update && sudo apt upgrade
- Install Nginx, PHP, and MySQL:
sudo apt install nginx php php-fpm php-mysql mysql-server
Step 3: Configure Nginx
- Create a new Nginx server block:
sudo nano /etc/nginx/sites-available/your_domain
- Add the following configuration:
server {
listen 80;
server_name your_domain.com;
root /var/www/your_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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- Enable the server block:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
- Test Nginx configuration:
sudo nginx -t
- Reload Nginx:
sudo systemctl reload nginx
Step 4: Deploy your Laravel application
- Copy your Laravel application to the server:
scp -r /path/to/your/laravel/app user@your_server_ip:/var/www/your_laravel_app
- Install composer dependencies:
cd /var/www/your_laravel_app
composer install --no-dev
- Set permissions:
sudo chown -R www-data:www-data /var/www/your_laravel_app
sudo chmod -R 755 /var/www/your_laravel_app/storage
- Generate application key:
php artisan key:generate
Step 5: Set up the database
- Create a new database and user:
mysql -u root -p
CREATE DATABASE your_database;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
- Update your
.env
file with the database details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_user
DB_PASSWORD=your_password
Step 6: Finalize
- Migrate your database:
php artisan migrate
- Optimize Laravel for production:
php artisan config:cache
php artisan route:cache
php artisan view:cache
- Restart Nginx:
sudo systemctl restart nginx
Now your Laravel 11 application should be successfully deployed to your server and accessible through your domain.