How to install Laravel on Ubuntu
Laravel is a free, open-source web framework that includes tools and resources for building feature-rich PHP applications. Laravel infrastructure was developed by Taylor Otwell, and he posted the source code of his brainchild on GitHub almost from the very beginning, which greatly helped popularize and spread the product.
In this article, we will show you how to install Laravel on Ubuntu 20.04, from preparation to the setup of public directories.
Prerequisites
There are three tasks to perform before installing Laravel on a dedicated server with Ubuntu:
- Create an account with sudo privileges and activate the Uncomplicated Firewall (UFW).
- Download the Composer package manager, which we will need to further install Laravel and its components on the server.
- Install the LEMP software stack and MySQL database management system on the dedicated server.
After that, also execute two commands:
sudo apt update sudo apt upgrade
This is necessary in order to install all the latest and up-to-date updates for the system. When the preparatory work is done, you can proceed directly to installing the framework.
Laravel installation guide for Ubuntu 20.04
You can always find the most up-to-date version of Laravel in various repositories, but the best way is to use Github, since it has the largest community and free maintenance. To download from there, use the command:
cd /var/www git clone https://github.com/laravel/laravel.git
Now switch to the framework directory and use the Composer manager to load all the dependencies it needs to work:
cd /var/www/laravel sudo composer install
And finally, set all necessary permissions for new files through the console:
chown -R www-data.www-data /var/www/laravel chmod -R 755 /var/www/laravel chmod -R 777 /var/www/laravel/storage
That's it, there is nothing complicated about installing Laravel on VPS, everything is done in a few easy steps. But this is not the end, because we still need to configure the environment, the DBMS, and the webserver.
How to set up the environment
At this point, we need to create a Laravel configuration file, and we recommend simply renaming the .evn.example file .env. Command:
mv .env.example .env
Now generate a base64 random number encryption key, it is required by the Illuminate service:
php artisan key:generate
You will get the message "Application key set successfully", after which you can start editing the configuration file. Open it with the command:
nano .env
You will see a list with many different variables, and we don't need to edit them all. We only pay attention to these parameters:
- APP_NAME: The name of the application used by the notification and messaging services.
- APP_ENV: the current environment.
- APP_KEY: unique encryption key, which is automatically generated during Laravel installation through Composer. Do not change it.
- APP_DEBUG: determines whether client-side debugging data will be shown.
- APP_URL: the base URL of the application used to generate links to the application.
- DB_DATABASE: name of the database.
- DB_USERNAME: account name to connect to the database.
- DB_PASSWORD: database connection password.
All of these values are initially configured for the local Laravel Homestead development environment. You can change them if the system is intended to be developed, tested, or run in a production environment. For development and testing, the APP_DEBUG option should be enabled, and you will also need to set the development or testing parameters to emphasize the focus of the work. But in the production environment, the APP DEBUG option should be disabled to prevent the end user from getting important data about your application. After editing, don't forget to save your changes.
Correctly configuring the MySQL database
The first thing we do is create a MySQL database for our framework:
CREATE DATABASE laravel;
And then immediately create a new account with the full set of rights:
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'your-password'; GRANT ALL ON laravel.* to 'laravel'@'localhost'; FLUSH PRIVILEGES;
After these manipulations are completed, leave the MySQL shell:
Quit
Then edit the familiar .env file and update the database settings:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=your-password
How to configure the Apache webserver for Laravel
The next step is to edit the Apache virtual host configuration file and update Document Root:
nano /etc/apache2/sites-enabled/000-default.conf
In this file we add the following lines:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/laravel/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/laravel> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
And at the end, restart the webserver so that all the changes you made start working.
Final settings: how to access the framework's web interface
By default, you can access Laravel through port 80. If you're using a firewall, you can go to the control panel at http://your-domen.com, http://ip-server-address. That's the end of our material, thank you for your attention!