Install Laravel with Nginx and MariaDB on Ubuntu 18.04


Ubuntu 18.04 Bionic Beaver


A working install of Ubuntu 18.04 with root privileges




  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user


Laravel as quickly earned its place as the top PHP framework. It brought a lot of developer friendly features found in other frameworks like Rails to the PHP ecosystem, and in doing so, modernized PHP development.

If you're looking to develop with Laravel or host it on Ubuntu 18.04, the setup process has never been easier. Since Laravel is PHP, there are a couple of ways to handle this, but Nginx and MariaDB provide a fairly straightforward way to get your project running with modern production-ready tools.

Install the Packages

Before you can start setting everything up, there are a few packages that you'll need. They're all available in Ubuntu's repositories, and they're all fairly current in Bionic, so there's no reason to look elsewhere.
$ sudo apt install composer php-mysql php-fpm php-mbstring php-tokenizer php-xml php-json php-common nginx mariadb-server
They'll pull in more, but it won't be too bad.

Create Your Lavavel Project

The next thing that you'll need to do is start your Laravel project. This isn't too big of a task either. The Composer package that you installed earlier handles just about everything for you, and will give you a functional Laravel install out of the box.

Start off by changing directory into your /var/www/ directory. Since this is going to be served with Nginx, that's the best place to organize your sites.
$ cd /var/www
Next, use Composer to create your project. The name at the end is the name of your project and the folder containing it. Choose it accordingly.
$ sudo composer create-project --prefer-dist laravel/laravel yourProject
Install Laravel With Composer On Ubuntu 18.04
Install Laravel With Composer On Ubuntu 18.04
Composer will automatically download and install all of the PHP dependencies along with Laravel and set them up. Before you can get started working with and serving your project, change the ownership for Nginx.
$ sudo chown -R www-data:www-data yourProject

Set Up Your Database

Next, you're going to need to set up your database. Before you start adding anything in, secure your server.
$ sudo mysql-secure-install
Go through, set up your root password, and use their secure defaults. Now, you can sign in to MariaDB, and set up your database for Laravel to use.
$ sudo mysql -u root -p
Once you're in, start by creating your database.
Then, create a user to use that database.
> CREATE USER `user`@`localhost` IDENTIFIED BY 'yourpassword';
Give that user permission to use that database.
> GRANT ALL ON laravel.* TO `user`@`localhost`;
Finally, flush the privileges and exit.

Connect Laravel

By default, Laravel is configured to use MySQL(MariaDB), but you need to give it the right information to connect to the database that you just set up.
Configure Laravel Database On Ubuntu 18.04
Configure Laravel Database On Ubuntu 18.04
Go ahead and change into the directory where you installed Laravel. You'll see a config folder there. Inside that folder is a database configuration file. Open config/database.php. Scroll downd to find the MySQL block like the one pictured above. Change the database name, username, and password to match the ones you set up. Then, save and exit.

Configure Nginx

The final piece of the puzzle is Nginx. That's the part that'll actually server your site. Change into /etc/nginx. In that folder, you'll find all of the configuration for Nginx. You don't need to mess with any of it. You can tweak the options in nginx.conf if you choose, but it isn't necessary to get Laravel working.

There are two folders that you need to pay attention to. They're sites-availabe and sites-enabled. sites-available contains all of your site configurations. To begin serving a site, create a soft link to the site configuration in site-enabled. Currently, there's only one entry. Delete it from both folders. Then, create a new entry in sites-available for your project.

Open your new site configuration in your text editor and set it up to resemble the following.
server {
    listen 80;
    listen [::]:80;
	server_name your-site.com

    root /var/www/yourProject;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

When everything reflects your server, save and exit. Now, you can restart Nginx for the changes to take effect.
$ sudo systemctl restart nginx
You can browse to the server name that you specified to Nginx to view your site.

Closing Thoughts

Laravel is extremely powerful, and this configuration can get you to either a great development setup or the beginnings of a production server. There certainly is more that you can do with Laravel and Nginx, so feel free to explore different configuration options on a development machine.
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
Get extra help by visiting our LINUX FORUM or simply use comments below.