Sebastian Mogilowskis Blog

Just another blog about administration, linux and other stuff

Language: German English

Nginx as reverse proxy cache for wordpress and apache

1. Install Nginx

* Install from PPA
* Install using self build packages

2. General Nginx Settings

Edit “/etc/nginx/nginx.conf” and modify or insert the following settings:

[...]
http {
  [...]
  # Gzip Settings
  gzip on;
  gzip_disable "msie6";
  gzip_buffers 32 8k;
  gzip_comp_level   6;
  gzip_http_version 1.0;
  gzip_min_length   0;
  gzip_types        text/html text/css image/x-icon application/x-javascript application/javascript text/javascript application/atom+xml application/xml ;

  # Proxy Settings
  proxy_temp_path /var/lib/nginx/proxy;
  proxy_connect_timeout 30;
  proxy_read_timeout 120;
  proxy_send_timeout 120;
  proxy_cache_key "$scheme://$host$request_uri";
  [...]
}
[...]

3. Create Site

Create “/etc/nginx/sites-available/www.mogilowski.net” with following content:

Note: Replace “www.mogilowski.net” with your site and replace “mogfilecache” with a unique name for the filecache for this site.

proxy_cache_path  /var/lib/nginx/cache  levels=1:2   keys_zone=mogfilecache:180m  max_size=500m;

server {
        proxy_cache_valid 200 3h;

        listen 80;

        server_name www.mogilowski.net;

        access_log /var/log/nginx/mogilowski.proxied.log;
        error_log /var/log/nginx/mogilowski.proxied.log crit;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
            # If logged in, don't cache.
            if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
                set $do_not_cache 1;
            }
            proxy_cache_key "$scheme://$host$request_uri $do_not_cache";
            proxy_cache mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        location ~* wp\-.*\.php|wp\-admin {
            proxy_pass http://127.0.0.1:81;
        }

	location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
            proxy_cache_valid 200 6h;
            expires max;
            proxy_cache mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        location ~* \/[^\/]+\/(feed|\.xml)\/? {
            if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
                set $do_not_cache 1;
            }
            proxy_cache_key "$scheme://$host$request_uri $do_not_cache";
            proxy_cache_valid 200 1h;
            proxy_cache mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        # No access to .htaccess files.
        location ~ /\.ht {
            deny  all;
        }

}

Note: In this example configuration the apache webserver runs on port 81 on the localhost. But it can also be a different machine on the internet. Just use “http://YOUR_APACHE_SERVER:80″ instead.

4. Enable site

ln -s /etc/nginx/sites-available/www.mogilowski.net /etc/nginx/sites-enabled/www.mogilowski.net

Note: Please remove the default site from sites-enabled.

5. Purge Cache (optional)

