Installing Laravel PHP Framework on Ubuntu for Apache
Laravel is a very popular open source PHP framework aimed at easy development of applications. If you are looking for a new PHP framework to try, you should give Laravel a try.
The following guide will show you how to install and run Laravel on an Ubuntu 16.04 LTS based Apache server. This tutorial works for Ubuntu 17.x as well, but for servers you should prefer to use an Ubuntu LTS release like the current Ubuntu 16.04 LTS.
Pre-Requisites
Before proceeding with the installation, it's always a good idea to make sure your sources and existing software are updated.
sudo apt-get update
sudo apt-get upgrade
For this guide, we will assume that you have a basic server based on Ubuntu running. Before Laravel, we need to install other components that are essential.
Installing PHP 7.1
Next step is to install PHP along with several extra packages that would prove useful if you are going to work with Laravel.
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.1 php7.1-mcrypt php7.1-xml php7.1-gd php7.1-opcache php7.1-mbstring
Even though Ubuntu's own repository has PHP, it's better to add a 3rd party repository here because it gets more frequently updated. You can skip that step and stick to Ubuntu's version if that's what you prefer.
Installing Apache
It's time to install Apache server now. We would also need to install libapache2-mod-php7.1 package to hook up Apache with PHP.
sudo apt-get install apache2 libapache2-mod-php7.1
Installing Laravel
Before we finally delve into it, we also need Git version control to be installed. If you have it installed, you can skip the following step. If you don't have, then you can follow our guide to set it up first.
To install Laravel, we need to install Composer first. It is a tool for dependency management in PHP that allows you to package all the required libraries associated with a package as one. To install Laravel and all its dependencies, Composer is required. It will download and install everything that is required to run Laravel framework. To install Composer, issue the following commands.
cd /tmp
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
The curl command downloads composer.phar package to your /tmp directory. But we would want composer to run globally hence we need to move it to /usr/local/bin directory under the name 'composer'. Now we can run composer from anywhere.
To install Laravel, move to the public html directory on your system. Since we are on Ubuntu and using Apache, we will install it in the /var/www/html directory.
cd /var/www/html
sudo composer create-project laravel/laravel your-project --prefer-dist
The above command will create a "your-project" directory with Laravel installation in it. Composer uses git to download and install all the packages and modules that Laravel requires for functioning.
Configuring Apache
Now that we have installed Laravel, we move on to the step of configuring Apache web server.
Next step is to give proper permissions to the project directory. For this, we need to enable access to it from the www-data group and to give it writing permissions to the storage directory.
sudo chgrp -R www-data /var/www/html/your-project
sudo chmod -R 775 /var/www/html/your-project/storage
Now go to the /etc/apache2/sites-available directory and use the following command to create a configuration file for our laravel install.
cd /etc/apache2/sites-available
sudo nano laravel.conf
Now add the following content to the file and close it after saving. Replace yourdomain.tld with the domain name of your website inside the file.
<VirtualHost *:80>
ServerName yourdomain.tld
ServerAdmin [email protected]
DocumentRoot /var/www/html/your-project/public
<Directory /var/www/html/your-project>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now we have to enable this newly created .conf file and disable the default .conf file that is installed with the default Apache install. Also, we need to enable mod_rewrite so that permalinks can function properly.
sudo a2dissite 000-default.conf
sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo service apache2 restart
Your Laravel installation is now complete. Visit the IP address or domain name of your server with a web browser (in my case http://192.168.1.100). You will see the Laravel default page.
Virtual machine download of this tutorial
This tutorial is available as ready to use virtual machine image in ovf/ova format that is compatible with VMWare and Virtualbox. The virtual machine image uses the following login details:
SSH / Shell Login
Username: administrator
Password: howtoforge
To become root user, run: sudo -s
Password: howtoforge
The IP of the VM is 192.168.1.100, it can be changed in the file /etc/network/interfaces. Please change all the above passwords to secure the virtual machine.
Suggested articles
44 Comment(s)
Comments
You got me with this:
sudo composer create-project laravel/laravel your-project --prefer-dist
So after we have to do:
sudo chgrp -R www-data /var/www/html/your-projectsudo chmod -R 775 /var/www/html/your-project/storage
(where is your-project is some project name)
Also here:
<VirtualHost *:80> ServerName localhost ServerAdmin [email protected] DocumentRoot /var/www/html/your-project/public <Directory /var/www/html/your-project> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>Because project will never start...But any way, thank you guys for great tutorial ;)Best tutorial so far. Congrats!
very useful. Thanks.
Best explanation I've ever encountered! Thnx,
thanks , very simple and useful.
perfect!!! everything has been in order !!! thanks
i've been trying to install for last 2 days.
This is the only tutorial I've been able to use to install laravel succcessfully. Thanks a lot!
Very usefull...Thanks
Your configure apache section worked perfectly to solve my problem.
Everything worked like cham!!. Great post and Thanks
Awesome tutorial
after running
sudo apt-get install apache2 libapache2-mod-php5
this shows up:
The following packages have unmet dependencies:
libapache2-mod-php5: Depends: apache2-api-20120211 but it is not installable
Depends: apache2(>= 2.4)
E: Unable to correct problems, you have held broken packages.
I really dont know how to move forward from this
run:
apt-get update
and then try the failed command again.
hi people,
after i follow this tutorial, in the end give me on error
* Restarting web server apache2 [fail] * The apache2 configtest failed.Output of config test was:AH00526: Syntax error on line 2 of /etc/apache2/sites-enabled/laravel.conf:Invalid command '\xc2\xa0\xc2\xa0', perhaps misspelled or defined by a module not included in the server configurationAction 'configtest' failed.
i do this
sudo chgrp -R www-data /var/www/html/extremesudo chmod -R 775 /var/www/html/extreme/storage
this is i have in laravel.conf
<VirtualHost *:80> ServerName localhost ServerAdmin [email protected] DocumentRoot /var/www/html/extreme/public <Directory /var/www/html/extreme> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
what i do wrong?
Really clear and detailed instructions.Can't thank you enough ! :D
Followed all instructions but hit one problem at the end. /localhost/index creates a Laravel routes error but /localhost/index.php runs fine.
I'm running Ubuntu 16.04 LTS and php7. Laravel version is 5.3.9.
The biggest problems with Laravel are the subtle changes from release to release which means each release requires a different installation stragety. :/
Best tutorial. It works like a charm!
Hi! I have tried to install the laravel..but when I type this command "sudo composer create-project laravel/laravel your-project --prefer-dist" I get the following error: [Composer\Downloader\TransportException] The "https://packagist.org/packages.json" file could not be downloaded: fai led to open stream: Connection timed out ......Somebody help...I will so much appreciate thanks
How to install laravel 5.3 on ubuntu ?
Thank you!
Versions of php and Apache are no more supported in Ubuntu.
Solution :
sudo apt install apache2 php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettextI got all the things done accordding to this tutorial and without any error but going to localhost, it shows completely blank screen. so, i don't know whether laravel is working fine or not.
I am new to laravel. Can anyone suggest me what to do?
Any help will be highly apprecciated.
How can I run two websites (main domain website mainwebsite.com in HTML, mainwebsite.com/laravel-project ). How can I make sure that both websites are running after disabling default config? How to take care of that?
Thanks in advance!
Step: 'composer install' is missing
Thank you. Worked like a charm
sudo add-apt-respository ppa:ondrej/php5 ---> sudo add-apt-repository ppa:ondrej/php5
after run :
sudo composer create-project laravel/laravel your-project --prefer-dist
i got "Do not run Composer as root/super user! See https://getcomposer.org/root for details"
what could go wrong ?
sudo add-apt-repository ppa:ondrej/php5'This PPA does not support trusty'Cannot add PPA: ''This PPA does not support trusty''.
Please help me..
how to restart server after closing system and using that again.
Thanks! This works great! :)
Thanks, this tutorial really works.
Best tutorial... Congrats. I've been searching for tutorial on website and I always get orror on instalation. And now I found your website. Ta da.. everything gonna be allright
Great!I change one thing:
chgrp -R www-data /var/www/html/project
for:chown -R www-data:www-data /var/www/html/projectGive all permissions to web service
Thanks a lot bhai..
Thank you, man!
Thanks.
sudo add-apt-respository ppa:ondrej/php5 please replce this command line because you have misspelled repository
Nice article, thank you!
An easy way to try out Laravel in action is to use Laravel 5 Boilerplate / Starter Kit - https://github.com/Labs64/laravel-boilerplate
This is also offering Docker container, with this you don’t need a local PHP (composer, node.js, etc.) environment and can start to evaluate Laravel right away.
the best tutorial for laravel installation , please update with php7 version too.
Thanks
I only needed the apache configuration and it worked perfectly on Debian Stretch, thanks a lot!
Good !
I am successfully instald laravel for thanks.
Running sudo chgrp -R www-data /var/www/html/project command just messed up my entire linux, now my sudo command doesn't work.
The command is fine, it can not mess up your Linux when typed in correctly. Most likely, you added a whitespace somewhere in the path.
Why on earth you give ownership to the whole filesystem of your web project to apache user? you don't really need it and it opens the project to any kind of defacement if you have a small bug or any uncontrolled upload feature in your application
English |
Deutsch