Setting up a Laravel Server on Debian 11
Introduction
This article provides a step-by-step guide for beginners on installing and configuring Laravel - a popular PHP framework - on the Debian 11 operating system.
Step 1: Connecting to the Server
To begin, connect to your server via SSH. You can do this using a program like PuTTY for Windows, or directly through the terminal for macOS and Linux:
ssh ваше_имя_пользователя@ваш_IP_сервера
Step 2: Updating Packages
First, make sure all packages are updated to their latest versions:
sudo apt update && sudo apt upgrade
Step 3: Installing Basic Tools
Install necessary tools that will be useful for further work:
sudo apt install -y htop mc etckeeper fail2ban ufw curl gnupg2 ca-certificates lsb-release xfsprogs mdadm
Step 4: Installing Nginx
Install the Nginx web server, which will serve our Laravel application:
echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt update
sudo apt install nginx
Step 5: Installing MySQL (Percona)
Install the MySQL database using Percona Server:
During MySQL installation, set a password and choose the "Legacy" authentication method.
wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb
sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
sudo percona-release setup ps80
sudo apt update
sudo apt install percona-server-server percona-server-common percona-xtrabackup-80
Step 6: Configuring MySQL Client
For ease of working with MySQL, set the password in the ~/.my.cnf file:
[client]
user=root
password=ВАШ_ПАРОЛЬ
Step 7: Installing PHP-FPM and Composer
Install PHP-FPM for processing PHP code and Composer for managing dependencies:
sudo apt install -y php7.4-{,mysql,cli,common,fpm,curl,mbstring,zip,pdo,gd,mbstring,bz2,msgpack,redis,dom,opcache,xml,xsl,soap}
sudo apt install composer
Step 8: Installing Laravel
Create a new Laravel project:
composer create-project laravel/laravel ваш_проект
Step 9: Editing the PHP-FPM Configuration File
Edit the file /etc/php/7.4/fpm/pool.d/www.conf, specifying the parameters:
user = ваш_пользователь
group = ваш_пользователь
listen.owner = ваш_пользователь
listen.group = ваш_пользователь
listen.mode = 0660
Where your_user is the username that will run the site.
Restart php-fpm:systemctl restart php7.4-fpm && systemctl enable php7.4-fpm
Step 10: Configuring Nginx for Laravel
Create a configuration file for your project:
sudo nano /etc/nginx/сonf.d/ваш_проект.conf
Add the following configuration:
server {
listen 80;
server_name ваш_домен_или_IP;
root /var/www/ваш_проект/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/php7.4-fpm.sock;
}
}
Restart Nginx and add it to autostart:
systemctl restart nginx && systemctl enable nginx