How to Install Laravel on Ubuntu

How to Install Laravel on Ubuntu

Before we start, you’ll need to SSH into your virtual private server. Here’s a helpful tutorial to help you along!

Following the steps below will walk you through the easiest way to install Laravel on Ubuntu:

1. Install Apache Web Server

For Laravel to work, you’ll need Apache. It is one of the most popular HTTP server tools, so it’s likely that your VPS has it installed. Luckily, you can check easily! Once you connect to your server using SSH, verify that an Apache system service exists. To do so, we have to run this command.

As you can see, on our VPS there is no Apache service, so we have to install it. To do this, execute the following command.

Ubuntu by default, starts the Apache service and makes it boot during system loading.

Now, if you’re using a firewall, it is necessary to establish a rule in the Ubuntu firewall so that Apache can run smoothly. If you have no firewall installed, feel free to skip this step.

After that, we can check the Apache service status again.

Apache server status command output on Ubuntu

Finally, open a web browser and we go to the IP address of your server or its domain name.

If you see this screen, that means Apache is up and running.

Apache default screen on a browser

2. Install PHP

The next step is to install PHP. Fortunately, PHP 7 comes by default in Ubuntu’s official repositories, which makes the installation very easy. You will need to install the language itself and some extra module. To do this, execute the following command:

If the following command produced an output saying some packages were not found, simply update your Ubuntu by running the following command, and rerun the previous one:

Now, we can test if PHP is working correctly. To do this, we need to create a file in Apache’s root directory. Let’s call it test.php. Run the following command:

And add the call to the phpinfo function.

We have to save it and close it. To save, hit CTRL+O, and to exit, hit CTRL+X Then, open the web browser and go to http://Your-serverIP/test.php.

If you see this screen, you can be sure that PHP is working as it should.

PHP info screen on a browser

3. Download and Install a Database Manager

If we are going to develop using Laravel in Ubuntu 18.04, for that it is necessary to install a database manager. Laravel supports PostgreSQL, MySQL, MariaDB, SQLite and SQL server. We can install and configure the one we want. For this tutorial we will install MariaDB.

After that, you can set a password for the root. To do this, you need to use the mysql_secure_installation script. Keep in mind that this step is optional, but recomended for security reasons.

After we define the root password, we will be asked several MariaDB configuration questions. The answers you should input are next to the lines of code:

Maria DB configuration on ubuntu

Congratulations, MariaDB was installed successfully.

If you prefer a NoSQL database management system, read our tutorial to learn about how to install MongoDB on Ubuntu.

4. Install Composer

Composer is a PHP dependency manager that facilitates the download of PHP libraries in our projects. Composer both works great with and makes it much easier to install Laravel.

First, we need to download Composer.

Next, we have to make sure Composer can be used globally and make it executable. The following commands will take care of that.

Suggested Reading

Check out our article to learn more how to change permissions and owners in Linux to control user access in your system.

5. Install Laravel on Ubuntu Using Composer

With Composer installed, now we can install Laravel. To do this, run the following command:

Of course, we have to replace [project_name] with the name of your application. In this case, we name the project example.

Suggested Reading

Check out our article to learn how to list installed packages on Ubuntu.

How to Use Laravel for Local Development

To develop applications locally, we can use PHP serve and specify the host and port of our server. To do this execute the commands following commands, and replace [IP] with your server IP, and [port] with the port you wish to use.

Next, open your web browser and go to the servers IP address or domain name and the specified port. The address would look like the one displayed in the output above. If you see the screen below in your browser, that means you’re ready to start working with Laravel.

Laravel landing Page on a browser

How to Use Laravel to Deploy an Application

On the contrary, if we are going to deploy a Laravel application on our VPS, then we have to make some adjustments to avoid problems.

First, we need to move the previously created project directory to the Apache web root. Remember, in our case, the folder name is Example. Execute the following command:

After that, set the necessary permissions to ensure the project runs smoothly:

It is necessary to create a new virtual host for the project. It can be done easily with the commands below:

Add the following to create the new Virtual host. Remember to replace thedomain.com with your server’s IP address.

Save the file and close it.

After that, disable the default configuration file of the virtual hosts in Apache with this command:

Afterwards, enable the new virtual host:

Enable the Apache rewrite module, and finally, restart the Apache service:

Now, open the web browser and go to the servers IP and voila. If you get the same Laravel landing screen you have seen last time, you’re ready to start working.

Now we can get to work with this great PHP framework.


Source: https://www.hostinger.com/tutorials/how-to-install-laravel-on-ubuntu#1_Install_Apache_Web_Server