Matteo Mattei

Hello, my name is Matteo Mattei and this is my personal website. I am computer engineer with a long experience in Linux system administration and web software development.

linkedin rss twitter google+ github facebook

Create a self-contained installer in Bash

In this post I will show you how to develop a self contained Linux command line installer in Bash that will decompress an archive and perform some tasks.

Installer content

Our installer that is basically a self-extracting archive with some logic around, consists in three parts:

  • A bash script that performs the extraction of the archive and applies some logic.
  • A marker to separate the bash script and the archive.
  • An archive containing the actual data to install.

Start now!

Create a new bash script called with the following content:


echo ""
echo "My Command Line Installer"
echo ""

# Create destination folder
mkdir -p ${DESTINATION}

# Find __ARCHIVE__ maker, read archive content and decompress it
ARCHIVE=$(awk '/^__ARCHIVE__/ {print NR + 1; exit 0; }' "${0}")
tail -n+${ARCHIVE} "${0}" | tar xpJv -C ${DESTINATION}

# Put your logic here (if you need)

echo ""
echo "Installation complete."
echo ""

# Exit from the script with success (0)
exit 0


This script is self-explain but I will try to describe the steps:

  1. Create a destination folder ${DESTINATION}.
  2. Find __ARCHIVE__ marker and put the tarball content into ${ARCHIVE} variable.
  3. Decompress the tarball into the destination folder.
  4. Eventually apply your installation logic (copy some files, change some others, etc…).
  5. Exit from the script (this step is mandatory otherwise bash will try to interpret the tarball and will exit with error).
  6. Add __ARCHIVE__ marker at the bottom of the script. This marker will be used to separate the actual bash script with the tarball content.

Now generate a compressed tarball of your application (I used .tar.xz in the above example):

tar cJf myarchive.tar.xz /folder/to/archive

OK, now append it to the installer bash script and make it executable:

cat myarchive.tar.xz >>
chmod +x

That’s all! You can now distribute your installer.

Execute your installer

The users will execute your installer simply running:


Install CouchDB 1.6.x on Debian 7 (Wheezy)

Setup repository and install all dependencies

echo "deb wheezy contrib" >> /etc/apt/sources.list
wget -qO - | apt-key add -
apt-get update
apt-get install -y build-essential curl erlang-nox erlang-dev libmozjs185-1.0 libmozjs185-dev libcurl4-openssl-dev libicu-dev

Create CouchDB account

useradd -d /var/lib/couchdb couchdb
mkdir -p /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb /var/lib/couchdb
chown -R couchdb:couchdb /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb
chmod -R g+rw /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb

Install CouchDB

tar xzf apache-couchdb-1.6.1.tar.gz
cd apache-couchdb-1.6.1
./configure --prefix=/usr/local --with-js-lib=/usr/lib --with-js-include=/usr/include/js --enable-init
make && make install
chown couchdb:couchdb /usr/local/etc/couchdb/local.ini
ln -s /usr/local/etc/init.d/couchdb /etc/init.d/couchdb
/etc/init.d/couchdb start
update-rc.d couchdb defaults

Verify that all is working fine


The output should be like this:

{"couchdb":"Welcome","uuid":"5da242ff50cecec904d6caf36be34194","version":"1.6.1","vendor":{"name":"The Apache Software Foundation","version":"1.6.1"}}

Finalize setup

In order to connect from remote edit /usr/local/etc/couchdb/local.ini and change



bind_address to

Restart the database:

service couchdb restart

And from a web browser visit the CouchDB Futon:


Full web server setup with Debian 7 (Wheezy)

Setup bash and update the system

cp /etc/skel/.bashrc /root/.bashrc
apt-get update
apt-get dist-upgrade

Configure hostname correctly

Make sure to have the following two lines (with the same format) at the top of your /etc/hosts file       localhost.localdomain localhost web1

Note: is the public IP address assigned to your server.

Install all needed packages

