Laravel is a robust, feature-rich PHP framework used for creating high-quality web applications. It simplifies tasks like routing, authentication, caching, and sessions, making it a popular choice for developers worldwide. If you are using Ubuntu 22.04 and planning to set up Laravel, this comprehensive guide is all you need.
Before we start, make sure that you have administrative access to an Ubuntu 22.04 system.
Step 1: System Update
Before installing any new packages, it’s recommended to update your Ubuntu system. Open a terminal window and type:
sudo apt update
sudo apt upgrade
Step 2: Install PHP
Laravel requires PHP to run. You can install PHP along with necessary extensions by typing:
sudo apt install php php-xml php-gd php-zip php-mbstring php-curl php-tokenizer php-json
Your can install specific PHP version using the article: How To Install PHP (8.3, 8.2, 7.4) on Ubuntu 22.04
Step 3: Install PHP Composer
Composer is a dependency management tool for PHP. It will help you download and install Laravel in your system. You can install Composer with the following commands:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Step 4: Create a New Laravel Project
Navigate to the directory where you want your new Laravel project to reside, then run:
composer create-project laravel/laravel my_project
Replace “my_project” with the name you wish to give your Laravel project.
This command will create a new directory with the specified name, download a fresh Laravel installation into that directory, and install all of Laravel’s dependencies.
Step 5: Start Laravel’s Local Development Server
Move into the Laravel project directory and use the Artisan command-line utility included with Laravel to start the Laravel server:
cd my_project
php artisan serve
You’ll see Laravel development server started on http://localhost:8000. Open your browser and visit this URL to see your new Laravel application in action.
Step 6: Configure the .env File
The last step in setting up Laravel is configuring the environment file (.env). This file contains settings like database credentials, mail server credentials, and other sensitive settings. You can edit this file to match your specific settings.
Step 7: Deploy Laravel with Apache
Apache is a popular web server and a good choice for hosting Laravel applications. After installing Laravel and Apache on your Ubuntu 22.04 system, you’ll need to configure Apache to serve your Laravel application. Follow these steps:
- Install Apache: If you haven’t already installed Apache, do so with the following commands:
sudo apt update
sudo apt install apache2
- Enable mod_rewrite: Laravel requires mod_rewrite to handle URL rewriting. You can enable it with the following command:
sudo a2enmod rewrite
- Configure a Virtual Host: Apache uses virtual host files to contain server configuration information for individual websites. Create a new configuration file for your Laravel application:
sudo nano /etc/apache2/sites-available/my_project.conf
Replace “my_project” with your Laravel project’s name.
Add the following configuration to the file:
<VirtualHost *:80> ServerName my_project.com ServerAlias www.my_project.com DocumentRoot /var/www/my_project/public <Directory /var/www/my_project> AllowOverride All Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/my_project_error.log CustomLog ${APACHE_LOG_DIR}/my_project_access.log combined </VirtualHost>
Replace my_project.com with your domain or subdomain and /var/www/my_project with the path to your Laravel project. The DocumentRoot should point to the public directory of your Laravel application.
Press CTRL+X followed by Y and ENTER to save and close the file.
- Enable the Virtual Host: You can enable your new virtual host using the a2ensite command:
sudo a2ensite my_project.conf
- Restart Apache: Finally, to apply all the changes, restart the Apache server:
sudo systemctl restart apache2
Your Laravel application should now be accessible from your web browser via the domain or subdomain you specified in your virtual host configuration. Remember to replace “my_project.com” with your actual domain or IP address. If you’re developing locally, this may be localhost or a local IP.
Note: If you’re deploying your application to production server, you need to have a domain pointing to your server’s IP address. Also, always ensure that your server’s firewall rules allow traffic on HTTP and HTTPS.
Conclusion
This step-by-step guide aimed to help you understand how to install Laravel on Ubuntu 22.04. The installation process can seem daunting, but with these steps, you should have been able to navigate it easily. Enjoy coding with Laravel!
Note: This tutorial assumes you have a basic knowledge of the Linux command line interface. If you’re new to it, you might want to familiarize yourself with basic Linux commands and how to use the terminal.
Remember, the best way to learn is by doing. Now that you have Laravel set up on your Ubuntu system, it’s time to start building!