install laraveel on ubuntu 20.04 code example
Example: install laravel on ubuntu 20.04
1. Install Prerequisites
Install the software dependencies.
$ sudo apt install -y php-mbstring php-xml php-fpm php-zip php-common php-fpm php-cli unzip curl nginx
Install Composer:
$ sudo curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
Verify the Composer installation.
$ composer diagnose
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0 87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B 0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 1.10.10
PHP version: 7.4.3
PHP binary path: /usr/bin/php7.4
OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
Install MariaDB.
$ sudo apt install -y mariadb-client mariadb-server
Start MariaDB.
$ sudo systemctl enable --now mariadb.service
The root password is blank for a newly installed MariaDB server. Configure the MariaDB security by answering these questions appropriately for your server.
$ sudo mysql_secure_installation
2. Install Laravel
Replace example with your project name where it appears throughout this guide.
Create a Laravel project with Composer. You may disregard the warning not to run Composer as root for this step. See the Composer documentation for details.
$ cd /var/www/html
$ sudo composer global require laravel/installer
$ sudo composer create-project --prefer-dist laravel/laravel example
Grant your non-root user access.
$ sudo chmod -R 755 /var/www/html/example
$ sudo chown -R example_user:example_user /var/www/html/example
Install the example project.
$ cd example
$ composer install
Test the Laravel application manually in the /var/www/html/example folder. Replace the example IP address with your server's IP.
$ cd /var/www/html/example
$ php artisan serve --host=192.0.2.123 --port=8000
To access this example:
http://192.0.2.123:8000
Type CTRL+C in your SSH session to stop the Laravel application.
3. Configure Nginx
For public/production use, configure Nginx.
Set the file permissions. Replace example with your Laravel project name.
$ sudo chmod -R 755 /var/www/html/example
$ sudo chown -R www-data:www-data /var/www/html/example
Create an Nginx configuration file.
$ sudo nano /etc/nginx/sites-available/example
Paste the following to your example configuration file.
Replace server_name example.com; with your domain or server IP address. You can also use an underscore as a wildcard, for example: server_name _;
Replace /var/www/html/example/public with your project path.
If you installed a different version of PHP, edit the /var/run/php/php7.4-fpm.sock value for your version. You can find your sock file in /var/run/php/.
server {
listen 80;
server_name example.com;
root /var/www/html/example/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Enable the Nginx configuration.
$ sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
Remove the default configuration file.
$ sudo rm /etc/nginx/sites-enabled/default
Restart Nginx.
$ sudo systemctl restart nginx
Test that your Laravel application loads properly in a web browser.
http://example.com/
4. Configure MariaDB
Log in to MariaDB as root.
# mysql -p -u root
Create a database named laravelexample.
MariaDB [(none)]> CREATE DATABASE `laravelexample` CHARACTER SET utf8 COLLATE utf8_general_ci;
Create a database user.
MariaDB [(none)]> CREATE USER 'laravelexampleuser'@'%' IDENTIFIED BY 'password';
Grant permissions.
MariaDB [(none)]> use laravelexample;
MariaDB [laravelexample]> GRANT ALL ON `laravelexample.*` TO 'laravelexampleuser'@'%';
MariaDB [laravelexample]> FLUSH PRIVILEGES;
MariaDB [laravelexample]> EXIT;
Edit the Laravel application .env file.
$ sudo nano /var/www/html/example/.env
Set the database connection variables.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelexample
DB_USERNAME=laravelexampleuser
DB_PASSWORD=password
Save and exit the file.