If you have build your own nginx package with the “proxy_cache_purge” module, you can add this to your server config. (http://labs.frickle.com/nginx_ngx_cache_purge/)

server {
    [...]
    location ~ /purge(/.*) {
                allow                   127.0.0.1;
                deny                    all;
                proxy_cache_purge       mogfilecache   "$scheme://$host$request_uri";
        }
    [...]
}

You can use this together with this plugin: http://wordpress.org/extend/plugins/nginx-proxy-cache-purge

6. Links

* http://wp-performance.com/2010/10/nginx-reverse-proxy-cache-wordpress-apache/
* http://wiki.nginx.org/Wordpress
* http://wordpress.org/extend/plugins/nginx-proxy-cache-integrator/
* http://www.djm.org.uk/wordpress-nginx-reverse-proxy-caching-setup/
* http://zachbrowne.com/2011/how-to-build-the-fastest-wordpress-website-with-apache-ngnix-reverse-proxy-php-cgi-google-pagespeed/

, , , , ,

Install nginx on debian squeeze from PPA

You find currently nginx version 0.7.67-3 in Debian Squeeze. If you want the latest version you have to build your own paket or you can use one from the ubuntu ppa.

This installs the latest stable relase of nginx webserver (current 1.0.1) on Debian Squeeze.

echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-stable-lucid.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
aptitude update
aptitude install nginx

But if you need some third party modules, you have to build your own packages.

, , ,

Building a nginx debian package with third party modules

I created this manual on Debian Squeeze (amd64) with the latest version of nginx (stable) an one third party module as example.
If you want to build your own package, check for the latest versions of the modules and nginx and modify the version numbers in this manual.

1. Install build debian tools and depending packages

aptitude install build-essential dpkg-dev debhelper autotools-dev libgeoip-dev libssl-dev libpcre3-dev zlib1g-dev

2. Download latest nginx and unpack the tarball

wget http://nginx.org/download/nginx-1.0.2.tar.gz
tar xvzf nginx-1.0.2.tar.gz

3. Download additional modules

I use the nginx purge module in this example. Download the module

wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz

and create a “module” directory if it not exists and untar the module to this directory:

mkdir nginx-1.0.2/modules
tar vfx ngx_cache_purge-1.3.tar.gz -C nginx-1.0.2/modules/

4. Get latest debian package

apt-get source nginx

You get a directory with the latest nginx source (nginx-0.7.67). We only need the “debian” folder.

5. Copy the debian folder to the new nginx folder

cp -r nginx-0.7.67/debian/ nginx-1.0.2
rm nginx-1.0.2/debian/patches/*

Don’t forget to remove the old patches !

Edit “nginx-1.0.2/debian/changelog” and prepend:

nginx (1.0.2-1) unstable; urgency=low

  * added purge proxy module

 -- Sebastian Mogilowski   Sat, 14 May 2011 13:00:00 +0100

Note: Replace my name with your own name :-)

Add the module to the build rules edit “nginx-1.0.2/debian/rules” and add:

--add-module=$(CURDIR)/modules/ngx_cache_purge-1.3 \

to the “./configure” setting.

Example:

./configure --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --http-client-body-temp-path=/var/lib/nginx/body \
            --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
            --http-log-path=/var/log/nginx/access.log \
            --http-proxy-temp-path=/var/lib/nginx/proxy \
            --lock-path=/var/lock/nginx.lock \
            --pid-path=/var/run/nginx.pid \
            --with-debug \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_geoip_module \
            --with-http_gzip_static_module \
            --with-http_realip_module \
            --with-http_stub_status_module \
            --with-http_ssl_module \
            --with-http_sub_module \
            --with-ipv6 \
            --with-mail \
            --with-mail_ssl_module \
            --add-module=$(CURDIR)/modules/ngx_cache_purge-1.3 \
            $(CONFIGURE_OPTS)

6. Build the package

cd nginx-1.0.2
dpkg-buildpackage -b

7. Install

dpkg -i nginx_1.0.2-1_amd64.deb

, ,

Nagios 3 with Nginx on Ubuntu Server

1. Install Nagios 3

aptitude install nagios3

2. Test Nagios Installation

Open “http://SERVER/nagios3/” in your browser. Login with user “nagiosadmin” and the passwort you have entered during the installation.

3. Switch from Apache to Nginx

Nagios has dependencies to the Apache webserver. So Apache was installed with Nagios. Now we replace it with Nginx.

3.1 Install Nginx and libfcgi (Perl)
aptitude install nginx libfcgi-perl
3.2 Configrate Fastcgi
wget https://library.linode.com/web-servers/nginx/perl-fastcgi/reference/fastcgi-wrapper
wget https://library.linode.com/web-servers/nginx/perl-fastcgi/reference/init-deb.sh
mv fastcgi-wrapper /usr/bin/fastcgi-wrapper.pl
mv init-deb.sh /etc/init.d/perl-fastcgi
chmod +x /usr/bin/fastcgi-wrapper.pl
chmod +x /etc/init.d/perl-fastcgi
update-rc.d perl-fastcgi defaults
/etc/init.d/perl-fastcgi start
3.3. Configurate Nginx
vim /etc/nginx/sites-available/nagios
server {
	listen   80;
	server_name  servername.com;

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

	auth_basic            "Restricted Nagios Area!";
  	auth_basic_user_file  /etc/nagios3/htpasswd.users;	

	location / {
		root /usr/share/nagios3/htdocs;
		index index.html;

		rewrite ^/nagios3/(.*)$ /$1 break;
	}

	location /nagios3/stylesheets {
                alias /etc/nagios3/stylesheets;
        }

	location ~ \.cgi$ {
		root /usr/lib/cgi-bin/nagios3;
		include /etc/nginx/fastcgi_params;

		rewrite ^/cgi-bin/nagios3/(.*)$ /$1;

		fastcgi_pass 127.0.0.1:8999;
		fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/nagios3$fastcgi_script_name;
		fastcgi_param AUTH_USER       $remote_user;
		fastcgi_param REMOTE_USER     $remote_user;
	}

}
ln -s /etc/nginx/sites-available/nagios /etc/nginx/sites-enabled/nagios
/etc/init.d/nginx restart
3.4 Remove Apache

Now you can remove all apache pakets. Exept “apache2-utils” this paket is required by nagios3.

4. Test with Nginx

Open “http://SERVER/nagios3/” in your browser. Login with user “nagiosadmin” and the passwort you have entered during the installation.

5. Links

* http://inode.co.nz/running-nagios3-under-nginx-fastcgi

* http://library.linode.com/web-servers/nginx/perl-fastcgi/ubuntu-10.04-lucid

* http://wiki.linuxwall.info/doku.php/fr:ressources:dossiers:supervision:nagios3

* http://wiki.nginx.org/Main

* http://www.nagios.org

, ,

Install Gnome3 on Ubuntu 11.04 Natty or Ubuntu 10.10 Maverick

Run the following commands in your terminal:

On Ubuntu 11.04:

add-apt-repository ppa:gnome3-team/gnome3
aptitude update
aptitude dist-upgrade
aptitude install gnome-shell

On Ubuntu 10.10:

add-apt-repository ppa:ubuntu-desktop/gnome3-builds
aptitude update
aptitude install gnome3-session

Note: This repository is outdated !

Lock out and back in, after completing the installation. (Choose Gnome3)

Remove:

aptitude install ppa-purge
ppa-purge ppa:gnome3-team/gnome3

Links:

* http://www.gnome.org

* http://www.ubuntu.com

, ,

Enable SNMP on Citrix XenServer 5.6

Open XenServer Console or open a SSH connection to your XenServer for this howto:

1. Firewall Settings

Open “/etc/sysconfig/iptables” with “vi” editor and add the following line to allow SNMP connections:

-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 161 -j ACCEPT

Restart iptables service:

service iptables restart

2. Configurate SNMPD

Edit “/etc/snmp/snmpd.conf” and insert the following lines:

[...]
com2sec MY-TRUST-NET 192.168.0.0/24      public
[...]

[...]
group   notConfigGroup v1            MY-TRUST-NET
[...]

Note: Replace “192.168.0.0/24″ with your Subnet.

Now start the SNMP-Deamon with:

service snmpd start

To start SNMP automatic on system startup execute the following command:

chkconfig snmpd on

Note: This settings are not persistant. If you upgrade your XenServer you have to make this settings again.

3. Links

* http://support.citrix.com/article/CTX122337
* HP SNMP Agents for Citrix XenServer

, , ,

Ubuntu Lucid on XenServer 5.6 FP1

Describes howto install Ubuntu Lucid 10.04 LTS (32-Bit or 64-Bit) on a Citrix XenServer 5.6 FP1 and convert the virtual machine into a template.

Note: Click to enlarge a picture.

1. Create a new virtual machine

Select template “Ubuntu Lucid Lynx 10.04 (64-bit)” and choose name and description for your virtual machine (or 32-Bit if necessary). Select “Install from URL” and insert the URL http://archive.ubuntu.net/ubuntu

xl1xl2xl3

Specify the number of vCPUs and the memory for your virtual machine and create the virtual disks.

xl4xl5

I choose a 8 GB Disk for the system and 1 GB for the swap disk.
By using 2 different disks i can delete the swap partition if i don’t need it or can replace disks with bigger ones.

At least add network interfaces to your VM. This depends on your XenServer network setup. Choose a bonding or single interface here for example and finish the virtual machine wizzard.

XenServer now builds your virtual machine and start it.

If the virtual machine is ready open the console to the virtual machine and start the installation of Ubuntu:

xl6xl7xl8

Choose your country and keyboard layout and configurate the network interface.
If you don’t have a DHCP Server in your network you choose “manual” and configurate the network by hand.

xl9xl10xl11xl12

Enter the hostname and domain name. In this example, my system is called xen.local, so I enter xen and local:

xl13xl14

Choose a mirror of the Ubuntu archive.
Please check if the installer detected your time zone correctly. If so, select Yes, otherwise No:

xl15xl16

Partitioning

Now you have to partition your hard disks.
First select “manual” and select the first virtual disk “xvda”. Create a new empty partition table and select the free disk space to create a new partition.
Use this partition as “Ext4 journaling file system” and as “/” mount point.

xl17xl18xl19xl20xl21

xl22xl23xl24







Now repeat all with “xvdb” but now create a “swap area”.

xl26xl27xl28xl29

The partitioning is totally up to you – if you know what you’re doing, you can also set up different partitions.
At least “Finish partitioning” and select “Yes” when you are asked “Write the changes to disks”. Now the new partitions are being created and formatted.

Afterwards the base system is being installed:
xl30

Create a user, for example the user “sebastian” with the user name “sebastian”:

xl31xl32xl33xl34

Note: I don’t need an encrypted private directory, so I choose No here.

Next the package manager apt gets configured. Leave the HTTP proxy line empty unless you’re using a proxy server to connect to the Internet.
I select “No automatic updates”. Of course, it’s up to you what you select here. But on a server i usually want to install the updates by myself.
Make your choce about automatic updates. Now the installer installs the system and download some essential packets. Make a small software selecten. I choose “Basic Ubuntu server” and “OpenSSH server” to get a small virtual machine.

xl35xl36xl37xl38

Now the GRUB boot loader gets installed. Select “Yes” when you are asked “Install the GRUB boot loader to the master boot record?”.

xl39xl40xl42

The base system installation is now finished. Hit “Continue” to reboot the system.

2. Install XEN Tools

In XenCenter, attach “xs-tools.iso” to the DVD drive of the VM.
Mount “xs-tools.iso”, install the correct XenServer Tools package (replace amd64 with i386 if necessary) and unmount “xs-tools.iso”.

mount /dev/cdrom /mnt
dpkg -i /mnt/Linux/xe-guest-utilities_5.6.100-647_amd64.deb
umount /mnt/

At least reboot the VM. Make sure the services run at boot time. If not try this:

update-rc.d -f xe-linux-distribution remove
update-rc.d xe-linux-distribution defaults

Reboot the VM for the last time.

3. Convert to template

Install all software on your virtual machine you need on all virtual machines and make all settings you need.

After you have made all your changes and settings shut down the virtual machine.

“Right click” on the virtual machine in your inventory and select “Convert to Template”

Now you can create new virtual machines with a basic setup for your environment.

Select “New VM from template” and choose your custom template.

, , , ,

Resize images (folder) with imagemagick (mogrify)

Before upload pictures to my online gallery i want to resize them to a smaller size. I don’t want to open all with a tool or something. I want to run a small script on my netbook without GUI.

First install imagemagick:

aptitude install imagemagick

Now “cd” into the directory with your images and execute:

mogrify -resize 800x600! *.JPG

All of the images will be replaced with resized version of themselves.

Now i can upload the directory to my gallery.

, , , ,

Change DNS-Server in XenServer 5.6 FP1

Citrix removed the change DNS from their console. But to change DNS-Server in “/etc/resolv.conf” are not solid settings.
If you reboot the server the settings will be changed back to the initial configuration.
You have to change it with “xe pif-reconfigure-ip”.

Change DNS-Server

First use

xe pif-list

to get the UUID of your PIF. You get an output like this:

uuid ( RO)                  : PIF-UUID
                device ( RO): eth0
    currently-attached ( RO): true
                  VLAN ( RO): -1
          network-uuid ( RO): NETWORK-UUID

You can view the current configuration of the interface with “xe pif-param-list uuid=PIF-UUID”.

To change the configration use the following command:

xe pif-reconfigure-ip uuid=PIF-UUID mode=static IP=IP-ADDRESS netmask=SUBNETMASK gateway=GATEWAY DNS=DNS-SERVER

Note: Use “DNS=DNS-SERVER-1,DNS-SERVER-2″ to specify more DNS-Servers.

Check your configuration with “xe pif-param-list uuid=PIF-UUID”.

Links:

http://forums.citrix.com/thread.jspa?messageID=1531853

Upgrade Debian Lenny to Squeeze on Citrix XenServer

Debian Logo

1. Update your Lenny installation

Make sure that your current Lenny is up-2-date.

aptitude update
aptitude upgrade

2. Change sources to Squeeze

Open “/etc/apt/sources.list” and replace “Lenny” with “Squeeze” here is an example:

# Debian Squeeze
deb http://ftp.de.debian.org/debian/ squeeze main
deb-src http://ftp.de.debian.org/debian/ squeeze main  

# Debian Squeeze Security
deb http://security.debian.org/ squeeze/updates main

3. Upgrade your system

aptitude update
aptitude install apt dpkg aptitude
aptitude full-upgrade

4. Upgrade Grub

Debian Squeeze use Grub2 you have to run “upgrade-from-grub-legacy” to upgrade from Grub to Grub2.

upgrade-from-grub-legacy
rm -f /boot/grub/menu.lst*

If you don’t do that, you get the following error in your XenServer.

Error: Starting VM 'YOUR_VM' - Using  to parse /boot/grub/menu.lst - Traceback (most recent call last): - File "/usr/bin/pygrub", line 746, in ? - raise RuntimeError, "Unable to find partition containing kernel" - RuntimeError: Unable to find partition containing kernel

, , , ,

Previous Posts Next posts