apt get life

Life around technology

  • Technology
    • Guides
    • Linux
    • Development
      • Laravel
    • Misc
    • Raspberry Pi
  • Writing
  • Crafts
    • Crochet
    • Model Making
    • Painting
  • Privacy Policy
You are here: Home / Archives for Uncategorized

Optimising Nginx for PHP & WordPress (Time To First Byte)

2019/04/06 by sudo

When running page speed insights, it seems that TTFB (Time To First Byte) is something that it really doesn’t like when checking performance.

To solve this, we can use nginx’s caching of compiled PHP pages. Even better, the cache can be a RAM disk, making it very responsive.

First, create a directory for the RAM disk:

sudo mkdir -p /mnt/nginx-cache

Now create an entry in the fstab file so it’s mounted to the RAM disk on boot:

sudo nano /etc/fstab

tmpfs /mnt/nginx-cache tmpfs rw,size=2048M 0 0

This creates a 2GB RAM disk. Edit the size as appropriate for your server. Then mount it:

sudo mount /mnt/nginx-cache

Now, create a cache configuration file for Nginx:

sudo nano /etc/nginx/conf.d/cache.conf

fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=phpcache:512m inactive=2h max_size=1024m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

This creates a cache of 1GB with a default time of 2 hours. Next update the config files for your website – change your config file name where appropriate.

/etc/nginx/sites-enabled/mysite.conf

