If you decide to deploy Laravel on a virtual server (VPS/VDS) running Linux (in our case it was Ubuntu), but don't know where to start, then you've come to the right place. In this article, we'll look at installing Laravel on Ubuntu 20.04 — from preparing the environment to setting up the web server.
“Laravel is not just a framework, but an entire ecosystem for developing PHP applications. The simplicity of the syntax and powerful built-in tools make it the number one choice for many developers.”
Step 1: Preparing the VPS
Before installing Laravel, make sure your virtual server is updated. Run:
sudo apt update && sudo apt upgrade -y
You will also need to create a user with sudo access and configure UFW (firewall). If all this is already done, let's move on!
Step 2: Install Composer
Laravel requires Composer, a package manager for PHP. Let's install it:
sudo apt install curl php-cli php-mbstring unzip git curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
Let's check:
composer --version
If we see the version number, then everything is ok.
Step 3: Installing the LEMP stack
For Laravel to work, you need to deploy LEMP (Linux, Nginx, MySQL, PHP):
sudo apt install nginx mysql-server php-fpm php-mysql
Launch and add to startup:
sudo systemctl enable --now nginx mysql php7.4-fpm
Step 4: Install Laravel
Now download Laravel:
cd /var/www sudo git clone https://github.com/laravel/laravel.git
Go to the project folder:
cd /var/www/laravel sudo composer install
Step 5: Setting up access rights
sudo chown -R www-data:www-data /var/www/laravel sudo chmod -R 755 /var/www/laravel sudo chmod -R 777 /var/www/laravel/storage
Now Laravel has the required permissions.
Step 6: Setting up the environment
Create a configuration file:
cp .env.example .env
Generate an application key:
php artisan key:generate
“The .env file is the heart of Laravel’s configuration. It’s where you set up database settings, operating modes, and other critical settings.”
Edit .env:
nano .env
Update database parameters:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=your-password
Save the changes.
Step 7: Configure MySQL
Let's create a database and a user:
mysql -u root -p CREATE DATABASE laravel; CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'your-password'; GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost'; FLUSH PRIVILEGES; EXIT;
Step 8: Configure Nginx
Editing the config:
sudo nano /etc/nginx/sites-available/laravel
Add:
server { listen 80; server_name your-domain.com; root /var/www/laravel/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:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(ht|git|env|svn) { deny all; } }
Save, create a symbolic link and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/ sudo systemctl restart nginx
Step 9: Test and Launch
Let's run database migrations:
php artisan migrate
Open your browser and go to https://sidata.com.ua. If everything is done correctly, we will see the Laravel start page!
Now Laravel is successfully running on your virtual server. As you can see, there is nothing complicated, the main thing is to follow the instructions and check for errors if something went wrong.
“The best way to learn Laravel is to deploy it on a VPS and start developing your first project!”
Just contact us and we will help you choose the best solution for you.