<!DOCTYPE html>
Laravel Installs on Ubuntu: A Step-by-Step Guide
<br> body {<br> font-family: sans-serif;<br> line-height: 1.6;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { margin-top: 2em; } pre { background-color: #f0f0f0; padding: 1em; overflow-x: auto; } img { max-width: 100%; display: block; margin: 1em auto; } </code></pre></div> <p>
Laravel Installs on Ubuntu: A Step-by-Step Guide
Laravel is a powerful PHP framework renowned for its elegance, speed, and robust features, making it a favorite among web developers. Ubuntu, a popular Linux distribution, provides a stable and efficient environment for building web applications. This comprehensive guide will walk you through installing Laravel on Ubuntu, step-by-step, ensuring a smooth and successful setup.
Prerequisites
Before we begin, ensure you have the following prerequisites:
- Ubuntu Server: We recommend a fresh Ubuntu Server installation.
- Terminal Access: You'll need access to the Ubuntu server's command-line interface (terminal).
- Basic Linux Knowledge: Familiarity with basic Linux commands is helpful, but not essential.
- Composer: A dependency manager for PHP projects.
-
MySQL or PostgreSQL: A database system for storing application data.
Step 1: Update the System
It's essential to keep your system up-to-date. Open a terminal and run the following commands:
sudo apt update sudo apt upgrade -y
This ensures you have the latest system packages and security patches.
Step 2: Install Composer
Composer is a vital tool for managing PHP dependencies. We'll use it to install Laravel and its required packages. Here's how to install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php php -r "unlink('composer-setup.php');" sudo mv composer.phar /usr/local/bin/composer
This script downloads the Composer installer, runs it, then places Composer in your system's binary directory for easy access.
Step 3: Install PHP and Related Packages
Laravel requires specific PHP extensions. Install these using the following command:
sudo apt install php-fpm php-mysql php-mbstring php-curl php-xml php-gd php-zip
This command installs the necessary PHP extensions, including MySQL support, MBString for multi-byte string handling, and others. For PostgreSQL, replace `php-mysql` with `php-pgsql`.
Step 4: Install a Database System
Choose your preferred database system. For MySQL, use the following:
sudo apt install mysql-server
For PostgreSQL, use:
sudo apt install postgresql postgresql-contrib
Follow the prompts to set a root password for MySQL or PostgreSQL, if required.
Step 5: Configure the Web Server
Laravel typically runs on Nginx or Apache. Here's how to configure Nginx:
sudo apt install nginx
Create a new Nginx server block for your Laravel application. Open a file named `laravel.conf` in the `/etc/nginx/sites-available` directory:
sudo nano /etc/nginx/sites-available/laravel.conf
Paste the following configuration into the file, replacing `example.com` with your domain name:
server { listen 80; server_name example.com www.example.com; root /var/www/laravel/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
Enable the server block:
sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/
Test your Nginx configuration:
sudo nginx -t
Restart Nginx:
sudo systemctl restart nginx
If using Apache, install it with:
sudo apt install apache2
Then enable the mod_rewrite module:
sudo a2enmod rewrite
Create a virtual host file for your Laravel application:
sudo nano /etc/apache2/sites-available/laravel.conf
Paste the following into the file:
ServerName example.com DocumentRoot /var/www/laravel/public AllowOverride All Require all granted RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php?/$1 [L]
Enable the virtual host and restart Apache:
sudo a2ensite laravel.conf sudo systemctl restart apache2
Step 6: Create the Laravel Project
Navigate to the directory where you want to store your Laravel project using the `cd` command. Then, use Composer to create a new Laravel project:
composer create-project laravel/laravel my-laravel-project
Replace `my-laravel-project` with your desired project name. This will download the Laravel framework and its dependencies into the specified directory.
Step 7: Configure Database Credentials
Open the `.env` file in your project directory:
nano .env
Set the database credentials according to your chosen database system. For MySQL:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
For PostgreSQL:
DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
Remember to replace the placeholders with your actual database credentials.
Step 8: Generate an Application Key
Laravel uses a secret key to protect your application. Generate a new key using this command:
php artisan key:generate
Step 9: Migrate the Database
Create the database tables for your application using the following command:
php artisan migrate
This will execute the database migrations defined in the `database/migrations` directory, creating the necessary tables.
Step 10: Access Your Laravel Application
Open your web browser and navigate to the domain you configured earlier. You should see the Laravel welcome page, indicating a successful installation.
Step 11: Start Developing!
Congratulations! Your Laravel application is ready. You can now start building your web application using the various features and components provided by the Laravel framework. You'll find a wealth of resources and documentation available online to help you get started and build amazing web applications.
Conclusion
This guide has demonstrated a comprehensive and step-by-step approach to installing Laravel on Ubuntu. Remember that the specific configuration details may vary slightly based on your specific setup. This guide will equip you with the necessary knowledge and tools to successfully set up Laravel on Ubuntu, enabling you to build robust and scalable web applications.