Install Laravel in Ubuntu 18.04

Laravel is one of the most popular frameworks for developing web applications and websites in PHP. It is objectively oriented, adheres to the MVC model, supports multiple databases, and has many tools to deploy your applications and to simplify the development process.

In this article we will look at how to install Laravel in Ubuntu 18.04 utility composer. It is much easier than ishodniki to download and install them manually. But the framework you will need to have installed the database, such as MariaDB, and web server such as Nginx or Apache. Read more about installing all those components, see install a LAMP stack on Ubuntu or how to set a web server on your own computer.

Install Laravel in Ubuntu 18.04

Before we proceed directly to the installation, we need to install several components. Composer first place, if it does not:

sudo apt install composer

I assume that the php interpreter you have installed, but additionally we will need the following libraries:

sudo apt install php-mysql php-mbstring php-tokenizer php-xml php-json php-common

Now you can install Laravel. First navigate to the folder where you have located web sites. Default in Ubuntu is /var/www:

cd /var/www/sites

To install all you need enough to run a single command:

sudo composer create-project --prefer-dist laravel/laravel laravelapp

The phrase laravelapp replace on the project name and the folder where is located your new project. The utility will automatically download the framework itself and all its dependencies. After installation you only need the correct rights on the files of the framework. The owner must be the user to run the web server and php. The default is www-data:

sudo chown-R www-data:www-data laravelapp

To complete the work of the framework database. In this example, we will configure MariaDB databases. Connect to the database and create a new table:

sudo mysql-u root-p

> CREATE DATABASE laravel;

Then create user for this database and give him all rights to it:

> CREATE USER `laravel_user`@`localhost` IDENTIFIED BY 'password';
> GRANT ALL ON laravel.* TO `laravel_user`@`localhost`;

Next, update the privileges table:

> FLUSH PRIVILEGES;

By default, Laravel is configured to use the database we need, but you need to specify the correct access to it. Open the file config/database.php and check the mysql section:

vi config/database.php

The syntax here is very simple. It is a regular php array, the peculiarity here is that the program tries to take the desired configuration from the environment variables using the function env. If the environment variable is not set, it is the second argument. It to you and you want to change. Change the name of the database (database), user name (username) and password (password). If the database is on the same host, the host address and port you can leave as is.

Configure web server for Laravel

Have to check how it works. You can run Laravel without external web servera, only by means of a built-in utility artisan:

php artisan serve

This command runs a private web server, which is available at 127.0.0.1:8000:

But this approach is not very practical. Usually we need to run the web application in Apache or Nginx. In Apache there are no special configuration is needed. Enough to create a virtual host for the directory where laravel and enabled for AllowOverride. For example, create a file in the directory sites-available:

sudo vi /etc/apache2/sites-available/laravelapp.conf

<VirtualHost *:80>
ServerName laravelapp.ru
ServerAdmin [email protected]
DocumentRoot /var/www/sites/laravelapp/public
<Directory "/var/www/sites/laravelapp/public">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here we use domain lavarelapp.ru and our files are located in /var/www/sites/laravelapp. Now you need to enable this site in apache:

sudo a2ensite laravelapp

sudo systemctl restart apache2

Domain laravelapp.ru virtual, he does not belong to me and you, I think, too, so you need to add an entry to /etc/hosts file to the local machine everything worked:

sudo vi /etc/hosts

127.0.01 laravelapp.ru

On a production server just use the real domain in the settings of the web server. Then your site will be available on the selected domain:

The configuration of the web server Nginx is a little different. Here files not supported .httaccess, so we need to include perenapravlenie all requests index.php in the main configuration file:

sudo vi /etc/nginx/sites-available/laravelapp.conf

server {
listen 80;
listen [::]:80;
server_name laravelapp.ru
root /var/www/sites/laravelapp/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}

Next you need to enable this site and restart nginx. Also note that configuring php with nginx I’m not here quote, but they also must be present.

Insights

In this small article we discussed how to install laravel in Ubuntu 18.04. This is a very powerful PHP framework, which creates many major projects. At the moment quite popular on the market and very interesting to use, as it implements many of the standard design patterns.

Source: losst.ru

(Visited 38 times, 1 visits today)