Laravel is a popular open source web framework built for PHP developer. Laravel follows the Model-View-Controller(MVC) architecture. Laravel is robust and easy to understand and it has elegant syntax that enables rapid development of websites.
In this tutorial we will guide you on how to setup Laravel on Ubuntu server using the LAMP stack.
Let us start with the setup
1. Updating the System
sudo apt update -y && apt upgrade -y
2. Installing PHP and PHP extensions
In this section we will install PHP and PHP extensions needed to run the Laravel Framework.
sudo apt-get install php libapache2-mod-php php-dev php-zip php-curl php-pear php-mbstring php-mysql php-gd php-xml curl -y
Verify if PHP is installed.
php -v
Output:
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
3. Installing Apache Web Server
To install the Apache web server run the following.
sudo apt install apache2
Let us start and enable Apache.
sudo systemctl start apache2
sudo systemctl enable apache2
4. Creating Database for our Laravel App
Since our environment is on a LAMP stack we will use MariaDB as a database system. But you can choose different database system as Laravel supports wide variety of database systems like SQLite, MySQL, Postgres, and SQL Server.
Let us install MariaDB.
sudo apt install mariadb-server
Login to your MariaDB server.
sudo mysql -u root -p
Create the database for our Laravel app.
MariaDB [(none)]> create database laravel;
MariaDB [(none)]> grant all privileges on laravel.* to 'laravel'@'localhost' identified by 'mypassword';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;
5. Installing Composer
Composer is a package manager for PHP that provides a standard format for managing dependencies of PHP software and required libraries. To install composer follow the steps below.
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Verify the installation.
composer --version
Output:
Composer version 2.2.3 2021-12-31 12:18:53
6. Installing the Laravel Framework
First let us create a project directory.
sudo mkdir /var/www/laravel
Let us change directory to our project directory.
cd /var/www/laravel
Install the Laravel framework using composer.
composer create-project laravel/laravel myapp --prefer-dist
Output:
Package manifest generated successfully.
77 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.
Check the Laravel version. First go to your myapp.
cd myapp
php artisan
Fix the permission of our project directory.
Set the owner to www-data(Apache user).
sudo chown -R www-data:www-data /var/www/laravel
Set all directories to 755 permission.
sudo find /var/www/laravel/ -type d -exec chmod 755 {} \;
Set all files to 644 permission.
sudo find /var/www/wordpress/ -type f -exec chmod 644 {} \;
7. Creating Virtual Host for our Laravel App
Let us create a virtual host.
nano /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/laravel/myapp/public
ServerName domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/laravel/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Note: Don’t forget to change the domain in the ServerName directive.
Save the file and exit.
Let us enable the laravel virtual host.
sudo a2ensite laravel.conf
Also enable the Apache mod_rewrite.
sudo a2enmod rewrite
Restart the Apache web server.
sudo systemctl restart apache2
8. Accessing your Laravel App
You have successfully configured your Laravel application. You can check it by accessing the domain set on your virtual host. Open your browser and type your domain e.g http://doamin.com.
If you want to secure your site and install SSL certificate you can check this article.
Conclusion
You have learned how to install Laravel on Ubuntu 20.04 with LAMP stack. If you like this article you might like also our other articles on this site.
If you have questions, feel free to leave a comment and we will try to answer it.
Thank you and hope you enjoy our tutorial ?