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.

4.22
9
Pietross 80 points

                                    ssh root@IP_ADDRESS -p PORT_NUMBER
apt-get update -y && apt-get upgrade -y
apt-get install apache2 php7.4 libapache2-mod-php php-curl php-pear php-gd php-dev php-zip php-mbstring php-mysql php-xml curl -y
systemctl start apache2
systemctl enable apache2

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

cd /var/www/html
composer create-project laravel/laravel laravelapp --prefer-dist

cd laravelapp
php artisan

chown -R www-data:www-data /var/www/html/laravelapp
chmod -R 775 /var/www/html/laravelapp/storage

# add the following text to /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerName laravel.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# then, run 
a2ensite laravel.conf
a2enmod rewrite

systemctl restart apache2

# now, open http://laravel.example.com!

4.22 (9 Votes)
0
4.22
9
GregDoesMath 110 points

                                    ssh root@IP_ADDRESS -p PORT_NUMBER
apt-get update -y && apt-get upgrade -y
apt-get install apache2 php7.4 libapache2-mod-php php-curl php-pear php-gd php-dev php-zip php-mbstring php-mysql php-xml curl -y
systemctl start apache2
systemctl enable apache2

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

cd /var/www/html
composer create-project laravel/laravel laravelapp --prefer-dist

cd laravelapp
php artisan

chown -R www-data:www-data /var/www/html/laravelapp
chmod -R 775 /var/www/html/laravelapp/storage

# add the following text to /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerName laravel.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# then, run 
a2ensite laravel.conf
a2enmod rewrite

systemctl restart apache2

# now, open http://laravel.example.com!

4.22 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source