Among the web development frameworks, Laravel is always heard and is the priority of developers to improve and develop complex web applications. Since the PHP programming language is one of the best programming languages in the field of web design, Laravel is a PHP programming language framework that is provided as an open source and free and is based on the MVC architecture and the Symphony framework. Usually, it creates PHP-based web applications by delivering intelligent features.
It also follows the model-view-controller architecture pattern. Using Laravel, you can change many parts’ functionality without changing the main code. In addition, it offers various services such as Artisan console, user access, authentication, cache, template engine, database transfer system, payment, billing, etc. its best features are Laravel’s unparalleled security, development speed, and scalability. Laravel has made the development of web-based applications easier and faster by providing advanced tools and unique features.
This article will teach you how to install Laravel on Ubuntu 20.04. However, the instructions provided in this tutorial are applicable to any Ubuntu-based distribution, including Linux Mint, Kubuntu, and Elementary OS. It should also be noted that the most stable version of the Ubuntu operating system is the LTS version. For operating environments, the LTS version is the latest and best version. If you are interested in using Laravel after purchasing a Linux VPS, you can, Through this tutorial, install Laravel on Ubuntu 20.04 and enjoy the wonderful features of Laravel on Ubuntu. Join us to install Laravel on Ubuntu 20.04 easily.
Contents
Steps to install Laravel on Ubuntu 20.04
Step 1: Install Apache web server
To host the Laravel program, you need to install a web server, which can install either Nginx or Apache web servers. To choose one of the two proposed web servers, you can Compare Apache with Nginx and select one after familiarizing yourself with them. In this tutorial, we preferred installing the Apache web server. For this purpose, enter the apache2 command:
1 |
<span class="hljs-variable">$ </span>sudo apt install apache2 |
After installing the Apache web server, you should see it running. If it doesn’t work for any reason, start Apache:
1 |
<span class="hljs-variable">$</span> sudo systemctl <span class="hljs-built_in">start</span> apache2 |
Then enter the following command to enable Apache at boot time:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo systemctl <span class="hljs-built_in">enable</span> apache2</span> |
You can check the Apache status and make sure it is running without errors using the following command:
1 |
<span class="hljs-variable">$ </span>sudo systemctl status apache2 |
Step 2: Installing PHP and its additional plugins
PHP 7.3 and above is a prerequisite for using Laravel 8, which is available in the Ubuntu repositories, PHP 7.4. As a result, type the following command to install PHP and its required modules:
1 |
$ sudo apt install php libapache2-<span class="hljs-keyword">mod</span>-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql |
Verify the PHP version after successfully installing PHP using the following command:
1 |
<span class="hljs-variable">$ </span>php –v |
Step3: creating a database
To store the data generated by the Laravel program, you need a database. At this stage, you must create a database for the Laravel program. Since Laravel is compatible with MariaDB, MySQL, SQLite, Postgres, or SQL Server database systems, we will install the MariaDB database engine:
1 |
<span class="hljs-variable">$ </span>sudo apt install mariadb-server |
To access the MariaDB prompt after installing the database server, you need to use the following command:
1 |
<span class="hljs-variable">$ </span>sudo mysql -u root -p |
Sign in, then make a database and a user for it; grant the database user the necessary permissions.
1 |
<span class="hljs-keyword">CREATE</span> DATABASE laravel_db; |
1 |
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">USER</span> <span class="hljs-string">'laravel_user'</span>@<span class="hljs-string">'localhost'</span> IDENTIFIED <span class="hljs-keyword">BY</span> <span class="hljs-string">'secretpassword'</span>; |
1 |
GRANT <span class="hljs-keyword">ALL</span> <span class="hljs-keyword">ON</span> laravel_db.* <span class="hljs-keyword">TO</span> <span class="hljs-symbol">'laravel_user</span>'@<span class="hljs-symbol">'localhost</span>'; |
1 |
<span class="hljs-attribute">FLUSH</span> PRIVILEGES; |
1 |
<span class="hljs-keyword">QUIT;</span> |
Step 4: Install Composer
Composer is a package manager and prerequisite management tool for PHP and manages the libraries and dependencies required by PHP based on the particular framework. In this section, we will use it to download the Laravel core and install all the necessary Laravel components, so to install Composer, it is required to download the Composer installer (composer. phar file) by executing the curl command and move the file to the usr/local/bin/ directory:
1 |
<span class="hljs-variable">$ </span>curl -sS <span class="hljs-symbol">https:</span>/<span class="hljs-regexp">/getcomposer.org/installer</span> |<span class="hljs-params"> php</span> |
After executing the above command, the composer. phar file will be downloaded, and to move the file to the usr/local/bin/ path, run the following command:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo <span class="hljs-built_in">mv</span> composer.phar /usr/local/bin/composer</span> |
Assign authority to execute:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo <span class="hljs-built_in">chmod</span> +x /usr/local/bin/composer</span> |
Verifying the Composer version after installing it is done through the following command:
1 |
$ composer –<span class="hljs-built_in">version</span> |
Step 5: Install Laravel 8 on Ubuntu
After completing Composer installation, it is time to install Laravel.
To install Laravel, you must first go to the webroot directory, and for this purpose, you must type the following command:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash"><span class="hljs-built_in">cd</span> /var/www/html</span> |
Then, to install Laravel, you must use the create-project command for the composer:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo composer create-project laravel/laravel laravelapp</span> |
With the help of this command, you create a new directory called laravelapp to install the necessary files and directories of Laravel. The web server user is set to own the Laravel directory:
1 |
sudo chown -R www-<span class="hljs-keyword">data</span>:www-<span class="hljs-keyword">data</span> /<span class="hljs-keyword">var</span>/www/html/laravelapp |
1 |
<span class="hljs-selector-tag">sudo</span> <span class="hljs-selector-tag">chmod</span> <span class="hljs-selector-tag">-R</span> <span class="hljs-number">775</span> /<span class="hljs-selector-tag">var</span>/<span class="hljs-selector-tag">www</span>/<span class="hljs-selector-tag">html</span>/<span class="hljs-selector-tag">laravelapp</span>/<span class="hljs-selector-tag">storage</span> |
Then you can choose a new directory name for laravelapp.
Once the installation process is finished, go to the installation directory to check the version of Laravel:
1 |
<span class="hljs-variable">$</span> <span class="hljs-built_in">cd</span> laravelapp |
1 |
<span class="hljs-variable">$ </span>php artisan |
Step 6: Configure Apache to serve Laravel
To host a Laravel site, it is necessary to configure the Apache web server, and for this purpose, you must launch the virtual host file:
1 |
<span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo vim /etc/apache2/sites-available/laravel.conf</span> |
In this step, skip the displayed content and change the example.com address of the ServerName Instructions to the fully qualified domain name (FQDN) or the server’s public IP (or private IP if the server is located in a LAN network).
1 2 3 4 5 6 7 8 9 10 11 |
<span class="hljs-section"><VirtualHost *<span class="hljs-number">:80</span>></span> <span class="hljs-attribute">ServerName</span> example.com <span class="hljs-attribute">ServerAdmin</span> admin@example.com <span class="hljs-attribute">DocumentRoot</span> /var/www/html/laravelapp/public <span class="hljs-section"><Directory /var/www/html/laravelapp></span> <span class="hljs-attribute">AllowOverride</span> <span class="hljs-literal">All</span> <span class="hljs-section"></Directory></span> <span class="hljs-attribute">ErrorLog</span> <span class="hljs-variable">${APACHE_LOG_DIR}</span>/error.log <span class="hljs-attribute">CustomLog</span> <span class="hljs-variable">${APACHE_LOG_DIR}</span>/access.log combined <span class="hljs-section"></VirtualHost></span> |
Now you need to save the changes made and close the file. In the next step, you must execute these two commands to activate the Laravel site and the Apache rewrite module.
1 |
<span class="hljs-variable">$ </span>sudo a2ensite laravel.conf |
1 |
<span class="hljs-variable">$ </span>sudo a2enmod rewrite |
For your changes to take effect, restart Apache with the following command:
1 |
<span class="hljs-variable">$ </span>sudo systemctl restart apache2 |
Step 7: Run Laravel in a web browser
Finally, to access Laravel, refer to the Fully Qualified Domain Name (FQDN) or IP address of the server where you installed Laravel. If you do not specify a URL, Laravel will use the default URL. If you followed the steps correctly and the installation was successful, you will be presented with the Laravel page.
Source: https://operavps.com/docs/install-laravel-on-ubuntu/