Deploy Laravel Nginx Ubuntu 17


Deploy Laravel Nginx Ubuntu 17

Requirements for Installing Laravel 5.4 on Ubuntu 17

This article will guide you through the process of installing Laravel 5.4 on Ubuntu 17. Before we move on to the installation process, there are a few things that we need to make sure we have in place. First of all, you should have a fresh install of Ubuntu 17. Secondly, it is important to have Nginx installed already. Third, you will need Node.js installed (this can be done by following this article: Install Node.js on Ubuntu 17.04). Last but not least, you need to ensure that you have MySQL installed (this can be done by following this article: Install MySQL on Ubuntu 17.04).

Installing Composer

We need to install Composer in order to install Laravel 5.4. To do this, first enter the command:
sudo apt-get install composer. Once the installation has finished, you can verify that it is installed properly by entering the command : composer -V. The output should display the version of Composer which is installed.

Installing Laravel 5.4 on Ubuntu 17

In order to install Laravel 5.4 on Ubuntu 17, we need to first download and install it. To do this, we must first enter the command :
composer create-project laravel/laravel myproject --prefer-dist. This command will install the latest version of Laravel 5.4. Once the installation is complete, navigate to the newly created project directory. You can do this by entering the command :
cd myproject.

Configuring Nginx for Laravel 5.4

Now that Laravel 5.4 is installed, we need to configure Nginx for it. To do this, create a new Nginx virtual host configuration file. You can do this by entering the command :
sudo nano /etc/nginx/sites-available/myproject.conf. Once the file is created, paste the following inside of it:


server {
listen 80;
root /var/www/myproject/public;
index index.php index.html index.htm;
server_name myproject.com;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

location ~ /.ht {
deny all;
}
}

Once you have pasted the above code, save the file and close it. To enable the Nginx virtual host configuration, enter the command : sudo ln -s /etc/nginx/sites-available/myproject.conf /etc/nginx/sites-enabled/. Lastly, we need to restart Nginx so that the changes can take effect. You can do this by entering the command : sudo systemctl restart nginx.

Configuring MySQL for Laravel 5.4

Before configuring MySQL for Laravel 5.4, lets create a new database. To do this, log in to MySQL by entering the command:
mysql -u root -p [enter_your_MySQL_password]. Once you are logged in, create a new database by entering the following command :
CREATE DATABASE myproject;. Next, we need to create a new user for this database. To do this, enter the command :
CREATE USER 'myproject'@'localhost' IDENTIFIED BY '[enter_your_password]';. Finally, we need to grant privileges to our newly created user. You can do this by entering the following command:
GRANT ALL PRIVILEGES ON myproject.* TO 'myproject'@'localhost';. Once you have done this, exit the MySQL shell by entering the command : exit.

Connect Laravel 5.4 with MySQL

Now, let us connect our newly created database with Laravel 5.4. To do this, open the .env file inside of our project directory. This can be done by entering the command :
nano .env. Once the file is open, search for the following code :


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Now, we just need to replace the settings with ours. Replace the code with the following :


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=myproject
DB_PASSWORD=[enter_your_password]

Once you have done this, save and close the file. Your project is now connected to MySQL.

Testing Laravel 5.4 Installation

Now that everything is set up, let us test our Laravel 5.4 installation. To do this, open the browser and enter the URL : http://localhost/myproject/public. If everything is set up correctly, you should see the Laravel welcome page as shown below.

Laravel Welcome Page

Conclusion

That’s it! You have now successfully installed Laravel 5.4 on Ubuntu 17. This article only covered the basics of getting started with Laravel 5.4. There are many more features and settings that can be adjusted, but this should give you the basic foundation to start building your applications. Thank you for reading this article. Please read other articles for further help.