From ElectroDragon
Jump to: navigation, search


Type PHP -
Debian 9/8 5.6 -
Ubuntu 17/16 7 -
  • Reference from here LEMP


  • apt-get install nginx
  • allow gfw -> sudo ufw allow 'Nginx HTTP'
  • Test Welcome to Nginx.png


  • service nginx start
  • check start status -> systemctl status nginx.service
  • systemctl restart nginx


Steps Header text Header text
Nginx config /etc/nginx/nginx.conf
Default site config /var/www/html -> defined at /etc/nginx/sites-enabled/default -
  • create file e.g. /etc/ningx/sites-enabled/
  • Optimize -> delete other conf file? FastCGI cache?, gzip missing?


NGINX Config

server {
        listen 80;
        listen [::]:80;
	root /usr/share/nginx/www;  # root of user file
	index index.html index.htm; # file name
	server_name localhost; # e.g.
	location / {
		try_files $uri $uri/ /index.html;
	location /doc/ {
		alias /usr/share/doc/;
		autoindex on;
		deny all;

PHP Configure

use this latest config

  • File at /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name your_server_ip;
    location / {
        try_files $uri $uri/ =404;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    location ~ /\.ht {
        deny all;


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    server_name server_domain_name_or_IP;
    location / {
        try_files $uri $uri/ =404;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    location ~ \.php$ {
        try_files $uri =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;

Let's copy the follow code into the default to enable php, compare to the code at Nginx on pcduino, it's php-enabled, you can read it on this part "location ~ \.php$".

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.php index.html index.htm;
        # Make site accessible from http://localhost/
        server_name _;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                deny all;
        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index index.php;
                include fastcgi_params;
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;

And now when we navigate to (or info.php), we will see the info like below:

  • Create PHP test file -> sudo nano /usr/share/nginx/html/info.php


We're going to start moving about in Linux now. Remember that Linux still has a folder structure - just like any graphical operating system. If you want to learn more about the folder structure of a Linux system - there are some great tutorials and images that can be found through a quick Google search.

Let's move to where nginx stores the sites which it hosts, by using the command:

cd /etc/nginx/sites-enabled 

or try at /etc/nginx/sites-available

And let's edit the file "default", delete everything in it, and copy the following code into it, you can do this with editor like nano or GUI editor:

server { 
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /usr/share/www/example;
index index.php index.html index.htm;

location / {
error_page 404 = 404.html;

# Make site accessible from http://localhost/
server_name *;

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow ::1;
deny all;

Build the website

Now, we're actually going to make a little website in the /usr/share/nginx/www/example folder we made earlier.

Let's move to the folder by using the command:

cd /usr/share/nginx/www/example 

We're going to make two files - an index page (homepage), and a 404 page (a page to display if the page a user is trying to access doesn't exist).

Let's first make the index page by using the command:

pico index.html 

If you're familiar with html, at this point type in whatever you want. If you're not, then paste in the following, save and exit (as you did earlier with the nginx configuration file):

Hello world, this is my first website!

Next, do the same for the 404 page by using the command:

pico 404.html 

Paste in the following if unsure:

Sorry, the page you are looking for doesn't exist!

The final step is to make sure that our pages are readable for others by changing the permissions on the files. If we didn't change the permissions, someone accessing your website may be given the 'You are not allowed to view this page' (or equivalent) error generated by nginx.

Use the command:

chmod -R 775 /usr/share/nginx/www/example

Detailed Configuring nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

vi /etc/nginx/nginx.conf

The configuration is easy to understand (you can learn more about it here: and here:

First (this is optional) adjust the number of worker processes and set the keepalive_timeout to a reasonable value:

worker_processes  4;
    keepalive_timeout   2;


  • basedir
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/data/tmp/php/upload/:/proc/:/";
  • rewrite root to /k, add line in location / { }
#return 301 /k;


  • Edit sources.list due to licensing issues
  • /etc/apt/sources.list -> add contrib non-free at end of each line
deb jessie main contrib non-free
deb-src jessie main contrib non-free
deb jessie/updates main contrib non-free
deb-src jessie/updates main contrib non-free
# jessie-updates, previously known as 'volatile'
deb jessie-updates main contrib non-free
deb-src jessie-updates main contrib non-free
  • sudo apt-get update
  • sudo apt-get install php5-fpm php5-mysql
  • sudo nano /etc/php5/fpm/php.ini -> cgi.fix_pathinfo=0


  • apt-get install php5-cgi php5-cli php5-mysql
  • apt-get install php5-fpm php5-mysql
  • latest php version php7


  • service php5-fpm restart
  • sudo systemctl restart php5-fpm


  • sudo apt-get install mysql-server
  • sudo mysql_secure_installation -> after set root pass, all the rest set to Y


  • apt-get install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as, so we don't have to specify a MySQL root password manually later on.

Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Xcache is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and APC. It is strongly recommended to have one of these installed to speed up your PHP page.

Xcache can be installed as follows:

apt-get install php5-xcache

and let's restart the php by doing:

service php5-fpm restart

Now we should see many extra modules you installed in the phpinfo.php, including the mysql.




  • Debug -> vim /etc/php5/fpm/pool.d/www.confMake necessary changes to match following values:
slowlog = /var/log/php5/slow.log
request_slowlog_timeout = 10s

MYSQL debug

  • enter into shell -> mysql -u root -p
  • Enable log -> set global slow_query_log = 'ON';
  • Set log address -> set global slow_query_log_file ='/var/log/mysql/slow-query.log';

the rest debug

  • Use wordpress plugin Query Monitor
  • Use WP Debug
 * For developers: WordPress debugging mode.
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
define('WP_DEBUG', true);
define('SAVEQUERIES', true);


  • ufw app list


  • ngxtop


  • Gitlab
  • Seafile


Webfiles side

Header text Header text Header text
1. create config file
  • nano /etc/rsyncd.conf
2. Config
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
path = /backup/
comment = backup file
ignore errors 
read only = no
write only = no
hosts allow = *
hosts deny =
list = false
uid = root
gid = root
auth users = backup
secrets file = /etc/server.pass
  • backup user name backup
  • backup pass at /etc/server.pass
  • backup module [bk_files], inside folder is /backup/
3. create secrete files
  • nano pass
  • backup:password
  • chmod 666 pass
  • maybe restart rsync --daemon
  • backup:password, user name and pass

Backup side

Steps Header text Header text
1. create secrete file
  • nano server.pass, and then type pass
  • chmod 600 server.pass
2. Main command
  • rsync -azvi --delete --progress backup@ /backup/ --password-file=/backup/
  • rsync -azvi --delete --progress backup@ /backup/ --password-file=/backup/
3. Note config
  • -azvi
  • --delete delete file on this side
  • --progress show transfer progress
  • --exclude "*access*" --exclude "debug", exclude files
  • backup is rsync user name
  • remote server
  • bk_files is backup module
  • /backup/ local folder
  • --password-file=/etc/server.pass, use pass
4. setup cronteb -e
  • 00 01 * * * rsync -azvi --delete --progress backup@ /backup/ --password-file=/backup/server.pass




  • php库缺失 curl -> apt-get install php5-curl
  • html// 不可写 -> chmod 777 html (folder)
  • 须开启php GD库,否则验证码、缩略图使用将不正常 -> apt-get install php5-gd


Type alexa - prerequisite
vanilla 72K github - 1.7K PHP7
Carbon forum 687K github - 1.25K
phpbb 42K free
nodebb 189K github
smf 69K free
mybb 68K free
woltlab 115K free
discourse 35K github 2GB RAM min.
flarum 119K beta version now
fluxbb 661K non-free
IPS Board 44K non-free
xenforo 39K non-free
vbulletin 67K non-free


Reference tutorial