Introduction
Laravel is a powerful MVC PHP framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. In this tutorial, we’ll walk you through installing and configuring Laravel on an Ubuntu system. We’ll cover everything from prerequisites to running a Laravel application.
Before getting started, make sure you have the following ready:
- An Ubuntu server
- Non-root user with sudo privileges
- PHP version 7.3 or higher, including necessary extensions
- Composer, the PHP dependency manager
- MySQL or another database system (optional)
The Steps
Step 1: Install PHP and Extensions
sudo apt update
sudo apt install php php-cli php-mbstring php-xml php-zip php-gd php-json php-pdo
Verify the installation of PHP and its version once all packages are installed:
php -v
Step 2: Install Composer
How to download and install Composer globally:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Verify the installation of Composer:
composer
Step 3: Install Laravel
Create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel myLaravelApp
Set the correct permissions for the project:
cd myLaravelApp
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
Step 4: Set Up a Web Server
Installing and configuring Nginx:
sudo apt install nginx
Create a new configuration file for your Laravel application.
sudo nano /etc/nginx/sites-available/myLaravelApp
Include the following server block information:
server {
listen 80;
server_name example.com;
root /path/to/your/myLaravelApp/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 ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/myLaravelApp /etc/nginx/sites-enabled/
Step 5: Set Up the Environment File
Edit the .env
file in your Laravel project to set up your database and other environment variables:
sudo nano .env
Adjust the following lines with your actual database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
If you’re using a different database or connection, make sure to install the corresponding PHP extension for it.
Step 6: Run Migrations
After configuring your database information, you can run migrations to set up default tables:
php artisan migrate
Step 7: Run the Laravel Application
Finally, restart your Nginx server to implement the changes:
sudo systemctl restart nginx
You should now be able to access your Laravel application in your web browser:
http://example.com
If you need to run your Laravel app in development without a full web server, you can use Artisan:
php artisan serve
This will start Laravel’s built-in development server at http://localhost:8000
.
Conclusion
Installing Laravel on Ubuntu is a straightforward process that sets the stage for development of high-quality PHP applications. With the power of Laravel at your fingertips, you can harness the capabilities of this sophisticated framework to build elegant and feature-rich web applications.