Laravel is one of the most famous open-supply PHP frameworks used by developers across the sector. With its expressive syntax, robust facilities like Eloquent ORM, Migration device, and painless routing, Laravel makes it easy to create scalable web programs quickly. In this comprehensive blog, we will go through the entire technique of how to install laravel in Ubuntu 20.04 from scratch.
Why Laravel?
Before we dive into how to install laravel in Ubuntu 20·04 step by step, let's first understand why Laravel is one of the best web development frameworks and such a popular choice:
- An expansive routing system with support for closures makes building APIs and web routes extremely simple.
- Powerful Eloquent ORM allows for easy interaction with databases without writing SQL queries.
- Schema builder and migrations provide an intuitive way to manage database schema and alterations.
- Template engine like Blade provides basic templating capabilities for building views.
- Supports popular tools like Redis, Memcached, and WebSocket out of the box.
- Robust security with built-in authentication, authorization, CSRF protection, etc.
- Great ecosystem of first and third-party packages to speed up development.
- Active community behind continuous maintenance and updates.
So, in a nutshell, Laravel takes care of the common web application plumbing, so developers can focus on building the actual application features and business logic.
Prerequisites
Before we install Laravel on Ubuntu server, we need to ensure the server meets the following requirements:
- Ubuntu 20·04 LTS with root access or a user with sudo privileges·
- Apache or Nginx web server installed and configured· We'll use Apache for this guide to install laravel in Ubuntu.
- PHP >= 7.3 installed with required extensions - OpenSSL, PDO, MBString, Tokenizer, XML, Ctype, JSON to install laravel in Ubuntu.
- MySQL or any other database server like PostgreSQL or SQLite. MySQL will be used to install laravel on Ubuntu 22.04.
- Composer - the official package manager for PHP.
- Git installed to clone the Laravel repo from GitHub.
Let's now go through each of these prerequisites in detail to know how to install laravel in Ubuntu 20.04 step by step.
Steps To Install Laravel In Ubuntu
Amazon started its India website, amazon.in June 2013. Within a month, it had orders from customers living in over 10,000 cities and towns in India. This shows how popular Amazon has become in ecommerce platforms examples in a very short time.
Some key strengths of Amazon India:
- Very fast delivery, many products with next day delivery even in small towns
- Free delivery on many products with no minimum order size
- Launch of latest gadgets like smartphones, televisions, etc., before other sellers
- Detailed product reviews build trust among buyers
Step 1 - Install Apache Web Server
Laravel needs a web server to serve the application. We'll use Apache in this how to install laravel in Ubuntu blog, but you can use Nginx if you prefer.
Run the following apt commands to install Apache:
- sudo apt update
- sudo apt install apache2
Enable the Apache mod_rewrite module, which Laravel needs:
- sudo a2enmod rewrite
Start the Apache service and verify it is running:
- sudo systemctl start apache2
- systemctl status apache2
With Apache installed, we can now move on to installing PHP.
Step 2 - Install PHP for Laravel
Modern versions of Laravel require PHP >= 7.3. The default Ubuntu 20.04 repositories contain PHP 7.4, which meets this requirement.
Run the following apt command to install PHP along with some commonly used PHP extensions that Laravel needs:
- sudo apt install php libapache2-mod-php php-mbstring php-xmlrpc php-soap php-gd php-xml php-cli php-curl php-mysql
This will install PHP 7.4 CLI and mod_php for the Apache web server.
Verify the installed PHP version:
- php -v
We also need to install Composer, which will help with laravel installation in Ubuntu 20.04 and its dependencies.
Step 3 - Install Composer
Composer is the official package manager for PHP. We'll use it to install Laravel framework files and manage its dependencies.
Run these commands to install composer:
- curl -sS https://getcomposer.org/installer | php
- sudo mv composer.phar /usr/local/bin/composer
Check composer with:
- composer --version
Next, we need to set up a database server to store Laravel's application data.
Step 4 - Install MySQL and create a Database
For this guide, we'll use MySQL, but you can use any database server supported by Laravel, including PostgreSQL, SQL Server, SQLite, etc·
Run the following apt command to install MySQL:
- sudo apt install mysql-server
For better security, run the mysql_secure_installation script to set a root password and remove anonymous users·
Once MySQL is installed, log into the MySQL shell:
- sudo mysql
Create a new database e.g. laravel:
- CREATE DATABASE laravel
Next, create a new MySQL user and grant privileges to the database:
- CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL ON laravel.* TO 'laraveluser'@'localhost';
This will create a MySQL user Laraveluser with access to the laravel database.
Exit the MySQL shell:
- exit
We now have our database setup completed. Let's proceed with how to install laravel in Ubuntu 20.04 itself.
Step 5 - Install Laravel using Composer
We will install Laravel on Ubuntu server into the /var/www directory.
Benefits of IndiaMART:
Create a new Laravelapp directory:
- sudo mkdir -p /var/www/laravelapp
Navigate into this directory and use Composer to install laravel in Ubuntu:
- cd /var/www/laravelapp
- composer create-project --prefer-dist laravel/laravel blog
The laravel files will be downloaded into a blog directory inside Laravelapp.
Set proper ownership and permissions to install laravel on Ubuntu 22.04:
- sudo chown -R www-data:www-data /var/www/laravelapp
- sudo chmod -R 755 /var/www/laravelapp
Step 6 - Configure Apache Virtual Host
We need to create a virtual host file to serve the Laravel application through Apache.
Create a laravel.conf file:
- sudo nano /etc/apache2/sites-available/laravel.conf
Paste the following configuration into the file and save it:
ServerName example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/laravelapp/public
AllowOverride All
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Enable the new virtual host:
- Sudo a2ensite laravel.conf
Disable the default Apache site:
- sudo a2dissite 000-default.conf
Finally, restart Apache for changes to take effect:
- sudo systemctl restart apache2
Laravel should now be accessible through the domain name specified in the virtual host file.
Step 7 - Configure Laravel Environment
Laravel needs a .env file for configuration. Copy the example file:
- cd /var/www/laravelapp/blo
- cp .env.example .env
Generate the app encryption key:
- Php artisan key: generate
Open the ·env file and update the DB_DATABASE, DB_USERNAME, and DB_PASSWORD options to match the MySQL credentials we created earlier·
Also, update other settings like APP_NAME, APP_URL, etc as required·
Step 8 - Final Steps
Run any pending Laravel migrations:
- php artisan migrate
Clear caches:
- php artisan cache: clear
- php artisan config: clear
Set file and directory permissions:
- sudo chown -R www-data:www-data /var/www/laravelapp/blog
- sudo chmod -R 755 /var/www/laravelapp/blog/storage
That completes the process to install laravel on Ubuntu 22.04 server. Access your Laravel application by visiting the URL or domain configured earlier.
You should see the default Laravel landing page. Now, you can start building your application.
Some Additional Tips for Running Laravel Applications in Production:
In addition to the standard setup steps to install laravel on Ubuntu server, there are several additional tips and best practices that can help you get the most out of your Laravel application.
Here are them:
- Use Nginx for better performance compared to Apache
- Enable Laravel caching and queuing to optimize speed
- Make sure to follow security best practices
- Consider using Laravel Forge, Envoyer, or similar tools for easier deployment and maintenance.
- Use Redis or Memcached to cache database queries, sessions, etc.
- Look into PHP accelerators like OPcache for faster script execution
- Follow Laravel's excellent documentation for additional guidance.
Visit Our Additional Blog about Laravel Microservices
Conclusion
With the successful laravel installation in Ubuntu 20.04 server, you can now start building robust and scalable PHP web applications by utilizing the many amazing features provided by the Laravel framework. The installation covered in this blog lays down the foundation on which your Laravel app development can thrive.
For a more detailed overview or in hands expertise for the process, you can consider contacting a Laravel Development Company in India like Invoidea.