4.1.4 How to Set Up a Development Environment for Multitasking Laravel Applications
Laravel is a popular open-source PHP framework designed for building web
applications with clean architecture, scalability, and modern development practices. Laravel features a powerful routing system, Blade templating, Eloquent ORM, API tools, and a built-in queue system—making it ideal for multitasking applications with background processing. Laravel is widely used for both simple websites and complex enterprise solutions.
Installing Laravel on a Hosting Service
To install Laravel on your hosting service, follow these steps:
- Initialize the project in your www directory using PHP Composer:
# Here we use PHP Composer 8.2 to get the latest Laravel version
composer-php-8.2 create-project laravel/laravel ~/www/test.com.ua
~/www/test.com.ua
— the destination for your Laravel projectcreate-project
— Composer command to scaffold a new projectlaravel/laravel
— the official Laravel starter template
- Create a database for your project:
Go to your hosting control panel. In “Tools”, select “Databases”:
Create a new database:
Open the file manager and edit your project’s config (e.g. ~/www/test.com.ua/config/
) with your DB parameters:
'mysql' => [
'driver' => 'mysql', // set to mysql
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'), // database name
'username' => env('DB_USERNAME', 'didi'), // username
'password' => env('DB_PASSWORD', ' '), // password
...
- Create an .htaccess file in the project root to redirect requests:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
</IfModule>
- Run database migrations:
php-8.2 artisan migrate
- Open your browser and enter your domain to test it. You should see the default Laravel welcome page:
System requirements
Before you start, make sure your server meets these requirements:
Installing Laravel on a Server
Before installing, update your system and enable necessary repositories for compatibility and security.
Update packages:
sudo apt update && sudo apt upgrade
Install utilities for managing external repositories:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
Add the official repository for the latest PHP versions:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Installing Nginx
sudo apt install nginx
Allow HTTP traffic via firewall:
sudo ufw allow 'Nginx HTTP'
Enable Nginx and autostart:
sudo systemctl start nginx
sudo systemctl enable nginx
Installing PHP and PHP-FPM
Laravel requires PHP. For multitasking apps, use PHP-FPM. Choose the appropriate version (e.g., PHP 8.2 or 8.3):
sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-mysql php8.2-mbstring php8.2-xml php8.2-bcmath php8.2-zip php8.2-curl php8.2-redis php8.2-dom
Check PHP-FPM status:
sudo systemctl status php8.2-fpm
Tune performance options in php.ini
:
sudo nano /etc/php/8.2/fpm/php.ini
Adjust:
memory_limit = 512M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
Save and restart PHP-FPM:
sudo systemctl restart php8.2-fpm
Installing MySQL
sudo apt install mysql-server
Secure installation:
sudo mysql_secure_installation
Create user and DB:
sudo mysql -u root -p
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY "your_password";
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Installing Composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Check Composer:
composer --version
Installing Redis
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-server
Check Redis status:
sudo systemctl status redis-server
Installing Supervisor
sudo apt install supervisor
sudo systemctl start supervisor
sudo systemctl enable supervisor
Create a config for queues:
sudo nano /etc/supervisor/conf.d/my-laravel-app-queue.conf
[program:my-laravel-app-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/my-laravel-app/artisan queue:work --sleep=3 --tries=3 --timeout=3600
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/my-laravel-app/storage/logs/queue.log
stopwaitsecs=3600
Config explanation
command
— artisan path and parameters;
autostart
/autorestart
— auto start/restart;
user
— usually www-data;
numprocs
— number of workers;
stdout_logfile
— log file path.
Important! Ensure www-data has write rights for your app’s storage/logs!
Update Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start my-laravel-app-queue:*
Check worker status:
sudo supervisorctl status
Creating a New Laravel Project
cd /var/www/html
composer create-project laravel/laravel my-laravel-app
cd my-laravel-app
Set up .env
(DB, CACHE, QUEUE):
cp .env.example .env
nano .env
APP_NAME="My Laravel App"
APP_ENV=local
APP_KEY=base64:YOUR_GENERATED_APP_KEY_HERE
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
BROADCAST_DRIVER=log
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Generate app key:
php artisan key:generate
Run migrations:
php artisan migrate
Nginx Setup for Laravel
Create a config:
sudo nano /etc/nginx/sites-available/my-laravel-app.conf
server {
listen 80;
server_name my-laravel-app.test;
root /var/www/html/my-laravel-app/public;
index index.php index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the site:
sudo ln -s /etc/nginx/sites-available/my-laravel-app.conf /etc/nginx/sites-enabled/
Test syntax:
sudo nginx -t
Reload:
sudo systemctl restart nginx
Set file permissions
sudo chown -R www-data:www-data /var/www/html/my-laravel-app/storage
sudo chown -R www-data:www-data /var/www/html/my-laravel-app/bootstrap/cache
sudo chmod -R 775 /var/www/html/my-laravel-app/storage
sudo chmod -R 775 /var/www/html/my-laravel-app/bootstrap/cache
Visit your domain or http://<server-ip>
.
/etc/hosts setup for local development
See details in our instruction.
Add your domain in /etc/hosts
:
sudo nano /etc/hosts
127.0.0.1 my-laravel-app.test Note: If the server is on a different IP, use that IP instead of 127.0.0.1. Now open http://my-laravel-app.test in your browser.
Common issues and solutions
Problem | Cause | Solution |
---|---|---|
“PHP module missing…” | Required PHP module not installed | Install missing module (apt install php8.2-... ) |
502/504 Gateway Timeout | Not enough memory, PHP not running | Increase memory limits, check RAM |
No write permission | Incorrect storage/cache folder permissions | Fix ownership: chown -R www-data:www-data |
Can’t connect to database | Wrong DB credentials or MySQL not running | Check DB info, MySQL status |
SSL/certificate error | No valid certificate or DNS issues | Check DNS, set up certbot |
“Permission denied” for queues | No rights to storage/logs | Grant www-data rights to storage/logs |
Migration error | Wrong .env or migrations already run | Check .env, php artisan migrate:fresh |