What is Laravel?
Laravel is a popular tool, called a framework, for building websites in PHP. A framework is like a ready-made kit of parts that saves you time. "Deploy" means moving your finished app from your computer to a live server.
The steps below use Ubuntu. Other systems work in a similar way, with different install commands.
Steps
- Log in to your VPS over SSH as root or a sudo user.
- Update the server and install the needed software:
This installs the web server, database, PHP and helper tools.sudo apt update sudo apt install nginx mysql-server php-fpm php-mysql php-mbstring php-xml php-curl php-zip unzip git -y - Install Composer, the tool that downloads Laravel's parts:
sudo apt install composer -y - Create a database. Open MySQL with
sudo mysql, then run:CREATE DATABASE laravel_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'ChooseAStrongPassword'; GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost'; EXIT; - Upload your app to
/var/www/myapp. You can use Git:cd /var/www sudo git clone YOUR_REPOSITORY_URL myapp - Install the app's parts:
cd /var/www/myapp composer install --no-dev --optimize-autoloader - Copy the settings file and make a key:
cp .env.example .env php artisan key:generate - Open
.envwith a text editor such asnano .env. Fill inDB_DATABASE,DB_USERNAMEandDB_PASSWORD. SetAPP_ENV=productionandAPP_DEBUG=false. - Build the database tables:
php artisan migrate --force - Give the web server write access to two folders:
sudo chown -R www-data:www-data storage bootstrap/cache sudo chmod -R 775 storage bootstrap/cache - Create a site file for Nginx at
/etc/nginx/sites-available/myapp. Point itsrootto/var/www/myapp/public, setserver_nameto your domain, and send PHP requests to PHP-FPM. - Switch the site on and reload Nginx:
Thesudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginxnginx -tline checks your file for mistakes first. - Point your domain's A record to the VPS IP address. Then open your domain in a browser.
Tip: Always point the web root at the
public folder. Never expose the whole app folder, as it holds your private .env file.Warning: migrate changes your database. Take a backup first if the database already holds real data.
Quick recap
- Install Nginx, MySQL, PHP and Composer.
- Create a database and upload the app.
- Run
composer install, set up.envand runmigrate. - Fix folder permissions and set the Nginx root to
public. - Point your domain to the server.