apt-get install php5 mysql-server mysql-client apache2 iptables phpmyadmin varnish shorewall vsftpd php5-cli php5-curl php5-dev php5-gd php5-idn php5-imagick php5-imap php5-memcache php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xcache php5-xmlrpc php-apc php-pear php-xml-rpc postfix apg ca-certificates heirloom-mailx


  • mysql root password: xxx
  • repeat mysql root password: xxx
  • web server to reconfigure automatically: apache2
  • configure database for phpmyadmin with dbconfig-common? Yes
  • Password of the database’s administrative user: xxx
  • Password for phpmyadmin: xxx
  • Password confirmation: xxx


  • Select Internet Site
  • System mail name: (insert here the FQDN, for example

Setup FTP

Stop VSFTP server:

/etc/init.d/vsftpd stop

Create backup configuration:

mv /etc/vsftpd.conf /etc/vsftpd.conf.backup

Add new configuration:

ftpd_banner=Welcome to my ftp server

Create an empty chroot_list file:

touch /etc/vsftpd/chroot_list

Start VSFTP server:

/etc/init.d/vsftpd start

Setup Apache

Stop Apache web server:

/etc/init.d/apache2 stop

Backup Apache configuration:

cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup

Edit the following lines in /etc/apache2/apache2.conf

  • From Timeout 300 to Timeout 45
  • From KeepAliveTimeout 5 to KeepAliveTimeout 15
  • Change the mpm_prefork_module section like the following:
  <IfModule mpm_prefork_module>
      StartServers         5
      MinSpareServers      5
      MaxSpareServers      10
      ServerLimit          400
      MaxClients           400
      MaxRequestsPerChild  10000

Edit /etc/apache2/ports.conf and change the port 80 with 8080 since we are going to use Varnish:

NameVirtualHost *:8080
Listen 8080

Change the port (from 80 to 8080) also in the default virtual host /etc/apache2/sites-enabled/000-default Now restart Apache:

/etc/init.d/apache2 restart

Setup Varnish

Stop Varnish daemon:

/etc/init.d/varnish stop

Open /etc/varnish/default.vcl and make sure the backend section is like this:

backend default {
    .host = "";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;

Now edit /etc/default/varnish and set the DAEMON_OPTS variable like this:

DAEMON_OPTS="-a :80 \
                -T localhost:6082 \
                -f /etc/varnish/default.vcl \
                -p thread_pools=4 \
                -p thread_pool_max=1500 \
                -p listen_depth=2048 \
                -p lru_interval=1800 \
                -h classic,169313 \
                -p max_restarts=6 \
                -p connect_timeout=600 \
                -p send_timeout=2000 \
                -s malloc,2G"

Restart Varnish:

/etc/init.d/varnish restart

Setup MySQL

MySQL is already configured. You only need to log slow queries (that is often usefult during slow load page investigation). Todo it, open /etc/mysql/my.cnf and decomment the following two lines:

log_slow_queries       = /var/log/mysql/mysql-slow.log
long_query_time = 2

Configure Shorewall firewall rules

Copy the default configuration for one interface:

cp /usr/share/doc/shorewall/examples/one-interface/interfaces /etc/shorewall/interfaces
cp /usr/share/doc/shorewall/examples/one-interface/policy /etc/shorewall/policy
cp /usr/share/doc/shorewall/examples/one-interface/rules /etc/shorewall/rules
cp /usr/share/doc/shorewall/examples/one-interface/zones /etc/shorewall/zones

Now open /etc/shorewall/policy file and change the line:

net             all             DROP            info

removing info directive given it fills the system logs:

net             all             DROP

Now open /etc/shorewall/rules and add the following rules at the bottom of the file:

HTTP/ACCEPT     net             $FW
SSH/ACCEPT      net             $FW
FTP/ACCEPT      net             $FW

# real apache since varnish listens on port 80
#ACCEPT         net             $FW             tcp             8080

NOTE: in case you want to allow ICMP (Ping) traffic from a specific remote hosts you need to add a rule similar to the following where is the remote IP address, before the Ping(DROP) rule:

Ping(ACCEPT)       $FW

Now edit /etc/default/shorewall and change startup=0 to startup=1 You are now ready to start the firewall:

/etc/init.d/shorewall start

Setup Postfix

Stop postfix server:

/etc/init.d/postfix stop

Edit /etc/mailname and set your server domain name, for example:

Then, in order to monitor mail traffic coming from PHP you need to edit /etc/php5/apache2/php.ini. Go to [mail function] section and set the following two options:

sendmail_path = /usr/local/bin/sendmail-wrapper
auto_prepend_file = /usr/local/bin/env.php

Now create the two files above:


logger -p site=${HTTP_HOST}, client=${REMOTE_ADDR}, script=${SCRIPT_NAME}, pwd=${PWD}, uid=${UID}, user=$(whoami)
/usr/sbin/sendmail -t -i $*



Now make they both have executable flag:

chmod +x /usr/local/bin/sendmail-wrapper
chmod +x /usr/local/bin/env.php

Add also /usr/local/bin/ to the open_basedir php list in /etc/apache2/conf.d/phpmyadmin.conf

php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/local/bin/

Restart Postfix:

/etc/init.d/postfix start

Prepare environment

Create all needed directories and files

mkdir /root/cron_scripts
mkdir -p /var/www/vhosts
mkdir -p /etc/vsftpd/users
touch /etc/vsftpd/passwd

Now download all tools to manage the server locally:

chmod 770 *.sh

Download also the tools that will be used with cron:

cd /root/cron_scripts
chmod 770 *.sh
  • Edit /root/ and change ADMIN_EMAIL variable with your email address.
  • Edit /root/ and change the variable MYSQL_ROOT_PASSWORD with your MySQL root password.
  • Edit /root/cron_scripts/ and change the variable DB_PASSWORD with your MySQL root password and MAIL_NOTIFICATION with your email address.
  • Edit /root/cron_scripts/ and change the variable MYSQL_ROOT_PASSWORD with your MySQL root password.

Configure CRON

Edit /etc/crontab and add the following lines at the bottom:

# mysql optimize tables
3  4  *  *  7   root    /root/

# mysql backup
32 4  *  *  *   root    /root/

How to backup MySQL database using shell and cron

Given I did it dozens of times and everytime I have rewritten the code form scratch, I decided to write a simple script to backup all MySQL databases separately in order to avoid to always reinvent the wheel.

The following script must be configured with:

  • the MySQL root user.
  • the password of MySQL root user.
  • the email address to receive the notifications in case of failures.
  • the destination folder of the backups.
  • the number of copies to keep before overwriting the old backup.

NOTE: given the MySQL root password is in clear is important to limit the access to the script:

chown root.root
chmod 660

Then, to set a cronjob to do it automatically every day, open /etc/crontab and add the following line at the bottom:

# mysql backup
32 4  *  *  *   root    /root/

How to cross compile CURL library with SSL and ZLIB support


Every time I have to cross compile a new application or library it is always painful and I often have to spend several minutes (hours) to figure out how to build it. In this case I am going to show you how to cross compile CURL library (and application) with SSL and ZLIB support for an embedded system based on ARM.

First of all I suppose you already have all includes and libraries (static and dynamic) of openSSL and Zlib somewhere in your system (how to cross-compile openssl and zlib is out of the scope of this post).

In my case I have this structure:

├── openssl
│   ├── apps
│   │   └── openssl
│   ├── include
│   │   └── openssl
│   │       ├── aes.h
│   │       ├── asn1.h
│   │       ├── asn1_mac.h
│   │       ├── asn1t.h
│   │       ├── bio.h
│   │       ├── blowfish.h
│   │       ├── bn.h
│   │       ├── buffer.h
│   │       ├── camellia.h
│   │       ├── cast.h
│   │       ├── cmac.h
│   │       ├── cms.h
│   │       ├── comp.h
│   │       ├── conf_api.h
│   │       ├── conf.h
│   │       ├── crypto.h
│   │       ├── des.h
│   │       ├── des_old.h
│   │       ├── dh.h
│   │       ├── dsa.h
│   │       ├── dso.h
│   │       ├── dtls1.h
│   │       ├── ebcdic.h
│   │       ├── ecdh.h
│   │       ├── ecdsa.h
│   │       ├── ec.h
│   │       ├── engine.h
│   │       ├── e_os2.h
│   │       ├── err.h
│   │       ├── evp.h
│   │       ├── hmac.h
│   │       ├── idea.h
│   │       ├── krb5_asn.h
│   │       ├── kssl.h
│   │       ├── lhash.h
│   │       ├── md4.h
│   │       ├── md5.h
│   │       ├── mdc2.h
│   │       ├── modes.h
│   │       ├── objects.h
│   │       ├── obj_mac.h
│   │       ├── ocsp.h
│   │       ├── opensslconf.h
│   │       ├── opensslv.h
│   │       ├── ossl_typ.h
│   │       ├── pem2.h
│   │       ├── pem.h
│   │       ├── pkcs12.h
│   │       ├── pkcs7.h
│   │       ├── pqueue.h
│   │       ├── rand.h
│   │       ├── rc2.h
│   │       ├── rc4.h
│   │       ├── ripemd.h
│   │       ├── rsa.h
│   │       ├── safestack.h
│   │       ├── seed.h
│   │       ├── sha.h
│   │       ├── srp.h
│   │       ├── srtp.h
│   │       ├── ssl23.h
│   │       ├── ssl2.h
│   │       ├── ssl3.h
│   │       ├── ssl.h
│   │       ├── stack.h
│   │       ├── symhacks.h
│   │       ├── tls1.h
│   │       ├── ts.h
│   │       ├── txt_db.h
│   │       ├── ui_compat.h
│   │       ├── ui.h
│   │       ├── whrlpool.h
│   │       ├── x509.h
│   │       ├── x509v3.h
│   │       └── x509_vfy.h
│   └── libs
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├── libcrypto.a
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├── libssl.a
│       ├──
│       ├──
│       ├──
│       └──
└── zlib
    ├── include
    │   ├── zconf.h
    │   └── zlib.h
    └── libs
        ├── libz.a
        ├── ->
        ├── ->

Now download the last version of CURL, decompress and configure it:

$ wget
$ tar xzf curl-7.37.1.tar.gz
$ export ROOTDIR="${PWD}"
$ cd curl-7.37.1/
$ export CROSS_COMPILE="arm-none-linux-gnueabi"
$ export CPPFLAGS="-I${ROOTDIR}/openssl/include -I${ROOTDIR}/zlib/include"
$ export LDFLAGS="-L${ROOTDIR}/openssl/libs -L${ROOTDIR}/zlib/libs"
$ export AR=${CROSS_COMPILE}-ar
$ export AS=${CROSS_COMPILE}-as
$ export LD=${CROSS_COMPILE}-ld
$ export RANLIB=${CROSS_COMPILE}-ranlib
$ export CC=${CROSS_COMPILE}-gcc
$ export NM=${CROSS_COMPILE}-nm
$ export LIBS="-lssl -lcrypto"
$ ./configure --prefix=${ROOTDIR}/build --target=${CROSS_COMPILE} --host=${CROSS_COMPILE} --build=i586-pc-linux-gnu --with-ssl --with-zlib

At the end of the configure you should see a configuration resume similar to the following:

  curl version:     7.37.1
  Host setup:       arm-none-linux-gnueabi
  Install prefix:   /tmp/working_copy/build
  Compiler:         arm-none-linux-gnueabi-gcc
  SSL support:      enabled (OpenSSL)
  SSH support:      no      (--with-libssh2)
  zlib support:     enabled
  GSS-API support:  no      (--with-gssapi)
  SPNEGO support:   no      (--with-spnego)
  TLS-SRP support:  enabled
  resolver:         default (--enable-ares / --enable-threaded-resolver)
  ipv6 support:     no      (--enable-ipv6)
  IDN support:      no      (--with-{libidn,winidn})
  Build libcurl:    Shared=yes, Static=yes
  Built-in manual:  enabled
  --libcurl option: enabled (--disable-libcurl-option)
  Verbose errors:   enabled (--disable-verbose)
  SSPI support:     no      (--enable-sspi)
  ca cert bundle:   no
  ca cert path:     no
  LDAP support:     no      (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  LDAPS support:    no      (--enable-ldaps)
  RTSP support:     enabled
  RTMP support:     no      (--with-librtmp)
  metalink support: no      (--with-libmetalink)
  HTTP2 support:    disabled (--with-nghttp2)

Now compile and install it in the previously configured build directory:

$ make
$ make install

Now in the parent directory you should have a new folder called build with the new curl library and application with OpenSSL and Zlib support cross compiled for ARM:

├── bin
│   ├── curl
│   └── curl-config
├── include
│   └── curl
│       ├── curlbuild.h
│       ├── curl.h
│       ├── curlrules.h
│       ├── curlver.h
│       ├── easy.h
│       ├── mprintf.h
│       ├── multi.h
│       ├── stdcheaders.h
│       └── typecheck-gcc.h
└── lib
    ├── libcurl.a
    ├── ->
    ├── ->
    └── pkgconfig
        └── libcurl.pc

(I voluntary omitted the share folder in the tree exploded).

How to protect wp-content/uploads folder in Wordpress and avoid spam

Usually it is not a problem with Wordpress itself but sometimes we install lot of plugins that come from not well known origins or that are buggy and they can compromise the entire server. So, after dozens of server maintanance sessions, I am going to summarize all the best practice I found.

  • If you have direct control of the admin area of WP you can restrict the filesystem permissions of uploads folder to only user and group:
chmod o-w wp-content/uploads

Remember that in this way you are not able to upload files from the admin area if the web server runs with other privileges.

  • Check the origin of all plugins and make sure to keep your WP installation up to date. In fact attackers often use the last vulnerabilities to attack your server!
  • Use a different user for each domain. This is a general best practice because if an attacker haks your website, he will not be able to access to all other websites in the same server with the same credentials.
  • Deny the usage of scripts in uploads folder using a special .htaccess file placed in wp-content/uploads/

This third rule is very important and saved me bad headaches.