Inside of the location ~ "^(.+\.php)($|/)" { section, add:

# ----------------------------------------------
# Caching
# ----------------------------------------------
# This defines which cache to use (defined in /etc/nginx/cache.conf)
fastcgi_cache phpcache;
# Cache only 200 Okay responses for 2 hours
fastcgi_cache_valid 200 2h;
# Don't cache POST requests, only GET
fastcgi_cache_methods GET HEAD;
# Optional. Add a header to prove it works
add_header X-Fastcgi-Cache $upstream_cache_status;

now you should be able to restart nginx sudo service nginx restart and access the site via a web browser. Then you can use something like developer tools access the headers of the web requests. You should find a header:

X-Fastcgi-Cache: HIT

 

Filed Under: Linux, Technology, Uncategorized Tagged With: nginx, php, ubuntu server, wordpress

Recommended starter hosting providers for 2014

2014/09/06 by sudo

These are my recommended hosting providers of 2014, all of whom offer Linux machines running Ubuntu or Debian:
Linode offer a wide range of VPS solutions in multiple locations. They have a good community behind them with some great tutorials. Their control panel makes things easy to manage too. There are pay-more additions like load balancers so if you think you’re going to grow your sites quickly or get lots of traffic it’s worth going with them as a more mature hosting provider.

Digital Ocean have been making a big name for themselves in the past year. Their machines are much faster than a traditional VPS as they’re an SSD only hosting provider, so no old spinning disks to slow things down. They have a wide range of tutorials and Q&A sections on their site which are growing rapidly. The web interface is easy to use, but I’m not too keen on the spin-up process yet as they insist on providing you a password via email.

Bytemark offer a cloud platform similar to both Linode and Digital Ocean. They’re a smaller team based in the UK and the platform is still maturing, but Bytemark are always my first port of call for hosting services. They sponsor many open source events and projects, and even offer hosting to the Debian project.

Filed Under: Linux, Technology, Uncategorized Tagged With: hosting, Linux

Speed up Apache Websites with Expires Headers

2014/08/24 by sudo

Page speed can be a big issue for site owners, developers and systems administrators alike. There are many things you can do at an application level to improve performance, but that takes a long time to review, write, test and implement. What about the quick gains, the things you can do quite easily that will improve performance? Well it turns out that you can speed up an Apache based webserver by simply enabling a module: expires headers.

Expires headers are part of the the computer code that gets exchanged when you access a page. You browser requests a page by sending a request to it, and the server responds with information indicating what it is that has been returned. As part of this response there’s a section called “Expires”. This indicates when the content that has been accessed on the website is going to “expire”. To understand this a bit better, you need to know that when a website’s content is loaded, it’s downloaded to your computer to be rendered in your browser. Once the site is on your screen, if expires headers are not set, each time you load the site it’s going to be downloaded again. This is a performace hit to you, your internet connection and your computer. Expires headers tell the computer to store content temporarily (“Cache” the content) on your computer. When you visit the site again in 5 minutes, if expires headers are set correctly, you’ll only download part of the information on the page.

So what should be cached? Well, here’s what content types I tend to set expires headers on:

  • Images (jpg, png, gif)
  • CSS
  • Javascript
  • content such as mp3, mov, mp4 and others that don’t change regularly

How do you do it? You can Speed up websites that operate on Apache using mod_expires. This is really simple to setup and configure if you know how to configure sites on the command line.

Enable the module

a2enmod expires

Edit the configuration file. This can be done in either /etc/apache2/sites-enabled/mysite.conf or /etc/apache2/mods-enabled/expires.conf. The virtualhost configuration file will enable it for a single site, the mods-enabled configuration file will enable it for all sites. Choose one and edit it with a command line text editor like nano. Enter the following:

          ExpiresActive on

          ExpiresByType image/jpg "access plus 30 days"
          ExpiresByType image/png "access plus 30 days"
          ExpiresByType image/gif "access plus 30 days"
          ExpiresByType image/jpeg "access plus 30 days"

          ExpiresByType text/css "access plus 7 days"

          ExpiresByType image/x-icon "access plus 1 month"

          ExpiresByType application/pdf "access plus 1 day"
          ExpiresByType audio/x-wav "access plus 1 month"
          ExpiresByType audio/mpeg "access plus 1 month"
          ExpiresByType video/mpeg "access plus 1 month"
          ExpiresByType video/mp4 "access plus 1 month"
          ExpiresByType video/quicktime "access plus 1 month"
          ExpiresByType video/x-ms-wmv "access plus 1 month"
          ExpiresByType application/x-shockwave-flash "access 1 month"

          ExpiresByType text/javascript "access plus 1 week"
          ExpiresByType application/x-javascript "access plus 1 week"
          ExpiresByType application/javascript "access plus 1 week"

Now restart or reload apache to apply the configuration

service apache2 restart

You can see from the code that the expires time has been set by content type. Each is different, depending on how often it’s expected to change and how big the file types are going to be. For example – a movie file is unlikely to change frequently, but is likely to be large, so if it’s got an expires header telling the browser to store it locally for up to 1 month after the date on which it was first accessed it. This makes the site faster to load. Now when someone loads a site on your server, it will store content after the initial page load and reduce subsequent loading time.

 

For further information on improving page speed you can check out Yahoo’s excellent article here: https://developer.yahoo.com/performance/rules.html

Why not checkout the Firefox and Chrome plugin “YSlow” which checks a range of potential speed issues and offers solutions:

  • FireFox: https://addons.mozilla.org/en-US/firefox/addon/yslow/
  • Chrome: https://chrome.google.com/webstore/detail/yslow/ninejjcohidippngpapiilnmkgllmakh

Google also has some useful tools and guides which can be found here: https://developers.google.com/speed/

Moz also has a brief article on the subject here: http://moz.com/blog/15-tips-to-speed-up-your-website

Filed Under: Guides, Technology, Uncategorized Tagged With: apache, expires headers, Linux, page speed

  • « Previous Page
  • 1
  • 2

Recent Posts

  • System Hang on Ubuntu 24.04 “e1000_print_hw_hang”
  • Disable iLO on HP Microserver Gen8
  • Ubuntu Desktop 24.04 Change Wallpaper Settings
  • Customising Ubuntu Desktop 24.04
  • Remove domains from Let’s Encrypt using Certbot

Tags

API auditing crochet data recovery debian debudding development Dingo API docker email Getting started with Laravel 5 & Dingo API hard drive health HP Microserver KVM Laravel larvel 5 lenovo Linux Minion mint netgear nas networking network shares php PHP development Postfix raspberry pi review samba security SMART smartctl smartmontools smb testing traefik ubuntu ubuntu 18.04 ubuntu 20.04 ubuntu 22.04 ubuntu server vagrant Virtual machines xdebug xubuntu

© Copyright 2015 apt get life