With this tutorial, you will start from a clean Ubuntu 16.04 install, set it up to run Laravel, and end up with a demo Laravel application installed. Other articles in this series will show you how to set up the database and Laravel environment, and then how to strengthen the Laravel setup for production.
;
We shall install and deploy Laravel 5 on Ubuntu 16.04:
Once you are logged in as root, you can create a new user account that you’ll use to access the app once it is installed. We’ll call the new user laraveluser. To create it, run:
adduser laraveluser
Then, add it to the sudo group, so that you can run commands as sudo:
usermod -aG sudo laraveluser
First, update your package manager’s cache:
sudo apt update -y
Install the Apache web server:
sudo apt install apache2 -y
Enable its service to make it run on every system boot:
sudo systemctl enable apache2
Finally, start it:
sudo systemctl start apache2
To verify that Apache was installed successfully, access it from your local browser by navigating to http://SERVER_IP/. If that does not work, try adding :80 in the end, like this:
http://SERVER_IP:80
You should see a welcome page for Apache, which means that you now have Apache running.
We are installing version 5.8 of Laravel, which requires a version of PHP greater than 7.1.3. We shall install PHP 7.2. First install the prerequisite packages:
sudo apt-get install software-properties-common python-software-properties
Then, add the ondrej PPA:
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
and update your sources by running:
sudo apt install php -y
Install PHP 7.2 using the following command:
sudo apt-get install php7.2 php7.2-cli php7.2-common
These are the PHP extensions that Laravel 5.8 requires:
First install the most popular PHP extensions:
sudo apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip
Then add the extensions specific to Laravel:
sudo apt install -y php7.2-bcmath php7.2-ctype
Restart to activate changes:
sudo systemctl restart apache2
Linux uses tar to decompress files while some of the files for PHP come in zip format, so here is the command to install zip and unzip extensions as well:
sudo apt -y install zip
We shall use git to install Laravel apps from github.com.
sudo apt install -y git
Redis is a database, cache and message in-memory broker. We are not going to use it in this tutorial, though, but we can install it now.
sudo apt install -y redis-server
Laravel uses Composer to manage all of its dependencies.
Install curl to download the install script for Composer:
sudo apt install curl -y
Then, download the installer:
cd ~
sudo curl -s https://getcomposer.org/installer | php
To make it available globally, move it to /usr/bin:
sudo mv composer.phar /usr/local/bin/composer
You are installing Composer as root user and it will show a warning about it. Once we have the app installed, we shall give access to its folder to user laraveluser.
Apache stores the data it serves under /var/www/html. To create a new Laravel app, run:
composer create-project --prefer-dist laravel/laravel /var/www/html/blog
Here “blog” will be the name of the app.
When Composer asks if you want it to set up folder permissions, answer with y
.
Run these two commands to enable access to folders storage and bootstrap/cache as without that access, Laravel will not run:
sudo chmod -R a+rw /var/www/html/blog/storage
sudo chmod -R a+rw /var/www/html/blog/bootstrap/cache
We can now give user laraveluser access to the app folder:
sudo chown -R laraveluser /var/www/html/blog
Ubuntu will ask you for passwords whenever you change or access something in the system so be sure to have them nearby.
Edit Apache configuration file so that it will serve Laravel app by default. It is called 000-default.conf and is stored in directory /etc/apache/sites-enabled. Open it for editing:
sudo nano /etc/apache2/sites-enabled/000-default.conf
Find line which starts with DocumentRoot and replace it with this:
DocumentRoot /var/www/html/blog/public
Save and close the file.
Laravel requires Apache modulerewrite_ to also be enabled; do so by running:
sudo a2enmod rewrite
Finally, instruct Apache to use .htaccess files, with which Laravel configures Apache on the fly. Open Apache global configuration file for editing:
sudo nano /etc/apache2/apache2.conf
Under the block, you’ll find the following line:
AllowOverride None
Change it to
AllowOverride All
When you are done, save the file.
Again restart Apache so that it takes new configuration into account:
sudo systemctl restart apache2
You can now navigate to your domain in your browser. You should see the following:
Laravel is now installed properly.
Now you have a Laravel app on your server. The server is set up but Laravel itself is not. Follow the article “How To Set Up a Laravel Application on Ubuntu 16.04” which will show you how to create a database, connect it to the app and set up environment for a Laravel app properly. This will enable you to further develop and test your app on this server.
When your app becomes ready for deployment and production, read the next article in the series, on “How To Set Up Laravel For Production: Ubuntu 16.04”.
In this article, we will see how to Install Google Cloud BigQuery Python client library…
Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…
MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…
Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…
In today’s massive private mobile network (PMN) market, one of the most common approaches to…
Welcome to the Ubuntu Weekly Newsletter, Issue 865 for the week of November 3 –…