1. Install PHP, Nginx and MySQL
Steps:
- Install PHP by:
sudo aptitude install php7.0-fpm php7.0-cli php7.0-curl php7.0-gd php7.0-mcrypt php7.0-cgi php-mysql
- Install Nginx by:
sudo apt-get install nginx
Notice:
- if all module were installed sucessfully, you should be able to visit nginx root page by
http://yourRaspberrypiIP
- Make sure all php packages were correctly installed, or the database may won't work at all.
2. Global configuration of Nginx and PHP
Steps:
- Open configuration file by:
sudo nano /etc/nginx/sites-available/default
- Switch the location block with following passage
# old location block
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# new location block
location / {
index index.html index.htm index.php default.html default.htm default.php;
}
location ~\.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
- Insert
index.php
into server_name parameter as described in configuration file - Reload Nginx configuration and restart Nginx
sudo /etc/init.d/nginx reload
sudo service nginx restart
Notice:
- All web docs should be stored at
/var/www/html
- So far your website can be visited by
IP/correct path
3. Site specific configuration
This part configure the port, root and path of specific site.
Steps:
- Create a new configuration file as following example and move it to
/etc/nginx/sites-available/
server{
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
try_files $uri/ = 404;
}
location ~ \.php${
include snippets/fastcgi-php.conf;
fastcgi_pass unix: /var/run/php/php7.0-fpm.sock;
}
location ~ /\.ht{
deny all;
}
}
- Enable your new server block by creating a symbolic link from your new server block configuration file (in the /etc/nginx/sites-available/ directory) to the /etc/nginx/sites-enabled/ directory, unlink default configuration file. (From official guide)
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
- Test and deploy configuration by:
sudo nginx -t
sudo systemctl reload nginx
Notice:
- Each site should has its own configuration file in
/etc/nginx/sites-enable
, in this way it's easier to manage multiple site configurations in one web server. - Refer to
/var/log/nginx/error.log
if any error occurs.