Click here to Skip to main content
15,887,596 members
Articles / Web Development / Apache
Tip/Trick

Installing & Configuring Laravel

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 Jan 2018CPOL2 min read 5.8K   2   1
Set up laravel 5 and set permissions for the Apache user to write to storage directory

Many times, after installing laravel, there is usually an access error message of the storage directory when trying to access the laravel home page of your project. While doing a "chmod -R 777" (allowing full access) on the project folder solves the issue, it is not a safe approach but rather you should allow apache user write access to the necessary folders while denying everyone else.

In this article, I will take you through a typical laravel setup. I will assume you are using linux and xampp is already installed. If not, you can find the tutorial here.

Once xampp is installed, you will need to change ownership of the htdocs directory from root to your current user. This is because you will not be editing your code in this folder as the root user but as regular user. To do this, you should change owner of the htdocs directory and all directories within like this:

Quote:

$ sudo chown username -R /opt/lampp/htdocs/

You will need to make PHP command available globally in the terminal because by default, for example, you will get an error if you run the following command:

Quote:

$ php --version

Without making the php command globally available, you will always need to type the full path to php as shown below which is usually cumbersome:

Quote:

$ /opt/lampp/bin/php --version

 

To correct this, edit your .bashrc to add the desired directory on the PATH environmental variable. The directory in this case is the bin folder located at /opt/lampp/bin. Type the following at the command line terminal.

Quote:

$ export PATH=/opt/lampp/bin:$PATH

$  source ~/.bashrc

With /opt/lampp/bin directory set to the PATH environmental variable, navigate to this folder and install composer. This will create a file called "composer" in the bin directory. Note that the composer command will also be available system wide on the terminal.

Quote:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 
'544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061'
) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); 
} echo PHP_EOL;"
php composer-setup.php --filename=composer
php -r "unlink('composer-setup.php');"

Navigate to htdocs folder (/opt/lampp/htdocs) and install laravel:

Quote:

composer create-project laravel/laravel yourprojectname

After installation is complete, you will then need to make sure the Apache user has write permissions to the storage folder and the bootstrap/cache folder of your just installed project. For xampp set up, the Apache user is called "daemon" but you can also create a small PHP script and run in to get the Apache username as follows:

PHP
// <?php
// echo exec(whoami);
//

This shows the Apache username on a web page. You then have to give this user write permissions on the boostrap/cache and storage folder of your project. Navigate to your project folder and type the following:

Quote:

$ sudo chgrp -R daemon storage bootstrap/cache

$ sudo chmod -R ug+rwx storage bootstrap/cache

You should now be able to view the laravel home page of your new application with no errors.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kenya Kenya
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWow thank you so much this is really helpful! Pin
Member 147361945-Feb-20 7:23
Member 147361945-Feb-20 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.