Learn how to install Laravel on Ubuntu Server with this easy step-by-step guide with images. This is a popular PHP framework for developing web applications.
To install Laravel in Ubuntu Server, you will need to have the following prerequisites installed:
Step 1: Install PHP and Composer
To install PHP, run the following command:
sudo apt update && sudo apt install php-fpm php-cli
To install Composer, run the following command:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Step 2: Install Apache2 or Nginx
If you are using Apache2, run the following command:
sudo apt install apache2
If you are using Nginx, run the following command:
sudo apt install nginx
Step 3: Create a Laravel Project
To create a new Laravel project, run the following command:
composer create-project laravel/laravel my-laravel-project
This will create a new Laravel project in the my-laravel-project
directory.
Step 4: Configure Apache2 or Nginx
If you are using Apache2, you will need to configure the Apache virtual host file to serve the Laravel application. To do this, open the Apache virtual host file in a text editor:
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following lines to the file:
<VirtualHost *:80>
DocumentRoot /var/www/html/my-laravel-project/public
ServerName my-laravel-project.com
<Directory /var/www/html/my-laravel-project/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Save the file and exit the text editor.
To enable the Apache virtual host file, run the following command:
sudo a2ensite 000-default.conf
To restart the Apache service, run the following command:
sudo systemctl restart apache2
If you are using Nginx, you will need to configure the Nginx configuration file to serve the Laravel application. To do this, open the Nginx configuration file in a text editor:
sudo nano /etc/nginx/sites-available/default
Add the following lines to the file:
server {
listen 80;
server_name my-laravel-project.com;
root /var/www/html/my-laravel-project/public;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
Save the file and exit the text editor.
To enable the Nginx configuration file, run the following command:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
To restart the Nginx service, run the following command:
sudo systemctl restart nginx
Step 5: Migrate the Laravel Database
To migrate the Laravel database, run the following command:
php artisan migrate
Step 6: Seed the Laravel Database
To seed the Laravel database, run the following command:
php artisan db:seed
Step 7: Start the Laravel Development Server
To start the Laravel development server, run the following command:
php artisan serve
This will start the Laravel development server on port 8000. You can now access the Laravel application in your web browser at http://localhost:8000.
Conclusion
You have now successfully installed Laravel on Ubuntu Server. You can now start developing your Laravel application.