nginx

I’ve been spending this morning optimizing the Flosoft.biz website in terms of load times in Browsers, and one key element of that is sending the correct expires headers to allow Browsers to cache the data.

Now, as of Plesk 11.5, you can edit nginx settings via the Control Panel, but this isn’t always straight forward, so I thought I’d write a small tutorial.

In the Control Panel:

  1. Select your Domain
  2. Click Web Server Settings
  3. Scroll down to nginx settings
  4. If you have “Serve static files directly by nginx” checked (which I recommend), you’ll need to remove the file extensions you’re going to use below, such as jpg,gif,…
  5. In the text box “Additional nginx directives” copy / paste the following configuration:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
try_files $uri @fallback;
}

That’s it. Just hit OK and enjoy a website that sends the correct headers for your static images and CSS.

 

Read more

One of the legacy systems we still use at Flosoft.biz is Plesk. Over the last few years it has slowly gotten better (don’t worry, it still completely breaks on every version upgrade) and nowadays comes with nginx.

However, I noticed that for some obscure reason, it doesn’t enable GZip compression for the webpages it serves? This is quite odd, having myself worked a lot with nginx over the last few years, it’s a default configuration!

Don’t worry, it’s quite easy to enable it though:

Just edit the following file as root: /etc/nginx/conf.d/gzip.conf

gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable “msie6”;

Then run nginx -t to test the configuration and if that’s all ok, restart nginx by running /etc/init.d/nginx restart.

That’s it. Your webserver will now be serving your pages with GZip compression.

Read more

This guide will guide you through the process of setting up PHP-FPM and nginx on your Mac OSX 10.8 Mountain Lion using MacPorts.

Installing XCode

For starters, you need MacPorts installed. This requires XCode and XCode’s Command Line tools. You can get XCode off the Apple Store. Once installed, go to preferences and install the Command Line tools.

Next install MacPorts. Just go to this page and download the pkg for your version of OSX.

Once that’s done, we’re ready to start.

Installing PHP-FPM

First, let’s install PHP-FPM:

sudo port install php54-fpm

Now we’ll also need to copy the config file:

sudo cp /opt/local/etc/php54/php-fpm.conf.default /opt/local/etc/php54/php-fpm.confsudo cp /opt/local/etc/php54/php.ini-development /opt/local/etc/php54/php.ini

That’s PHP sorted. If you want to install any PHP extensions, just use port install php54-EXTENSION.

Installing nginx

Next, we install nginx and copy the config files into the right place.

sudo port install nginx
 sudo cp -p /opt/local/etc/nginx/fastcgi.conf.example /opt/local/etc/nginx/fastcgi.conf
 sudo cp /opt/local/etc/nginx/fastcgi_params.example /opt/local/etc/nginx/fastcgi_params
 sudo cp /opt/local/etc/nginx/mime.types.example /opt/local/etc/nginx/mime.types
 sudo cp /opt/local/etc/nginx/nginx.conf.example /opt/local/etc/nginx/nginx.conf
 sudo mkdir /opt/local/etc/nginx/conf.d

Configuration

That’s it. Now you can go ahead and configure nginx to your liking by editing the file:

/opt/local/etc/nginx/nginx.conf

I have also changed some configuration files to make PHP work properly off my ~/Sites/ folder.

Edit /opt/local/etc/php54/php.ini:

cgi.fix_pathinfo=0

This will help with the nginx config.

Edit the fastcgi_params file:

sudo vi /opt/local/etc/nginx/fastcgi_params

It should look like this:

fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS         200;

And finally, add a location to your nginx configuration for PHP:

sudo vi /opt/local/etc/nginx/nginx.conf

Here’s my config:

        location ~ \.php$ {
            root   /Users/florian/Sites;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
        }

That’s it for the configuration.

Tweaking .bash_profile

Lastly, we’ll add some lines to your .bash_profile to make it easier to start and stop nginx and php_fpm.

vi ~/.bash_profile

And then add the following:

# nginx
 alias nginx_start='sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist'
 alias nginx_stop='sudo launchctl unload -w /Library/LaunchDaemons/org.macports.nginx.plist'
 alias nginx_restart='nginx_stop; nginx_start;'
#php-fpm
alias fpm_start=’sudo launchctl load -w /Library/LaunchDaemons/org.macports.php54-fpm.plist’
 alias fpm_stop=’sudo launchctl unload -w /Library/LaunchDaemons/org.macports.php54-fpm.plist’
 alias fpm_restart=’fpm_stop; fpm_start’
Boom! That’s it. You’ve now got nginx and php-fpm running on your Mac.
Read more