ToDo List Sample running on Nginx
Now that Laravel 5 has been installed and running on Apache server (Laravel 5 Todo sample app).
Now we want to run it on Nginx.
Install the missing PHP tools needed to actually run the Laravel code:
$ sudo apt-get install php5-fpm
Open the main PHP configuration file (/etc/php5/fpm/php.ini) for the PHP-fpm processor that Nginx uses. We only need to modify one value for the cgi.fix_pathinfo parameter. We need to uncomment this and set it to "0". This is to guard against a potential attacker who may want to run a script with a similar name.
Enable the MCrypt extension, which Laravel depends on:
$ sudo php5enmod mcrypt
Since we made some changes, we need to restart the php5-fpm service:
$ sudo service php5-fpm restart
First, let's work on nginx related setup.
/etc/nginx/sites-available/laravel.example.com:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/todo/public; index index.php index.html index.htm; server_name laravel.example.com; location / { try_files $uri $uri/ /index.php?$query_string; } }
We will set the fastcgi_* directives so that the path of requests are correctly split for execution, and make sure that Nginx uses the socket that php5-fpm is using for communication and that the index.php file is used as the index for these operations.
Then, we need to set the SCRIPT_FILENAME parameter so that PHP can locate the requested files correctly:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/todo/public; index index.php index.html index.htm; server_name laravel.example.com; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Actually, I'm using Nginx for another sites. So, I switched to a new port, 8001, like this:
# /etc/nginx/sites-available/laravel.example.com server { listen 8001 default_server; listen [::]:8001 default_server ipv6only=on; root root /var/www/todo/public; index index.php index.html index.htm; server_name laravel.example.com; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
Here is our page with a new port, 8001:
Another site can be configured like this (/etc/nginx/sites-available/epicmath.example.com):
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; ...
So, our activated sites are now look like this:
$ ls -la /etc/nginx/sites-enabled/ epicmath.example.com -> /etc/nginx/sites-available/epicmath.example.com laravel.example.com -> /etc/nginx/sites-available/laravel.example.com
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization