Tag Archives: nginx

WordPress HTTP error on image upload (Nginx)

HTTP Error during image upload on wordpress

HTTP Error during image upload on wordpress

Recently, I’ve encountered this kind of problem. Maybe because it has been awhile I’m not uploading any images to my post. :p

Anyway, if you came out with this kind of error and using Nginx as your web engine, here how to solve it.

Add:

to your nginx .conf file.

For example, mine looks something like this:

Hope it helps. 🙂

Credit to aaronjholbrook

Extract unique IP address from Apache & Nginx log file

Lets say you wanted to count the number of unique IP addresses hitting your Apache server. It’s very easy to do in a Linux (or compatible) shell. In this tutorial, I’m using Ubuntu server.

First, locate the log file that you want to extract. For example, apache2 log file is located at /var/log/apache2 (depending on your distro). For nginx, the log file is located at /var/log/nginx.

Here I give you the first example on how to extract & count unique IP address in Nginx log file.

Nginx Access Log file

Nginx Error Log file

Next, is the step on how to extract & count unique IP address from Apache log file.
Apache access & error log file

Apache Access Log file

Apache Error Log file

If you have any other step, you can share with me in the comment section. Hope it helps! 🙂

WordPress permalinks with Nginx

If you are on Apache(mod_rewrite), WordPress will automatically add the required rewrite rules to your .htaccess file for permalinks to work. Just add code generated by WordPress in .htaccess, and its done. But for nginx, you have to add the rules manually in the nginx conf file.

But the problem is when WordPress detects that mod_rewrite is not loaded (which is this case, nginx), it falls back to using PATHINFO permalinks, which inserts an extra ‘index.php’ in front. This will cause the problem for me as I want to remove the index.php from the permalinks.

Permalink Setting

So you need to edit your nginx configuration file to make the permalinks work. We will use the try_files directive to pass URLs to WordPress’s index.php for them to be internally handled.

If your blog is at the root of the domain (something like www.blog.com), find the location / block inside the configuration file, and add the following line to it.
[shell]try_files $uri $uri/ /index.php?q=$uri&$args;[/shell]

Here, Nginx checks for the existence of a file at the URL ($uri), then for a directory ($uri/). If it doesn’t find a directory or a file, it performs an internal redirect to /index.php passing the URL as PATHINFO.

It should look like this after the edits :
[shell]
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
[/shell]

If your blog is in a subfolder (lets say /blog), you’ll have to add an extra location /blog/ block to your configuration file :
[shell]
location /blog/ {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
[/shell]

After you have finished making the changes in the configuration file, reload the nginx configuration:
[shell]nginx -s reload[/shell]

The permalinks should be working fine now.

Install DenyHosts on Ubuntu 12.04

DenyHosts is a security tool written in python that monitors server access logs to prevent brute force attacks on a virtual private server. The program works by banning IP addresses that exceed a certain number of failed login attempts.

1. Install DenyHosts via apt
[shell]sudo apt-get install denyhosts[/shell]

2. Whitelist you IP Addresses
After you install DenyHosts, make sure to whitelist your own IP address. Skipping this step will put you at risk of locking yourself out of your own machine.

To insert you IP, open this file:
[shell]sudo nano /etc/hosts.allow[/shell]

under the description, add in any IP addresses e.g. your IP so that you will not kicked out from the server. You can write each one on a separate line, using this format:
[shell]
sshd: 12.34.45.678
sshd: 89.76.54.321
[/shell]

After making any changes, be sure to restart DenyHosts so that the new settings take effect on your virtual private server:
[shell]sudo /etc/init.d/denyhosts restart[/shell]

3. Configure DenyHosts (Optional)
If you want to customize the behavior of DenyHosts on your VPS, you can make the changes within the DenyHost configuration file:
[shell]sudo nano /etc/denyhosts.conf[/shell]

Create subdomain on Nginx + Bind9 on Ubuntu server 12.04

1. Edit you bind & dns config
Insert this to db.domain.net

blog    IN    CNAME    domain.net.

2. Edit /etc/nginx/sites-available/default
Add this:

server {
    listen 80;
    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    server_name domain.net;
[…]
}

server {
    listen 80;
    root /usr/share/nginx/www/blog;              <-- change this if you want
    index index.php index.html index.htm;

    server_name blog.domain.net;
[…]
}

3. Create new folder at /usr/share/nginx/www/blog

4. Put your file accordingly in the folder

5. Test open the URL e.g. blog.domain.net

Installing Owncloud on Nginx (Ubuntu 12.04)

Here I want to share with you on how to create your own sync server like dropbox.
The software that we will use is ownCloud.

But before we start, please ensure that you already have the up & running nginx.
For tutorial, please refer here.

1. Make sure your OS is updated

apt-get update
apt-get upgrade

2. Install required package

apt-get install php5-cgi autoconf automake autotools-dev curl libapr1 libtool curl libcurl4-openssl-dev php-pear php-xml-parser php5 php5-cli php5-common php5-curl php5-dev php5-gd php5-sqlite php5-fpm

3. Edit nginx site config file file

vi /etc/nginx/sites-available/default

4. Edit like this;
Copy line below;

server {
        listen   80;
        root /usr/share/nginx/www;
        index index.php index.html index.htm;
        server_name _;

        location / {
                try_files $uri $uri/ @webdav;
        }

        location ~ ^/(data|config|.ht|db_structure.xml|README) {
                deny all;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }

        location ~ .*.(php|php5)?$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass unix:/tmp/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        location ~ ^/owncloud/remote.php(/.*)$ {
                fastcgi_split_path_info ^(.+.php)(/.*)$;
                fastcgi_pass unix:/tmp/php5-fpm.sock;
                include fastcgi_params;
        }

        location @webdav {
                fastcgi_split_path_info ^(.+.php)(/.*)$;
                fastcgi_pass unix:/tmp/php5-cgi.sock;
                include fastcgi_params;
        }

        location ~ /.ht {
                deny all;
        }
}

The “root /usr/share/nginx/www” line defines the root directory for nginx.
You can change it to other path you like.

5. Edit /etc/php5/conf.d/xcache.ini
Edit this two line;

xcache.size = 64M
xcache.var_size = 64M

6. Edit /etc/php5/fpm/php.ini
Edit this two line;

post_max_size = 2G
max_upload_size = 2G

7. Reload php5-fpm

/etc/init.d/php5-fpm reload

8. Get latest owncloud file (Latest 4.5 (4.5.5))

wget http://mirrors.owncloud.org/releases/owncloud-4.5.5.tar.bz2

9. Extract it at /usr/share/nginx/www

tar xvf owncloud-4.5.5.tar.bz2

So the path will be /usr/share/nginx/www/owncloud after extracted & the URL will be http://localhost/owncloud

10. Set the directory permissions;

chown -R www-data:www-data /usr/share/nginx/www

Replace ‘/usr/share/nginx/www‘ with your own path you prefered. (The path must be the same with the one in /etc/nginx/sites-available/default)

11. Go to http://localhost/owncloud or http:///owncloud
Create new admin account. Also please unsure that the path on data folder is correct.

12. Create new folder name “clientsync“, something like this;

 
13. Get Desktop Sync Clients at here http://owncloud.org/sync-clients/
Install it. Put your credential accordingly.

14. If everything fines, then your owncloud has been configured properly. Congratz!

Done. Hope it helps. 🙂

Installing Nginx, MySQL & PHP5 (PHP5-FPM) on Ubuntu 12.04 LTS

Installing Nginx, MySQL & PHP5 (And PHP5-FPM) a.k.a (LEMP) On Ubuntu 12.04 LTS

1. Update & upgrade your OS first;

apt-get update
apt-get upgrade

  1. Install Myssql & PHP5-FPM;

apt-get install mysql-server mysql-client php5-fpm

  1. Install Nginx webserver

apt-get install nginx

  1. Start your Nginx!

/etc/init.d/nginx start

or

service nginx start

Try access your web via localhost or via ip
e.g. 127.0.0.1, 192.168.1.1

5. Edit nginx config file;

vi /etc/nginx/nginx.conf

Adjust worker proc & keepalive accordingly;

[…]
    worker_processes    4;
[…]
    keepalive_timeout    2;
[…]

  1. Edit the virtual host config;

vi /etc/nginx/sites-available/default

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

        root /usr/share/nginx/www; # you can change to any path you want
        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;
                allow 127.0.0.1;
                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 127.0.0.1:9000
        #
        location ~ .php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                #fastcgi_pass fastcgi_pass 127.0.0.1:9000;
                fastcgi_pass unix:/tmp/php5-fpm.sock;
                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;
        }
}
[…]

  1. Reload the config file

