How to install Laravel on Ubuntu
Last modifiedAre you eager to harness the power of Laravel, one of the most popular PHP frameworks, on your Ubuntu-based server? Installing Laravel on your Ubuntu system is your first step towards building robust web applications with ease and efficiency. In this comprehensive guide, we will walk you through the step-by-step process of setting up Laravel on your Ubuntu environment, ensuring that you have all the tools and knowledge necessary to embark on your web development journey. Let's dive into the world of Laravel and Ubuntu, and unleash the potential of this dynamic combination.
1. Install curl if not already installed
sudo apt-get install curl
2. install composer if not already installed
curl -sS https://getcomposer.org/installer | sudo php — –install-dir=/usr/local/bin –filename=composer
3. Now run blow command to install laravel
composer global require "laravel/installer=~1.1"
4. Add laravel to PATH to make it available everywhere
export PATH="~/.composer/vendor/bin:$PATH"
5. Create your Laravel app
Now that Laravel is added to path, now all sudoers can create laravel apps using below command
laravel new app-name
if you are not a sudoer, above command will show below error
laravel: command not found
You can create a link in /usr/local/bin to solve this problem
sudo ln -s /root/.composer/vendor/bin/laravel /usr/local/bin/laravel
Now non sudoers can create laravel apps like
sudo laravel new app-name
6.Install mod_rewrite if not already installed
sudo a2enmod rewrite
service apache2 restart
You can confirm if its enabled using below command
sudo apache2ctl -M
if you created your app inside web server root directory (/var/www/html/) then you can access it like
http://localhost/app-name/public
Creating VHOST for Laravel application
1. Create vhost file
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/app-name.com.conf
sudo nano /etc/apache2/sites-available/app-name.com.conf
The file will look something like this (I’ve removed the comments here to make the file more approachable):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.
ServerAdmin admin@app-name.com
Then ServerName and ServerAlias
ServerName app-name.com
ServerAlias www.app-name.com
The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRoot directive to reflect the directory we created:
DocumentRoot /var/www/app-name
In total, our virtualhost file should look like this:
<VirtualHost *:80>
ServerAdmin admin@app-name.com
ServerName app-name.com
ServerAlias www.app-name.com
DocumentRoot /var/www/html/app-name/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
save and close the file.
2. Enable the Virtual Host File
sudo a2ensite app-name.com.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
3. Set Up Local Hosts File (Optional)
sudo nano /etc/hosts
hosts file will look something like this
127.0.0.1 localhost
111.111.111.111 app-name.com
For the domain that I used in this guide, assuming that my VPS IP address is 111.111.111.111, if you are creating vhost locally, you can replace 111.111.111.111 with 127.0.0.1
Now that you have your virtual hosts configured, you can test your laravel app easily by going to the domain that you configured in your web browser:
http://app-name.com