Laravel is a popular open-source PHP framework that is designed for building web applications. In this article, we will explain how to install Laravel on an Ubuntu 22.04 server that is running Apache 2.
Before you begin, make sure that you have Apache 2 and PHP 7.4 installed on your Ubuntu server. You can check if Apache is installed by running the following command:
sudo apache2 --version
If Apache is not installed, you can install it using the following command:
sudo apt-get install apache2
Once Apache is installed, you can check if PHP is installed by running the following command:
php --version
If PHP is not installed, you can install it using the following command:
sudo apt-get install php7.4
Once Apache and PHP are installed, you need to install some additional PHP modules that are required by Laravel. You can do this by running the following command:
sudo apt-get install php7.4-common php7.4-cli php7.4-mbstring php7.4-xml php7.4-zip
This will install the necessary PHP modules, including the php7.4-common
module, which provides common functions for PHP, the php7.4-cli
module, which provides the PHP command-line interface, the php7.4-mbstring
module, which provides support for multi-byte strings, the php7.4-xml
module, which provides support for XML manipulation, and the php7.4-zip
module, which provides support for reading and writing ZIP archives.
Once the necessary PHP modules are installed, you need to download and install Composer, which is a dependency manager for PHP. You can do this by running the following commands:
php -r "copy('<https://getcomposer.org/installer>', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
These commands will download the Composer installer and run it, which will install Composer on your system. Once Composer is installed, you can use it to install Laravel by running the following command:
php composer.phar create-project --prefer-dist laravel/laravel laravel
This will create a new Laravel project in the laravel
directory. You can then configure Apache to serve the Laravel project by creating a new virtual host configuration file. You can do this by using a text editor to create a new file in the /etc/apache2/sites-available
directory. For example, if you want to create a virtual host configuration file for the laravel.example.com
domain, you can create a laravel.example.com.conf
file with the following content:
<VirtualHost *:80>
ServerName laravel.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This virtual host configuration file defines the server name and document root for the laravel.example.com
domain, and specifies the directory where Laravel’s public files are located. It also sets the AllowOverride
directive to All
, which allows Laravel to use its own routing rules and configuration settings.
Once you have created the virtual host configuration file, you need to enable it and disable the default virtual host configuration file by running the following commands:
sudo a2ensite laravel.example.com.conf
sudo a2dissite 000-default.conf
These commands will enable the laravel.example.com.conf
virtual host configuration file, and disable the default 000-default.conf
virtual host configuration file. Then, you can restart Apache to apply the changes by running the following command:
sudo systemctl restart apache2
Once Apache is restarted, you can visit the laravel.example.com
domain in your web browser to access your Laravel application. You should see the Laravel welcome page, which indicates that Laravel is installed and working correctly.
In conclusion, installing Laravel on an Ubuntu 22.04 server that is running Apache 2 is a straightforward process. By following the steps outlined in this article, you can quickly and easily set up a Laravel application and start developing your web applications with Laravel.