
Laravel is a popular PHP framework known for its elegant syntax and powerful features. In this tutorial, we will guide you through the process of installing Laravel on Ubuntu 18.04/20.04/22.04 and Debian, using both Apache and Nginx as web servers. We will provide step-by-step instructions to ensure a smooth installation. Let’s get started!
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- Ubuntu 20.04/22.04 server with a non-root user account with sudo privileges.
- Apache or Nginx web server installed and properly configured on your Ubuntu server.
- PHP installed on your Ubuntu server.
- Composer installed on your Ubuntu server.
Step 1: Update System Packages
First, let’s update the system packages to ensure we have the latest versions. Open a terminal and run the following command:
$ sudo apt update
$ sudo apt upgrade
Step 2: Install PHP and Required Extensions
Laravel requires PHP along with some specific extensions. We will install PHP and the required extensions using the following commands:
$ sudo apt install php php-cli php-common php-mbstring php-xml php-zip php-mysql php-pgsql php-sqlite3 php-json php-bcmath php-gd php-tokenizer php-xmlwriter
Once the installation is complete, verify the PHP version by running the command:
$ php -v
Step 3: Install Composer
Composer is a dependency management tool for PHP. We will use Composer to install Laravel and its dependencies. Run the following commands to install Composer globally:
$ sudo apt install curl php-cli php-mbstring git unzip
$ curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
To verify the installation, run the following command:
$ composer --version
Step 4: Install Laravel
Now that we have Composer installed, we can use it to install Laravel. Open a terminal and navigate to the document root of your web server. For Apache, the document root is typically located at /var/www/html
, and for Nginx, it is usually at /var/www
.
To install Laravel, run the following command:
$ composer create-project --prefer-dist laravel/laravel your-project-name
Replace your-project-name
with the desired name for your Laravel project. Composer will fetch the Laravel framework and its dependencies, which may take a few minutes.
Once the installation is complete, navigate to the project directory:
$ cd your-project-name
Step 5: Configure Apache
If you are using Apache as your web server, follow these steps to configure it for Laravel.
Create a new Apache configuration file for your Laravel project:
$ sudo nano /etc/apache2/sites-available/your-project-name.conf
Replace your-project-name
with the actual name of your project.
Add the following content to the configuration file:
<VirtualHost *:80>
ServerName your-domain-or-ip
DocumentRoot /var/www/html/your-project-name/public
<Directory /var/www/html/your-project-name>
AllowOverride All
</Directory>
</VirtualHost>
Replace your-domain-or-ip
with your actual domain name or server IP address.
Enable the Apache rewrite module:
$ sudo a2enmod rewrite
Enable the virtual host:
$ sudo a2ensite your-project-name.conf
Restart Apache for the changes to take effect:
$ sudo systemctl restart apache2
Step 6: Configure Nginx
If you are using Nginx as your web server, follow these steps to configure it for Laravel.
Create a new Nginx server block for your Laravel project:
$ sudo nano /etc/nginx/sites-available/your-project-name
Replace your-project-name
with the actual name of your project.
Add the following content to the server block:
server {
listen 80;
server_name your-domain-or-ip;
root /var/www/html/your-project-name/public;
index index.php;
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;
}
location ~ /\.ht {
deny all;
}
}
Replace your-domain-or-ip
with your actual domain name or server IP address.
Enable the Nginx server block:
$ sudo ln -s /etc/nginx/sites-available/your-project-name /etc/nginx/sites-enabled/
Test the Nginx configuration for any syntax errors:
$ sudo nginx -t
Restart Nginx for the changes to take effect:
$ sudo systemctl restart nginx
Step 7: Configure Laravel
Now that your web server is configured, let’s set up Laravel.
Copy the example .env
file:
$ cp .env.example .env
Generate a new application key:
$ php artisan key:generate
Set the appropriate permissions on Laravel directories:
$ sudo chown -R www-data:www-data /var/www/html/your-project-name/storage
$ sudo chmod -R 775 /var/www/html/your-project-name/storage
You’re ready to use Laravel! Access your Laravel application in a web browser by visiting your domain name or server IP address.
Important: The.env
file in Laravel contains sensitive configuration information, including database credentials and API keys. Securing this file is crucial to protect your application from unauthorized access and security breaches. To enhance security, you can move the.env
file outside the document root, restrict file permissions, disable directory browsing, encrypt sensitive information, and avoid storing production credentials in version control. Implementing these measures helps protect your application's sensitive data and maintain its security. Regularly reviewing and updating security practices is important to stay proactive against potential threats.
Conclusion
Congratulations! You have successfully installed Laravel on Ubuntu 20.04/22.04 using both Apache and Nginx as web servers. Now you can start building powerful web applications using the Laravel framework. Enjoy coding with Laravel!
2 thoughts on - Install Laravel on Ubuntu with Apache or Nginx
Please add this “php artisan serve” command also , after php artisan key .
To start a local development server using PHP Artisan for a Laravel project, you can specify the host and port using the php artisan serve command. This is useful when you want to run the server on a different IP address or port other than the default 127.0.0.1:8000.
Hi,
this tutorial is for deploying a production server , you don’t need to run
php artisan serve
.Best regards.