Setup Ubuntu Server For Laravel Project

Local set up with Laravel application in your local environment is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle. 



Here we’ve formulated a detailed 11 steps recipe for deploying your Laravel application on any Ubuntu box with ease.

Step # 01 – Setting up
sudo apt update
sudo apt upgrade

Step # 02 – Install some basic dependencies
sudo apt install -y curl wget zip unzip

Step # 03 – Install Apache Server
sudo apt install apache2

Wait for apache to complete installation. Test if its running.

sudo systemctl status apache2

apache-test

The services as you may see have started successfully, you can now access your server through http://your_ip_address/ & you’ll see apache2 default homepage.

It is important to know that all your projects should be under /var/www/html/ directory. Although you may assign any folder in your server as your default root web which we’ll cover later on.

You can read more about Apache’s basic commands that almost comes in handy for every project setup.

Step # 04 – Enable rewrite mode
sudo a2enmod rewrite
sudo systemctl restart apache2

Step # 05 – Install MySQL
sudo apt install mysql-server

Follow the instructions for setup.
Optional – For securing MySQL :

mysql_secure_installation

Step # 06 – Install PHP
sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.2
Additionally install some of useful PHP’s extensions.

sudo apt install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

Step # 07 – Install Composer
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Test Composer by running composer in your terminal. Make sure its working

Step # 08 – Setup Laravel
Go into your server’s web root. i.e

cd /var/www/html

Create a new Laravel Project using composer

composer create-project --prefer-dist laravel/laravel twt_laravel

Step # 09 – Test it out
Head over to your browser & type in http://your_ip_address/twt_laravel/public
You may also run php artisan serve inside your project root that will boot up php’s default web server & get your site up & running.

Step # 10 – Configure Virtual Host
cd /etc/apache2/sites-available
Now Lets create a new configuration file for our Laravel application.

sudo touch twt_laravel.conf

Edit the File & paste the following

<VirtualHost *:80>.
# Your IP Address
ServerName twt_laravel.local
ServerAdmin webmaster@localhost
# your Web Root Folder 0r Even your Custom Folder
DocumentRoot /var/www/html/twt_laravel/public/
<Directory /var/www/html/twt_laravel/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log
</VirtualHost>

Step # 11 – Setting up Permissions & Enabling Virtual Host
chmod -R 755 /var/www/html/twt_laravel/public
We’ll disable the default apache site & enable our newly created site likeso.

sudo a2dissite 000-default
sudo a2ensite twt_laravel

Lets set permissions for storage and cache folders aswell.

chmod -R 755 storage
chmod -R 755 bootstrap/cache
Lastly, change user type to apache www-data user.

chown -R www-data:www-data *

You may access your Laravel application by typing in http://twt_laravel.local/ in your browser and it will work just like magic.

If you have any questions or feedback related to this blog, let us know in the comment section below.

Source : thewebtier.com

Comments