Mount directory into the RAM

Needed to mount WordPress cache folder into the RAM of one VPS, to get that little bit of extra speed.
To mount it temporary and see how it works for you, you can use:

mount -t tmpfs -o size=64M tmpfs /absolute/path/to/your/folder/

To make it permanent you need to add this in the /etc/fstab file:

tmpfs /absolute/path/to/your/folder tmpfs defaults,size=64M 0 0

Systemd simple service

This a template for simple sysmtemd service to change the ownership of a file, since in my case the file is in /sys and it is generated on boot, so using acl didn’t help me. I had to use this hack to change the ownership of a file on each boot.
What I need is a write permissions to a file in order to change the brightness on my laptop with i3wm.

The file is /etc/systemd/system/brightness.service
but there is symlink from the /etc/systemd/system/multi-user.target.wants directory.

This are the contents of the service file:

[Unit]
Description=Alter permissionsfor brightness

[Service]
ExecStart=/bin/chmod go+rw /sys/class/backlight/intel_backlight/brightness
ExecStop=/bin/chmod go+rw /sys/class/backlight/intel_backlight/brightness

[Install]
WantedBy=multi-user.target

You will also want to enable and start the service with:

systemctl enable brightness.service
systemctl start brightness.service

All of this action is happening on Ubuntu 18.04

Remove files that are not in a list

Needed to remove bunch of files that are not matching list given to me, so to leave only the files that are on the list, which turned out to be a little hard, or at least for me.

That did the trick:

 find . -name "*" | grep -vFf update_images.csv | xargs rm -f

If the file names contain white spaces, you will have to use the following command, but I haven’t tested it myself:

find . -name "*" | grep -vFf update_images.csv |sed 's/^/"/;s/$/"/' | xargs rm -rf

MySQL tuning for low memory servers

If you are running couple of sites on a small VPS and you have 512-1024MB RAM, you are probably not amazed by mysql eating 400 just to start.
You can cut 200-300 of that memory usage by disabling performance schema in your config file – usually called my.cnf

performance_schema = off

If you are wondering if you should disable it and what will happen – you most probably don’t need that feature so it is safe to disable it. In a few words – it is to help you tune the SQL server, queries, find bottlenecks etc.
You can get more info here – MySQL performance schema

php-fpm child processes memory usage

Often you need to adjust php-fpm for the memory available, and to do so you need to average the child processes memory usage.

Human readable:

 ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm 

and also machine friendly:

 ps -ylC php-fpm --sort:rss 

Fix i3wm tearing in ubuntu

i3 is great window manger, but for some time I had struggles fixing some tearing it had, until I found recently this answer, which fixed it for me.

You will have to install the comptom composite manager with:

sudo apt install compton

then use the following config in ~./config/compton.conf or wherever you prefer to keep you config files. Then place this in that config file:

# basic configuration
backend = "glx";
vsync = "opengl-swc";

glx-copy-from-front = true;
glx-swap-method = 2;
xrender-sync = true;
xrender-sync-fence = true;

# transparancy settings for i3
opacity-rule = [
    "0:_NET_WM_STATE@:32a *= '_NET_WM_STATE_HIDDEN'"
];

Or this variation:

backend = "glx";
glx-no-stencil = true;
paint-on-overlay = true;
vsync = "opengl-swc";

Now you can start compton with that config file to test if it solves the problem for you:

compton --config ~/.config/compton.conf -b 

If that work for you, you can place it in your i3wm config file, so it will be loaded on startup –

  exec --no-startup-id compton --config ~/.config/compton.conf -b

Sed replace in file

This one liner will replace all occurrences in given file.

sed -i -e 's#https://www.oldurl.xyz/#http://newurl.us#g' dump.sql 

Sed can use for delimiter anything you specify, so in the above example is #(hashtag jajajaj :D)
But it could be comma or whatever fits your case, so this will work too:

sed -i -e 's,https://www.oldurl.xyz/,http://newurl.us,g' dump.sql 

osTicket nginx config

Had to install osTicket recently, and it had bit of a problems with the ajax requests returning 404.
This config should be enough to get you started, you might not even need to change it, well besides the obvious things like server name and root.
Ideally it shouldn’t have if blocks, but I am too lazy right now to refine it.

server {
        root /var/www/osticket/;
        index index.php index.html;
        listen 80;

        server_name domain.com;

        set $path_info "";

        # Deny access to everything inside the include directory
        location ~ ^/include {
                deny all;
                return 403;
        }

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

        # Requests to /api/* need their PATH_INFO set, this does that
        if ($request_uri ~ "^/api(/[^\?]+)") {
                set $path_info $1;
        }

        # /api/*.* should be handled by /api/http.php if the requested file does not exist
        location ~ ^/api/(tickets|tasks)(.*)$ {
                try_files $uri $uri/ /api/http.php;
        }

        # /scp/ajax.php needs PATH_INFO too
        if ($request_uri ~ "^/scp/.*\.php(/[^\?]+)") {
                set $path_info $1;
        }

        # Catch requests to /scp/ajax.php/some/path and redirect them to ajax.php
        location ~ ^/scp/ajax.php/(.*)$ {
                try_files $uri $uri/ /scp/ajax.php;
        }

        # Set index.php as directory index
        location / {
                index index.php;
        }

        # PHP-FPM listening on 127.0.0.1:9001 or on a socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass 127.0.0.1:9001;
                #fastcgi_pass    unix:/var/run/php5-fpm.sock;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
                fastcgi_param   PATH_INFO               $path_info;
                include fastcgi_params;
        }
}

Mount SSHFS volumes in fstab with ssh key

SSHFS on command line usually takes the ssh key with the -o, option which doesn’t really work when adding it in fstab. If you look around the internet you most probably found that people recommend adding fstab records with the -o option, but this wont work, simply add the key as another regular fstab option –

sshfs#USER@domain.com:/data/www /mnt/logs/  fuse IdentityFile=/home/USER/.ssh/id_rsa,uid=UID,gid=GUID,users,idmap=user,noatime,allow_other,_netdev,reconnect,ro 0 0 

Replace USER with the user who connects to the remote server and UID and GUID with the ones from the remote server.
The above also mounts the remote system as read-only so you wont be able to write on the mount.