How to Install Laravel on Ubuntu Linux with Apache

|

|

This post instructs on how to install Laravel, an open-source PHP web application framework, on the Ubuntu Linux system using Apache. It covers the installation process of Apache and PHP with related modules. Additionally, it details the use of Composer for downloading Laravel, how to configure Apache for Laravel support, and the setup of Laravel…

This post shows students and new users how to install the Laravel framework on Ubuntu Linux with Apache. Laravel is an open-source PHP web application framework with cool built-in features such as routing, authentication, sessions, caching, and unit testing.

Laravel can be used as an alternative to the CodeIgniter framework. It is designed for ease of use to allow developers to create great applications. If you’re looking for a simple PHP framework to design your next application, you’ll find Laravel useful.

Also, for students and new users learning Linux, Ubuntu Linux is the easiest place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

To get started with installing Laravel on Ubuntu Linux, follow the steps below:

How to install Apache on Ubuntu Linux

A web server is unnecessary if you’re not using Laravel on a production server. If you want to run Laravel with a web server, then Apache is a great option since it’s the most popular web server used today.

To install Apache on Ubuntu, run the commands below.

sudo apt update
sudo apt install apache2

After installing, the commands below can be used to stop, start and enable the Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

How to install PHP on Ubuntu Linux

Laravel is based on PHP, so you must install PHP and related modules. At the time of this writing, PHP 7.4 is the default in Ubuntu repositories.

To install PHP and related modules, run the commands below

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-intl php7.4-imap php7.4-mbstring php7.4-xmlrpc php7.4-soap php7.4-gd php7.4-xml php7.4-cli php7.4-zip curl

PHP should be installed and ready to use.

How to install Composer to download Laravel

Now that PHP is installed, you can download Laravel via Composer. Composer is a dependency manager for PHP, which we will use to download Laravel core and related Laravel components.

To do that, run the commands below to install Composer.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

After installing Composer, change to the Apache root directory and run Composer to download Laravel code packages and create a Laravel project folder called MyProject.

Laravel will automatically create a file named .env. This file includes custom configuration variables, including the database credentials. If you’re using a database server, use this file to add its name and password.

cd /var/www/
sudo composer create-project laravel/laravel MyProject --prefer-dist

How to configure Apache to support Laravel

Finally, configure the Apahce2 site configuration file for Laravel. This file will control how users access Laravel content. Run the commands below to create a new configuration file called laravel.conf.

sudo nano /etc/apache2/sites-available/laravel.conf

Then copy and paste the content below into the file and save it. Replace the highlighted line with your domain name and directory root location.

<VirtualHost *:80>   
  ServerAdmin admin@example.com
     DocumentRoot /var/www/MyProject/public
     ServerName example.com

     <Directory /var/www/MyProject/public>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the file and exit.

How to enable Laravel Apache vhost

After configuring the VirtualHost above, please enable it by running the commands below.

sudo a2ensite laravel.conf

After running the commands above, a new project directory will be created. Run the commands below to set the correct permissions for that directory.

sudo chown -R www-data:www-data /var/www/MyProject/
sudo chmod -R 755 /var/www/MyProject/

Next, run the command below to restart Apache by running the commands below.

sudo systemctl restart apache2.service

Finally, open your browser and browse to the domain name added in the configuration file above.

http://example.com

You should then see Laravel’s default home screen. You can begin building your web apps via Laravel on Ubuntu.

That should do it!

Conclusion:

This post showed you how to install the Laravel framework on Ubuntu Linux with Apache. Please use the comment form below if you find any errors above or have something to add.


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading