Logicalwebhost Cheatsheet

Linux & Open Source Cheatsheets & Howto's

Skip to: Content | Sidebar | Footer

nginx howto

Nginx is a small web server that can also be used as a proxy/load-balancer to optimize a system for speed. Here we’ll install nginx from Debian repositories and then get it to interface with php5 and mysql, which you have to do (for now) with a cgi handler. This will hopefully be fixed in future packages, but this will get you up and running for now.

Install/configure nginx

su (or sudo, your choice)
apt-get install nginx php5-fpm php5-mysql php5-curl mysql-server
cd /etc/nginx
vi /var/www/html/index.nginx-debian.html (change it to whatever you want)

Now visit http://your.servername.com and you should see the default page. Now let’s make it serve php. To do this you uncomment some stuff in the default config to make fpm handle php for you, and change your index.whatever to index.php too like:

vi /etc/nginx/sites-available/default
  index index.php index.htm; <-- add index.php
  location ~ \.php$ {  <-- uncomment
  include snippets/fastcgi-php.conf;  <-- uncomment
  # With php5-fpm:
  fastcgi_pass unix:/var/run/php5-fpm.sock;  <-- uncomment
  }  <-- uncomment

You can test your config by doing:

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now let’s add a virtual host of whatever.com (obviously change it to your real server name, not whatever.com). You just copy the last few lines of /etc/nginx/sites-available/default, or just copy what’s below (this will emulate mod_rewrite/php5, like if you have a wordpress site):

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/whatever
vi /etc/nginx/sites-available/whatever
  server {
    listen 80;
    server_name .whatever.com;
    error_log /var/www/whatever.com/logs/whatever.com.error.log;
    access_log /var/www/whatever.com/logs/whatever.com.access.log;
    root /var/www/whatever.com/www;
    index index.php;
    location / {
	try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
  }
ln -s /etc/nginx/sites-available/whatever /etc/nginx/sites-enabled/whatever
mkdir /var/www/whatever.com
cd /var/www/whatever.com
mkdir logs www
touch logs/whatever.com.error.log
touch logs/whatever.com.access.log
chown -R www-data.www-data /var/www/whatever.com/
vi /var/www/whatever.com/index.php
  test page
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/etc/init.d/nginx/restart

Now go visit your new virtual host at http://whatever.servername.com, you should see just the text “test page” in your browser

site alias

This is if you want a site like http://your.serv.er/tacos that points to someplace else than http://you.ser.ver/

vi /etc/nginx/sites-available/default
  location /tacos {
 
      alias /var/www/html/tacos;
      #try_files $uri $uri/;
      location ~ \.php$ {
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          include fastcgi_params;                       
          fastcgi_param SCRIPT_FILENAME $request_filename;
          #fastcgi_intercept_errors on;
      }
}

phpmyadmin

apt-get install phpmyadmin <-- ignore prompts to set up apache/lighttp, but set up db
ln -s /usr/share/phpmyadmin/ /var/www/html/phpmyadmin
/etc/init.d/nginx reload

Now visit phpmyadmin in a browser like:

http://what.ever.ip/phpmyadmin

Nginx errors

Here’s how to fix some common errors:

Nginx Error – 413 Request Entity Too Large

Edit your nginx.conf or individual host in /etc/nginx/sites-available/whatever.conf:

 http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
 
    server {
        client_max_body_size 20M; <-- add this line there somewhere
        listen       80;
        server_name  localhost;
 
        # Main location
        location / {
            proxy_pass         http://127.0.0.1:8000/;
        }
    }
}

phpmyadmin

when you install phpmyadmin, ignore the pre-configuration for apache/lighttpd, you will set up that part later:

apt-get install phpmyadmin
php5enmod mcrypt

Now set up the default url by adding this near the end of the /etc/nginx/sites-available/default:

vi /etc/nginx/sites-available/default
  location /pmadmin/ {
                alias /usr/share/phpmyadmin/;
 
                index  index.html index.htm index.php;
 
                location ~ ^/pmadmin(.+\.php)$ {
                        alias /usr/share/phpmyadmin$1;
                        fastcgi_pass unix:/var/run/php5-fpm.sock; #OR fastcgi_pass 127.0.0.1:9000;
                        fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin$1;
                        include fastcgi_params;
                        fastcgi_intercept_errors        on;
        }

Or in Stretch like:

vi /etc/nginx/sites-available/default
  location /pmadmin/ {
                alias /usr/share/phpmyadmin/;
 
                index  index.html index.htm index.php;
 
                location ~ ^/pmadmin(.+\.php)$ {
                        alias /usr/share/phpmyadmin$1;
                        fastcgi_pass unix:/var/run/php/php7-fpm.sock; #OR fastcgi_pass 127.0.0.1:9000;
                        fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin$1;
                        include fastcgi_params;
                        fastcgi_intercept_errors        on;
        }

Now test your nginx config for errors and reload it like:

nginx -t
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
service nginx reload

now test it by visiting http:server.name.or.ip/pmadmin

Write a comment

You need to login to post comments!