/etc/init.d/nginx reload

  1. Install nessary file

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 php5-xcache

  1. Now reload PHP-FPM:

/etc/init.d/php5-fpm reload

  1. Test your nginx;

vi /usr/share/nginx/www/info.php

Paste this line below;

phpinfo();
?>

  1. Try access that file via web browser
    http:///info.php

    It will show info include mysql module

    Making PHP-FPM Use A Unix Socket
    By default PHP-FPM is listening on port 9000 on 127.0.0.1. It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php5/fpm/pool.d/www.conf.

    12. Edit file

vi /etc/php5/fpm/pool.d/www.conf

… and make the listen line look as follows:

[…]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[…]

  1. Then reload PHP-FPM:

/etc/init.d/php5-fpm reload

Done!. Hope it helps. 🙂

Nginx – 413 Request Entity Too Large

Haa.. Seperti tajuk diatas, bagi sape2 yang pernah configure & run server Nginx ni, korang pernah kena macam ni tak?
Benda ni jadi masa aku nak uploadkan theme kat wordpress.. Nak install theme la..
Saiz theme tu dalam 1.2MB.. Sekali keluar error macam tu..
So, dicari2 solutionnya.. Jumpa jugak..

Mari kita tengok macam mana solution dia..
First, kita kena edit file configuration nginx ni.. Kita taip ni:

Masukkan password korang, dan nanti keluarlah configuration dia kat terminal tu..
Lepas tu kita kena masukkan line ni..

Function dia untuk menetapkan jumlah memori untuk server tu..
Letakkan line tadi tu kat sini..

http {
include /etc/nginx/mime.types;

access_log /var/log/nginx/access.log;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 2;
tcp_nodelay on;

gzip on;
gzip_disable “MSIE [1-6].(?!.*SV1)”;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

client_max_body_size 4M;
client_body_buffer_size 128k;
}

Haa.. Nampak tak? Lepas tu korang tekan CTRL-O untuk write-out dan save..
Nak exit tekan CTRL-Z.. Lepas tu korang try restart nginx korang.. taip kan:

Kalau tak ada masalah apa2, dia akan restart dengan elok.. Lepas tu try korang upload theme tu balik..
Insyallah mesti boleh.. Sebab aku da buat & da jadi pun! 😀
Apa2 pertanyaan bolehlah tanya aku.. Insyallah aku cuba tolong korang..