Deploy Laravel Web App to Server CentOS7

This note shows my mistakes in an early stage of using Laravel due to lack of understanding the way this framework works. This also shows my solution and my experience after solving this problem.

Problem

In the beginning, I deploy my project to Apache by using Git to clone and pull a project from my PC. Later, I used `php artisan serve –host=110.xxx.xx.xx –port=8080 ` for starting my website and it works well in any scene. Afterward, I try to deploy other projects to the same server by running the same command but on another port `php artisan serve –host=110.xxx.xx.xx –port=8181 `. Although its run normally but I cannot access the second one. I only access the second when I stop to serve the first. So the question in my mind at that time is:<br>

How can we run multiple Laravel projects (multiple websites) in the same host?

Where is my solution?

Firstly, I try to find answers in [Stackoverflow](https://stackoverflow.com/) and [Laracast](https://laracasts.com/) for the question “How to serve multiple Laravel websites by using artisan?”. Although I found many solutions, those are related to opening a VirtualHost on serve. Those are not my expected solution so I try to find another way.<br>

After reading a ton of posts, I have found a good way to deploy our app to Apache without using `php artisan`. In this way, we will use `composer dump-autoload` for loading and rending php page from laravel statements. The detail of this way and some subproblems happing when I conduct it are shown step by step in the next section.

Current solution

[ running environment CentOS7]

  • Clone a project to the server by using
git clone [your_project_remote_link]
  • Set apache as ownner of the root project and `chmod` to set access permision for www
sudo chown -R apache:apache [path_to_project]
sudo chmod 775 [path_to_project]/storage
sudo chmod 775 [path_to_project]/boostrap/cache
  • If SElinux is enable on the server, we have to add permission for accessing `project/storage` as well as `database`
chcon -R -t httpd_sys_rw_content_t [path_to_project]/storage
setsebool -P httpd_can_network_connect_db on
  • For `routing`, we have to revise apache config file `httpd.config`. It lets `htaccess` file of Laravel can works
vim /etc/httpd/conf/httpd.conf

Change `AllowOverride None` to `AllowOverride All` and uncommand `Options Indexes FollowSymLinks`

  • Restart apache by using
service httpd restart
  • Now, jump into your project and run some commands for setting up:

Install project

composer install

Copy .env from .env.example

cp .env.example .env

Change information in .env such as sql connection and save it

vi .env

Generate app key

php artisan key:generate

Check out correct branch on git

git checkout -b branch name
git pull origin branch name

Update dependencies

composer update

Clear cache by using

php artisan cache:clear

Reload class file by using

composer dump-autoload

Migrate database

php artisan migrate

Add some sample data

php artisan db:seed
  • Afterward, create a shorcut directory in `/var/www/html/[sitename]` and link to directory ‘[path_to_project]/public’

Using create soft link command

ln -s [path_to_project]/public var/www/html/[sitename]

Test a site by accessing [domain]/[sitename]

Sub problem: `php artisan storage:link`

Although I have run `php aritisan storage:link`, we cannot access to any sub directory inside of `[path_to_project]/public/storage`.

I solved this problem by using create softlink command manually and for making sure I also set the permision for appache to access the new created dir.

ln -s [path_to_project]/storage/app/public/ [path_to_project]/public/storage

Leave a comment