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

phpmyadmin

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

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/;
        }
    }
}

Write a comment

You need to login to post comments!