Install Laravel 5 on Nginx
The below Laravel install and setup instructions are derived from forum thread here. It goes as far as proper install and Nginx vhost configuration for Centmin Mod Nginx. However, the rest of Laravel usage is up to you. I have never used Laravel myself beyond just test installation using below steps. If there's any suggestions and/or corrections to the steps and configuration, please post them to the forum thread here
Below steps are derived from official Laravel documentation at
- Installation - Laravel - The PHP Framework For Web Artisans
- Configuration - Laravel - The PHP Framework For Web Artisans
- Composer
Getting Started guide steps particular step 1 and 2 to create new Nginx vhost via centmin.sh menu option 2
, created a domain called newdomain1.com
so end up with:
vhost for newdomain1.com created successfully vhost conf file for newdomain1.com created: /usr/local/nginx/conf/conf.d/newdomain1.com.conf upload files to /home/nginx/domains/newdomain1.com/public vhost log files directory is /home/nginx/domains/newdomain1.com/log
Disable apc.enable_cli
if you use APC Cache
sed -i 's/apc.enable_cli=1/apc.enable_cli=0/' /etc/centminmod/php.d/apc.ini fpmrestart php -i | grep apc.enable_cli
Disable Zend Opcache opcache.enable_cli
if enabled. It's usually disabled by default opcache.enable_cli=0
sed -i 's/opcache.enable_cli=1/opcache.enable_cli=0/' /etc/centminmod/php.d/zendopcache.ini fpmrestart php -i | grep opcache.enable_cli
Install composer globally
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Check composer works
composer --help
Using composer create-project command Composer to create a new project called, myprojectname by first changing to /home/nginx/domains/newdomain1.com
top level directory of your account setup via centmin.sh menu option 2
So your Laravel project ends up at /home/nginx/domains/newdomain1.com/myprojectname
cd /home/nginx/domains/newdomain1.com composer create-project laravel/laravel myprojectname --prefer-dist
Setup nginx user/group permissions and permissions of storage
directory
chown -R nginx:nginx /home/nginx/domains/newdomain1.com/myprojectname chmod -R 0770 /home/nginx/domains/newdomain1.com/myprojectname/storage
Check directory ownership/permissions
ls -lah /home/nginx/domains/newdomain1.com/myprojectname/ total 164K drwxr-sr-x 11 nginx nginx 4.0K Aug 31 02:03 . drwxr-s--- 7 nginx nginx 76 Aug 31 01:59 .. drwxr-sr-x 10 nginx nginx 134 Aug 30 11:31 app -rwxr-xr-x 1 nginx nginx 1.7K Aug 30 11:31 artisan drwxr-sr-x 3 nginx nginx 51 Aug 30 11:31 bootstrap -rw-r--r-- 1 nginx nginx 1.2K Aug 30 11:31 composer.json -rw-r--r-- 1 nginx nginx 103K Aug 31 02:03 composer.lock drwxr-sr-x 2 nginx nginx 4.0K Aug 30 11:31 config drwxr-sr-x 5 nginx nginx 68 Aug 30 11:31 database -rw-r--r-- 1 nginx nginx 323 Aug 31 02:03 .env -rw-r--r-- 1 nginx nginx 307 Aug 30 11:31 .env.example -rw-r--r-- 1 nginx nginx 61 Aug 30 11:31 .gitattributes -rw-r--r-- 1 nginx nginx 57 Aug 30 11:31 .gitignore -rw-r--r-- 1 nginx nginx 503 Aug 30 11:31 gulpfile.js -rw-r--r-- 1 nginx nginx 159 Aug 30 11:31 package.json -rw-r--r-- 1 nginx nginx 87 Aug 30 11:31 phpspec.yml -rw-r--r-- 1 nginx nginx 899 Aug 30 11:31 phpunit.xml drwxr-sr-x 2 nginx nginx 73 Aug 30 11:31 public -rw-r--r-- 1 nginx nginx 1.9K Aug 30 11:31 readme.md drwxr-sr-x 5 nginx nginx 42 Aug 30 11:31 resources -rw-r--r-- 1 nginx nginx 567 Aug 30 11:31 server.php drwxrws--- 5 nginx nginx 43 Aug 30 11:31 storage drwxr-sr-x 2 nginx nginx 47 Aug 30 11:31 tests drwxr-sr-x 28 nginx nginx 4.0K Aug 31 02:03 vendor
Check version of Laravel installed
cd /home/nginx/domains/newdomain1.com/myprojectname php artisan -V Laravel Framework version 5.1.12 (LTS)
For Centmin Mod .08 beta and higher, need to edit /usr/local/nginx/conf/php.conf
and modify and comment out the open_basedir path as that prevents laravel from finding and accessing the rest of the projects files. You can use command short cut to edit /usr/local/nginx/conf/php.conf
phpinc
then comment out with hash # in front this line
#fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:/usr/local/lib/php/:/tmp/;
so it shows up as
location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename; #fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:/usr/local/lib/php/:/tmp/;
If you use Centmin Mod .07 stable you can ignore commenting out open_base_dir line
Then setup your Nginx vhost that was generated via centmin.sh menu option 2
at /usr/local/nginx/conf/conf.d/newdomain1.com.conf
change from default format below
# Centmin Mod Getting Started Guide # must read http://centmin.sh/getstarted.html # redirect from non-www to www # uncomment, save file and restart Nginx to enable # if unsure use return 302 before using return 301 #server { # listen 80; # server_name newdomain1.com; # return 301 $scheme://www.newdomain1.com$request_uri; # } server { server_name newdomain1.com www.newdomain1.com; # ngx_pagespeed & ngx_pagespeed handler #include /usr/local/nginx/conf/pagespeed.conf; #include /usr/local/nginx/conf/pagespeedhandler.conf; #include /usr/local/nginx/conf/pagespeedstatslog.conf; # limit_conn limit_per_ip 16; # ssi on; access_log /home/nginx/domains/newdomain1.com/log/access.log combined buffer=256k flush=5m; error_log /home/nginx/domains/newdomain1.com/log/error.log; root /home/nginx/domains/newdomain1.com/public; location / { # block common exploits, sql injections etc #include /usr/local/nginx/conf/block.conf; # Enables directory listings when index file not found #autoindex on; # Shows file listing times as local time #autoindex_localtime on; # Enable for vBulletin usage WITHOUT vbSEO installed # More example Nginx vhost configurations at # http://centmin.sh/nginx_configure.html #try_files $uri $uri/ /index.php; } include /usr/local/nginx/conf/staticfiles.conf; include /usr/local/nginx/conf/php.conf; include /usr/local/nginx/conf/drop.conf; #include /usr/local/nginx/conf/errorpage.conf; include /usr/local/nginx/conf/vts_server.conf; }
to this format with custom root doc root path at /home/nginx/domains/newdomain1.com/myprojectname/public
# Centmin Mod Getting Started Guide # must read http://centmin.sh/getstarted.html # redirect from non-www to www # uncomment, save file and restart Nginx to enable # if unsure use return 302 before using return 301 #server { # listen 80; # server_name newdomain1.com; # return 301 $scheme://www.newdomain1.com$request_uri; # } server { server_name newdomain1.com www.newdomain1.com; # ngx_pagespeed & ngx_pagespeed handler #include /usr/local/nginx/conf/pagespeed.conf; #include /usr/local/nginx/conf/pagespeedhandler.conf; #include /usr/local/nginx/conf/pagespeedstatslog.conf; access_log /home/nginx/domains/newdomain1.com/log/access.log combined buffer=256k flush=5m; error_log /home/nginx/domains/newdomain1.com/log/error.log; root /home/nginx/domains/newdomain1.com/myprojectname/public; location / { try_files $uri $uri/ /index.php?$query_string; } include /usr/local/nginx/conf/staticfiles.conf; include /usr/local/nginx/conf/php.conf; include /usr/local/nginx/conf/drop.conf; #include /usr/local/nginx/conf/errorpage.conf; include /usr/local/nginx/conf/vts_server.conf; }
Then restart Nginx and PHP-FPM services
using command shortcuts restart Nginx
ngxrestart
using command shortcuts restart PHP-FPM
fpmrestart
using command shortcuts restart Nginx + PHP-FPM at same time
nprestart
Then go to newdomain1.com
in web browser and should see the Laravel 5 defaul placeholder page.