6

How to Install Laravel 7 on Ubuntu 20.04 with Nginx and MariaDB

 2 years ago
source link: https://www.vultr.com/docs/how-to-install-laravel-7-on-ubuntu-20-04-with-nginx-and-mariadb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
<?xml encoding="utf-8" ??>

Introduction

Laravel is a framework for PHP to create web applications. This guide describes how to install Laravel on a freshly-deployed Ubuntu 20.04 LTS server instance.

Requirements

1. Install Prerequisites

  1. 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
    
  2. Install Composer:

    $ sudo curl -s https://getcomposer.org/installer | php
    $ sudo mv composer.phar /usr/local/bin/composer
    
  3. 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
    
  4. Install MariaDB.

    $ sudo apt install -y mariadb-client mariadb-server
    
  5. Start MariaDB.

    $ sudo systemctl enable --now mariadb.service
    
  6. 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.

  1. 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
    
  2. 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
    
  3. Install the example project.

    $ cd example
    $ composer install
    
  4. 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.

  1. 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
    
  2. Create an Nginx configuration file.

    $ sudo nano /etc/nginx/sites-available/example
    
  3. 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;
        }
    }
    
  4. Enable the Nginx configuration.

    $ sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
    
  5. Remove the default configuration file.

    $ sudo rm /etc/nginx/sites-enabled/default
    
  6. Restart Nginx.

    $ sudo systemctl restart nginx
    
  7. Test that your Laravel application loads properly in a web browser.

    http://example.com/
    

4. Configure MariaDB

  1. Log in to MariaDB as root.

    # mysql -p -u root
    
  2. Create a database named laravelexample.

    MariaDB [(none)]> CREATE DATABASE `laravelexample` CHARACTER SET utf8 COLLATE utf8_general_ci;
    
  3. Create a database user.

    MariaDB [(none)]> CREATE USER 'laravelexampleuser'@'%' IDENTIFIED BY 'password';
    
  4. Grant permissions.

    MariaDB [(none)]> use laravelexample;
    MariaDB [laravelexample]> GRANT ALL ON `laravelexample.*` TO 'laravelexampleuser'@'%';
    MariaDB [laravelexample]> FLUSH PRIVILEGES;
    MariaDB [laravelexample]> EXIT;
    
  5. 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
    
  6. Save and exit the file.

Conclusion

You've set up a template Laravel application. See the official documentation for further information.

Want to contribute?

You could earn up to $600 by adding new articles


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK