<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title><span>M</span>atteo <span>M</span>attei</title>
 <link href="http://matteomattei.com/atom.xml" rel="self"/>
 <link href="http://matteomattei.com/"/>
 <updated>2019-08-26T11:21:47+02:00</updated>
 <id>http://matteomattei.com</id>
 <author>
   <name>Matteo Mattei</name>
   <email>info@matteomattei.com</email>
 </author>
 
 <entry>
   <title>How to configure a secure SFTP chroot jail</title>
   <link href="http://matteomattei.com/how-to-configure-a-secure-sftp-chroot-jail/"/>
   <updated>2019-08-23T00:00:00+02:00</updated>
   <id>http://matteomattei.com/how-to-configure-a-secure-sftp-chroot-jail</id>
   <content type="html">&lt;p&gt;If you have a linux server, openssh is almost always already present, so without any other tool you can configure a super secure SFTP chroot jail to allow your users to access the server limiting the visibility to their home directory.&lt;/p&gt;

&lt;p&gt;Start creating a new linux system group called sftponly:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;groupadd --system sftponly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We create a system group because we want an ID lower than 1000 so that every new user will take a sequential UID.
Now open &lt;em&gt;/etc/ssh/sshd_config&lt;/em&gt; and make sure to have the following lines:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PasswordAuthentication yes
ChallengeResponseAuthentication no
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now replace the line starting with &lt;strong&gt;Subsystem&lt;/strong&gt; with the following:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Subsystem sftp internal-sftp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This line tells SSH to use its internal sftp subsytem to mange SFTP connections.&lt;/p&gt;

&lt;p&gt;Now add the following lines at the bottom of the file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Match Group sftponly
    ChrootDirectory %h
    X11Forwarding no
    AllowTcpForwarding no
    ForceCommand internal-sftp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically the above section describes how to handle connections from users belonging to &lt;em&gt;sftponly&lt;/em&gt; group.
In particular we are telling SSH to chroot the users to their home directory, does not allow X11 and TCP forwarding and force to use the internal sftp interface.&lt;/p&gt;

&lt;p&gt;After do that, restart ssh server to make the changes active:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/ssh restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now the SFTP server is ready to be used but you must keep in mind some important rules otherwise it will not work!&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;every user home directory must belong to &lt;strong&gt;root:root&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;every user home directory must have &lt;strong&gt;0755&lt;/strong&gt; permissions&lt;/li&gt;
  &lt;li&gt;every user must belong to &lt;strong&gt;sftponly&lt;/strong&gt; group&lt;/li&gt;
  &lt;li&gt;every first level folder in user home directory must belong to &lt;strong&gt;${USER}:sftponly&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s do an example: create a new user &lt;em&gt;matteo&lt;/em&gt; with no login shell, assign it to &lt;em&gt;sftpgroup&lt;/em&gt; group and set a password:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;useradd --create-home --shell /usr/sbin/nologin --user-group matteo
usermod --groups sftponly matteo
passwd matteo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Assuming you want the following permissions:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /home/matteo/pics    # write access by user matteo
mkdir /home/matteo/musics  # write access by user matteo
mkdir /home/matteo/logs    # read only access by user matteo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Configure the folders in this way:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chown root:root /home/matteo
chmod 755 /home/matteo
chown matteo:sftponly /home/matteo/pics
chown matteo:sftponly /home/matteo/musics
chown matteo:sftponly /home/matteo/logs
chmod 555 /home/matteo/logs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now try with &lt;em&gt;sftp&lt;/em&gt; command line client or with &lt;em&gt;filezilla&lt;/em&gt; and test your new SFTP server.
Files created from an SFTP session will belong to matteo:matteo.&lt;/p&gt;

&lt;p&gt;As you can understand, this configuration is very useful for web servers running with &lt;em&gt;PHP-FPM&lt;/em&gt; where every VirtualHost runs with its own user and privileges, so you can restrict the access by user with a secure SFTP connection and at the same time avoid all the problems related to the files permissions management and the configuration of a separated FTP/FTPS server.&lt;/p&gt;

&lt;p&gt;I hope you enjoy this article. If you like it please leave a comment!&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Monitor SSH access and send email when someone logins</title>
   <link href="http://matteomattei.com/monitor-ssh-access-and-send-email-when-someone-logins/"/>
   <updated>2019-08-21T00:00:00+02:00</updated>
   <id>http://matteomattei.com/monitor-ssh-access-and-send-email-when-someone-logins</id>
   <content type="html">&lt;p&gt;In order to monitor SSH access we can rely on &lt;strong&gt;rsyslog&lt;/strong&gt; given all SSH accesses are recorded in /var/log/auth.log.
Start creating a custom rsyslog configuration &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/rsyslog.d/90-ssh.conf&lt;/code&gt; with the following content:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/matteomattei/ff1286c839775fb3ffb4a27a2de8d1f5.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;Basically we are telling rsyslog to look for lines where the program name is &lt;strong&gt;sshd&lt;/strong&gt; and the message contains the &lt;strong&gt;session opened for user&lt;/strong&gt;.
Every time the above condition is matched, rsyslog will call the script we are going to create passing the entire log line as parameter.&lt;/p&gt;

&lt;p&gt;Assuming we want to receive an email with the user that have been logged, open your editor and create the file &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/local/bin/log_access.py&lt;/code&gt;:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/matteomattei/f2cc5a49e38894d81819322bb031cf4e.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;Make the file executable:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chmod +x /usr/local/bin/log_access.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Remember to fill the SMTP data at the beginning of the script.
As you can see the above script also logs all logins to &lt;code class=&quot;highlighter-rouge&quot;&gt;/var/log/logins.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to do what you want in the python script, the above it’s only an example!&lt;/p&gt;

&lt;p&gt;Now restart rsyslog and try if everything works as expected.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/rsyslog restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let me know your work cases and if this article can help you!&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Full web server setup with Debian 10 (Buster)</title>
   <link href="http://matteomattei.com/full-web-server-setup-with-debian-10-buster/"/>
   <updated>2019-07-28T00:00:00+02:00</updated>
   <id>http://matteomattei.com/full-web-server-setup-with-debian-10-buster</id>
   <content type="html">&lt;h1 id=&quot;setup-bash-and-update-the-system&quot;&gt;Setup bash and update the system&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp /etc/skel/.bashrc /root/.bashrc
apt-get update
apt-get dist-upgrade
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;configure-hostname-correctly&quot;&gt;Configure hostname correctly&lt;/h1&gt;

&lt;p&gt;Make sure to have the following two lines (with the same format) at the top of your &lt;em&gt;/etc/hosts&lt;/em&gt; file&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1       localhost.localdomain localhost
xxx.xxx.xxx.xxx web1.myserver.com web1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note: &lt;em&gt;xxx.xxx.xxx.xxx&lt;/em&gt; is the public IP address assigned to your server.&lt;/p&gt;

&lt;h1 id=&quot;install-all-needed-packages&quot;&gt;Install all needed packages&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt install wget vim git acl screen rsync net-tools pwgen php mariadb-server mariadb-client apache2 iptables shorewall php php-cli php-curl php-dev php-gd php-imagick php-imap php-memcache php-pspell php-recode php-tidy php-xmlrpc php-pear php-fpm postfix ca-certificates bsd-mailx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Postfix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Select &lt;strong&gt;Internet Site&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;System mail name: (insert here the FQDN, for example web1.myserver.com)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;setup-chrooted-sftp-jail&quot;&gt;Setup chrooted SFTP jail&lt;/h1&gt;

&lt;p&gt;Create &lt;code class=&quot;highlighter-rouge&quot;&gt;sftponly&lt;/code&gt; group:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;addgroup --system sftponly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/ssh/sshd_config&lt;/code&gt; and make sure to have the following lines:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PasswordAuthentication yes
ChallengeResponseAuthentication no
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then change the &lt;em&gt;Subsystem&lt;/em&gt; line with the following:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Subsystem sftp internal-sftp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And create the section to allow chrooted SFTP access to the users belonging to the &lt;code class=&quot;highlighter-rouge&quot;&gt;sftponly&lt;/code&gt; group.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Match Group sftponly
    ChrootDirectory %h
    X11Forwarding no
    AllowTcpForwarding no
    ForceCommand internal-sftp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now restart ssh server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/sshd restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to have a working sftp jail, there are 4 rules to follow:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;every user home directory must belong to &lt;strong&gt;root:root&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;every user home directory must have &lt;strong&gt;0755&lt;/strong&gt; permissions&lt;/li&gt;
  &lt;li&gt;every user must belong to &lt;strong&gt;sftponly&lt;/strong&gt; group&lt;/li&gt;
  &lt;li&gt;every subfolder in user home directory must belong to &lt;strong&gt;${USER}:sftponly&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;setup-apache&quot;&gt;Setup Apache&lt;/h1&gt;

&lt;p&gt;Stop Apache web server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/apache2 stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Backup Apache configuration:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit the following lines in &lt;em&gt;/etc/apache2/apache2.conf&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;From &lt;strong&gt;Timeout 300&lt;/strong&gt; to &lt;strong&gt;Timeout 45&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;From &lt;strong&gt;KeepAliveTimeout 5&lt;/strong&gt; to &lt;strong&gt;KeepAliveTimeout 15&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a configuration for phpmyadmin:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/apache2/conf-available/phpmyadmin.conf
Alias /phpmyadmin /usr/share/phpmyadmin

&amp;lt;Directory /usr/share/phpmyadmin&amp;gt;
    Options SymLinksIfOwnerMatch
    DirectoryIndex index.php

    &amp;lt;IfModule mod_php5.c&amp;gt;
        &amp;lt;IfModule mod_mime.c&amp;gt;
            AddType application/x-httpd-php .php
        &amp;lt;/IfModule&amp;gt;
        &amp;lt;FilesMatch &quot;.+\.php$&quot;&amp;gt;
            SetHandler application/x-httpd-php
        &amp;lt;/FilesMatch&amp;gt;

        php_value include_path .
        php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
        php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/share/php/php-gettext/:/usr/share/php/php-php-gettext/:/usr/share/javascript/:/usr/share/php/tcpdf/:/usr/share/doc/phpmyadmin/:/usr/share/php/phpseclib/
        php_admin_value mbstring.func_overload 0
    &amp;lt;/IfModule&amp;gt;
    &amp;lt;IfModule mod_php.c&amp;gt;
        &amp;lt;IfModule mod_mime.c&amp;gt;
            AddType application/x-httpd-php .php
        &amp;lt;/IfModule&amp;gt;
        &amp;lt;FilesMatch &quot;.+\.php$&quot;&amp;gt;
            SetHandler application/x-httpd-php
        &amp;lt;/FilesMatch&amp;gt;

        php_value include_path .
        php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
        php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/share/php/php-gettext/:/usr/share/php/php-php-gettext/:/usr/share/javascript/:/usr/share/php/tcpdf/:/usr/share/doc/phpmyadmin/:/usr/share/php/phpseclib/
        php_admin_value mbstring.func_overload 0
    &amp;lt;/IfModule&amp;gt;

&amp;lt;/Directory&amp;gt;

# Authorize for setup
&amp;lt;Directory /usr/share/phpmyadmin/setup&amp;gt;
    &amp;lt;IfModule mod_authz_core.c&amp;gt;
        &amp;lt;IfModule mod_authn_file.c&amp;gt;
            AuthType Basic
            AuthName &quot;phpMyAdmin Setup&quot;
            AuthUserFile /etc/phpmyadmin/htpasswd.setup
        &amp;lt;/IfModule&amp;gt;
        Require valid-user
    &amp;lt;/IfModule&amp;gt;
&amp;lt;/Directory&amp;gt;

# Disallow web access to directories that don't need it
&amp;lt;Directory /usr/share/phpmyadmin/templates&amp;gt;
    Require all denied
&amp;lt;/Directory&amp;gt;
&amp;lt;Directory /usr/share/phpmyadmin/libraries&amp;gt;
    Require all denied
&amp;lt;/Directory&amp;gt;
&amp;lt;Directory /usr/share/phpmyadmin/setup/lib&amp;gt;
    Require all denied
&amp;lt;/Directory&amp;gt;
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Configure the proper Apache modules and configurations:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a2dismod mpm_worker
a2dismod mpm_prefork

a2enmod mpm_event
a2enmod ssl
a2enmod rewrite
a2enmod headers
a2enmod deflate
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_fcgi
a2enmod http2
a2enmod setenvif

a2enconf security
a2enconf php7.3-fpm
a2enconf phpmyadmin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now restart Apache:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/apache2 restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-mariadb&quot;&gt;Setup MariaDB&lt;/h1&gt;

&lt;p&gt;Secure MariaDB installation:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql_secure_installation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Enter current password for root (enter for none): &lt;strong&gt;[ENTER]&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Set root password? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Write your &lt;em&gt;MARIAB_ROOT_PASSWORD&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Remove anonymous users? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Disallow root login remotely? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Remove test database and access to it? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Reload privilege tables now? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instruct MariaDB to use native password:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql -u root mysql -e &quot;update user set plugin='mysql_native_password' where user='root'; flush privileges;&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Set MariaDB root password in a configuration file (the same password configured before!)&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /root/.my.cnf
[client]
user = root
password = MARIADB_ROOT_PASSWORD
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Enable MySQL slow query logging (often useful during slow page load debugging):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sed -i &quot;{s/^#slow_query_log_file /slow_query_log_file /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#long_query_time /long_query_time /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log_slow_rate_limit /log_slow_rate_limit /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log_slow_verbosity /log_slow_verbosity /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log-queries-not-using-indexes/log-queries-not-using-indexes/g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MySQL is now configured, so restart it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/mysql restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;install-phpmyadmin&quot;&gt;Install phpMyAdmin&lt;/h1&gt;

&lt;p&gt;The version of phpmyadmin coming with the distribution is not updated so I prefer to install the latest manually:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export VER=&quot;4.9.0.1&quot;
cd /tmp
wget https://files.phpmyadmin.net/phpMyAdmin/${VER}/phpMyAdmin-${VER}-all-languages.tar.gz
tar xvf phpMyAdmin-${VER}-all-languages.tar.gz
rm -f phpMyAdmin-${VER}-all-languages.tar.gz
mv phpMyAdmin* /usr/share/phpmyadmin
mkdir -p /var/lib/phpmyadmin/tmp
chown -R www-data:www-data /var/lib/phpmyadmin
mkdir /etc/phpmyadmin/
cp /usr/share/phpmyadmin/config.sample.inc.php  /usr/share/phpmyadmin/config.inc.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit the file &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/share/phpmyadmin/config.inc.php&lt;/code&gt; and set secret passphrase and temporary directory:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// http://www.passwordtool.hu/blowfish-password-hash-generator
$cfg['blowfish_secret'] = 'SECRET_HERE';
[...]
$cfg['TempDir'] = '/var/lib/phpmyadmin/tmp';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;configure-shorewall-firewall-rules&quot;&gt;Configure Shorewall firewall rules&lt;/h1&gt;

&lt;p&gt;Copy the default configuration for one interface:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /usr/share/doc/shorewall/examples/one-interface
cp interfaces /etc/shorewall/
cp policy /etc/shorewall/
cp rules /etc/shorewall/
cp zones /etc/shorewall/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open &lt;em&gt;/etc/shorewall/policy&lt;/em&gt; file and change the line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;net             all             DROP            info
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;removing &lt;em&gt;info&lt;/em&gt; directive given it fills the system logs:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;net             all             DROP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open &lt;em&gt;/etc/shorewall/rules&lt;/em&gt; and add the following rules at the bottom of the file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HTTP/ACCEPT     net             $FW
HTTPS/ACCEPT    net             $FW
SSH/ACCEPT      net             $FW
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;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 xxx.xxx.xxx.xxx is the remote IP address, before the &lt;strong&gt;Ping(DROP)&lt;/strong&gt; rule:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ping(ACCEPT)    net:xxx.xxx.xxx.xxx       $FW
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit &lt;em&gt;/etc/default/shorewall&lt;/em&gt; and change &lt;strong&gt;startup=0&lt;/strong&gt; to &lt;strong&gt;startup=1&lt;/strong&gt;
You are now ready to start the firewall:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/shorewall start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-postfix&quot;&gt;Setup Postfix&lt;/h1&gt;

&lt;p&gt;Stop postfix server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/postfix stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit &lt;em&gt;/etc/mailname&lt;/em&gt; and set your server domain name, for example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server1.mycompany.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Restart Postfix:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/postfix start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;lets-encrypt&quot;&gt;Let’s encrypt&lt;/h1&gt;

&lt;p&gt;In order to get SSL free certificates with let’s encrypt install the powerful (and simple) dehydrated tool:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /root
git clone https://github.com/lukas2511/dehydrated.git
cd dehydrated
touch domains.txt
cp docs/examples/config .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Prepare Apache2 configuration for letsencrypt:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/apache2/conf-available/dehydrated.conf
Alias /.well-known/acme-challenge /var/www/dehydrated
&amp;lt;Directory /var/www/dehydrated&amp;gt;
        Options None
        AllowOverride None

        # Apache 2.x
        &amp;lt;IfModule !mod_authz_core.c&amp;gt;
                Order allow,deny
                Allow from all
        &amp;lt;/IfModule&amp;gt;

        # Apache 2.4
        &amp;lt;IfModule mod_authz_core.c&amp;gt;
                Require all granted
        &amp;lt;/IfModule&amp;gt;
&amp;lt;/Directory&amp;gt;
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Enable new config and reload Apache&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a2enconf dehydrated
systemctl reload apache2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;log-rotation&quot;&gt;Log rotation&lt;/h1&gt;

&lt;p&gt;In order to correctly log files you need to adjust logrotate configuration for Apache:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt;&amp;gt; /etc/logrotate.d/apache2
/var/www/vhosts/*/logs/access*.log
{
    rotate 30
    missingok
    size 10M
    compress
    delaycompress
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload &amp;gt; /dev/null
    endscript
}

/var/www/vhosts/*/logs/error*.log
{
    rotate 3
    missingok
    compress
    delaycompress
    size 2M
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload &amp;gt; /dev/null
    endscript
}
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;prepare-environment&quot;&gt;Prepare environment&lt;/h1&gt;

&lt;p&gt;Create all needed directories and files&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /root/cron_scripts
mkdir -p /var/www/vhosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now download all tools to manage the server locally:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/ADD_ALIAS.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/ADD_DOMAIN.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/ADD_SSL.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/ALIAS_LIST.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/DEL_ALIAS.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/DEL_DOMAIN.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/DOMAIN_LIST.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/MYSQL_CREATE.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian10/LAMP/UPDATE_SFTP_PASSWORD.sh
chmod 770 *.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Download also the tools that will be used with cron:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /root/cron_scripts
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/cron_scripts/backup_mysql.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/cron_scripts/mysql_optimize.sh
chmod 770 *.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Edit &lt;em&gt;/root/ADD_DOMAIN.sh&lt;/em&gt; and change &lt;strong&gt;ADMIN_EMAIL&lt;/strong&gt; variable with your email address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;configure-cron&quot;&gt;Configure CRON&lt;/h1&gt;

&lt;p&gt;Edit &lt;em&gt;/etc/crontab&lt;/em&gt; and add the following lines at the bottom:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# mysql optimize tables
3  4  *  *  7   root    /root/cron_scripts/mysql_optimize.sh

# mysql backup
32 4  *  *  *   root    /root/cron_scripts/backup_mysql.sh

# letsencrypt
50 2 * * *      root    /root/dehydrated/dehydrated -c &amp;gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Full web server setup with Debian 9 (Stretch)</title>
   <link href="http://matteomattei.com/full-web-server-setup-with-debian-9-stretch/"/>
   <updated>2017-12-31T00:00:00+01:00</updated>
   <id>http://matteomattei.com/full-web-server-setup-with-debian-9-stretch</id>
   <content type="html">&lt;h1 id=&quot;setup-bash-and-update-the-system&quot;&gt;Setup bash and update the system&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp /etc/skel/.bashrc /root/.bashrc
apt-get update
apt-get dist-upgrade
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;configure-hostname-correctly&quot;&gt;Configure hostname correctly&lt;/h1&gt;
&lt;p&gt;Make sure to have the following two lines (with the same format) at the top of your &lt;em&gt;/etc/hosts&lt;/em&gt; file&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1       localhost.localdomain localhost
xxx.xxx.xxx.xxx web1.myserver.com web1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note: &lt;em&gt;xxx.xxx.xxx.xxx&lt;/em&gt; is the public IP address assigned to your server.&lt;/p&gt;

&lt;h1 id=&quot;install-all-needed-packages&quot;&gt;Install all needed packages&lt;/h1&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get install vim git acl screen rsync net-tools php mysql-server mysql-client apache2 iptables phpmyadmin varnish shorewall vsftpd php-cli php-curl php-dev php-gd php-imagick php-imap php-memcache php-pspell php-recode php-tidy php-xmlrpc php-pear postfix apg ca-certificates bsd-mailx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;MariaDB/PhpMyAdmin:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;web server to reconfigure automatically: &lt;strong&gt;apache2&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;configure database for phpmyadmin with dbconfig-common? &lt;strong&gt;Yes&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;MySQL application password for phpmyadmin: [blank]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Postfix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Select &lt;strong&gt;Internet Site&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;System mail name: (insert here the FQDN, for example web1.myserver.com)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;setup-ftp&quot;&gt;Setup FTP&lt;/h1&gt;
&lt;p&gt;Stop VSFTP server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/vsftpd stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create backup configuration:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mv /etc/vsftpd.conf /etc/vsftpd.conf.backup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add new configuration:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; &quot;EOF&quot; &amp;gt; /etc/vsftpd.conf
listen=YES
listen_port=21
anonymous_enable=NO
local_enable=YES
guest_enable=YES
guest_username=nobody
user_sub_token=$USER
local_root=/var/www/vhosts/$USER
virtual_use_local_privs=YES
user_config_dir=/etc/vsftpd/users
pam_service_name=vsftpd_local_and_virtual
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
ftpd_banner=Welcome to my ftp server
write_enable=YES
download_enable=YES
dirlist_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
xferlog_file=/var/log/xferlog
connect_from_port_20=YES
connect_timeout=60
data_connection_timeout=300
idle_session_timeout=300
local_max_rate=0
max_clients=0
max_per_ip=3
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create an empty chroot_list file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /etc/vsftpd
touch /etc/vsftpd/chroot_list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Install PAM module for virtual users:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get install libpam-pwdfile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And configure it creating the file &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/pam.d/vsftpd_local_and_virtual&lt;/code&gt; with this content:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Standard behaviour for ftpd(8).
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# first try to authenticate local users
auth    sufficient      pam_unix.so

# if that failed, login with virtual user
auth    required        pam_pwdfile.so  pwdfile /etc/vsftpd/passwd

# pam_pwdfile doesn't come with account, so we just permit on success
account required        pam_permit.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Start VSFTP server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/vsftpd start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-apache&quot;&gt;Setup Apache&lt;/h1&gt;
&lt;p&gt;Stop Apache web server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/apache2 stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Backup Apache configuration:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit the following lines in &lt;em&gt;/etc/apache2/apache2.conf&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;From &lt;strong&gt;Timeout 300&lt;/strong&gt; to &lt;strong&gt;Timeout 45&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;From &lt;strong&gt;KeepAliveTimeout 5&lt;/strong&gt; to &lt;strong&gt;KeepAliveTimeout 15&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Edit &lt;em&gt;/etc/apache2/mods-enabled/mpm_prefork.conf&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;IfModule mpm_prefork_module&amp;gt;
        StartServers             5
        MinSpareServers          5
        MaxSpareServers          10
        MaxRequestWorkers        150
        MaxConnectionsPerChild   10000
&amp;lt;/IfModule&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit &lt;em&gt;/etc/apache2/ports.conf&lt;/em&gt; and change the port &lt;strong&gt;80&lt;/strong&gt; with &lt;strong&gt;8080&lt;/strong&gt; since we are going to use Varnish:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Listen 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change the port (from &lt;strong&gt;80&lt;/strong&gt; to &lt;strong&gt;8080&lt;/strong&gt;) also in the default virtual host &lt;em&gt;/etc/apache2/sites-enabled/000-default.conf&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Enable useful Apache modules:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a2enmod ssl
a2enmod rewrite
a2enmod headers
a2enmod deflate
a2enmod proxy
a2enmod proxy_http
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now restart Apache:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/apache2 restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-varnish&quot;&gt;Setup Varnish&lt;/h1&gt;
&lt;p&gt;Stop Varnish daemon:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/varnish stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Backup your &lt;em&gt;/etc/varnish/default.vcl&lt;/em&gt; and create a new one with this content:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vcl 4.0;
import std;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = &quot;127.0.0.1&quot;;
    .port = &quot;8080&quot;;
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.

    if (req.url ~ &quot;^/phpmyadmin&quot;) {
        return (pass);
    }

    if ((client.ip != &quot;127.0.0.1&quot; &amp;amp;&amp;amp; std.port(server.ip) == 80) &amp;amp;&amp;amp;
        (
          (req.http.host ~ &quot;localhost&quot;)
          # ENSURE HTTPS - DO NOT REMOVE THIS LINE
        )
    ){
        set req.http.x-redir = &quot;https://&quot; + req.http.host + req.url;
        return (synth(750, &quot;&quot;));
    }
}

sub vcl_synth {
  # Listen to 750 status from vcl_recv.
  if (resp.status == 750) {
    # Redirect to HTTPS with 301 status.
    set resp.status = 301;
    set resp.http.Location = req.http.x-redir;
    return(deliver);
  }
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit &lt;em&gt;/etc/default/varnish&lt;/em&gt; and set the &lt;strong&gt;DAEMON_OPTS&lt;/strong&gt; variable like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;DAEMON_OPTS=&quot;-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have to make some changes also to systemd scripts (this step is mandatory for Debian Stretch!) since systemd does not consider /etc/default/varnish settings.&lt;/p&gt;

&lt;p&gt;Edit &lt;em&gt;/lib/systemd/system/varnish.service&lt;/em&gt; and change port 6081 with port 80:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd

[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Restart Varnish:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl daemon-reload
systemctl restart varnish.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-mariadb&quot;&gt;Setup MariaDB&lt;/h1&gt;

&lt;p&gt;Secure MariaDB installation:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql_secure_installation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Enter current password for root (enter for none): &lt;strong&gt;[ENTER]&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Set root password? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Write your &lt;em&gt;MARIAB_ROOT_PASSWORD&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Remove anonymous users? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Disallow root login remotely? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Remove test database and access to it? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Reload privilege tables now? [Y/n] &lt;strong&gt;Y&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instruct MariaDB to use native password:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql -u root mysql -e &quot;update user set plugin='mysql_native_password' where user='root'; flush privileges;&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Set MariaDB root password in a configuration file (the same password configured before!)&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /root/.my.cnf
[client]
user = root
password = MARIADB_ROOT_PASSWORD
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Enable MySQL slow query logging (often useful during slow page load debugging):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sed -i &quot;{s/^#slow_query_log_file /slow_query_log_file /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#long_query_time /long_query_time /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log_slow_rate_limit /log_slow_rate_limit /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log_slow_verbosity /log_slow_verbosity /g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i &quot;{s/^#log-queries-not-using-indexes/log-queries-not-using-indexes/g}&quot; /etc/mysql/mariadb.conf.d/50-server.cnf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MySQL is now configured, so restart it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/mysql restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;fix-for-phpmyadmin-redirecting-to-port-8080&quot;&gt;Fix for PhpMyAdmin redirecting to port 8080&lt;/h1&gt;
&lt;p&gt;If you try to access to &lt;em&gt;http://yoursitename/phpmyadmin&lt;/em&gt; you are redirected to &lt;em&gt;http://yoursitename:8080/phpmyadmin&lt;/em&gt; that will not work unless you open the firewall rule for port 8080 as described below. This because the web server is actually running on port 8080. To workaround this and have the PhpMyAdmin working on port 80 you need to force the redirect:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &lt;span class=&quot;nt&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt; /etc/phpmyadmin/conf.d/fix-redirection.php
&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$cfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'PmaAbsoluteUri'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'REQUEST_SCHEME'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'://'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'SERVER_NAME'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/phpmyadmin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;EOF&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;configure-shorewall-firewall-rules&quot;&gt;Configure Shorewall firewall rules&lt;/h1&gt;
&lt;p&gt;Copy the default configuration for one interface:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /usr/share/doc/shorewall/examples/one-interface
cp interfaces /etc/shorewall/
cp policy /etc/shorewall/
cp rules /etc/shorewall/
cp zones /etc/shorewall/

cd /usr/share/doc/shorewall6/examples/one-interface
cp interfaces /etc/shorewall6/
cp policy /etc/shorewall6/
cp rules /etc/shorewall6/
cp zones /etc/shorewall6/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open &lt;em&gt;/etc/shorewall/policy&lt;/em&gt; file and change the line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;net             all             DROP            info
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;removing &lt;em&gt;info&lt;/em&gt; directive given it fills the system logs:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;net             all             DROP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open &lt;em&gt;/etc/shorewall/rules&lt;/em&gt; and add the following rules at the bottom of the file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HTTP/ACCEPT     net             $FW
HTTPS/ACCEPT     net             $FW
SSH/ACCEPT      net             $FW
FTP/ACCEPT      net             $FW

# real apache since varnish listens on port 80
#ACCEPT         net             $FW             tcp             8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;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 xxx.xxx.xxx.xxx is the remote IP address, before the &lt;strong&gt;Ping(DROP)&lt;/strong&gt; rule:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ping(ACCEPT)    net:xxx.xxx.xxx.xxx       $FW
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit &lt;em&gt;/etc/default/shorewall&lt;/em&gt; and change &lt;strong&gt;startup=0&lt;/strong&gt; to &lt;strong&gt;startup=1&lt;/strong&gt;
You are now ready to start the firewall:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/shorewall start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;setup-postfix&quot;&gt;Setup Postfix&lt;/h1&gt;
&lt;p&gt;Stop postfix server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/postfix stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit &lt;em&gt;/etc/mailname&lt;/em&gt; and set your server domain name, for example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server1.mycompany.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, in order to monitor mail traffic coming from PHP you need to edit &lt;em&gt;/etc/php/7.0/apache2/php.ini&lt;/em&gt;. Go to &lt;strong&gt;[mail function]&lt;/strong&gt; section and set the following two options:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sendmail_path = /usr/local/bin/sendmail-wrapper
auto_prepend_file = /usr/local/bin/env.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now create the two files above in &lt;em&gt;/usr/local/bin&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sendmail-wrapper&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;
logger &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; mail.info sendmail-wrapper.sh: &lt;span class=&quot;nv&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HTTP_HOST&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;REMOTE_ADDR&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SCRIPT_NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PWD&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;UID&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;whoami&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
/usr/sbin/sendmail &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;env.php&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;putenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HTTP_HOST=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.@&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HTTP_HOST&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;putenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SCRIPT_NAME=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.@&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SCRIPT_NAME&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;putenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SCRIPT_FILENAME=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.@&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SCRIPT_FILENAME&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;putenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;DOCUMENT_ROOT=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.@&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;DOCUMENT_ROOT&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;putenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;REMOTE_ADDR=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.@&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;REMOTE_ADDR&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now make they both have executable flag:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chmod +x /usr/local/bin/sendmail-wrapper
chmod +x /usr/local/bin/env.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add also &lt;em&gt;/usr/local/bin/&lt;/em&gt; to the open_basedir php list in &lt;em&gt;/etc/apache2/conf-enabled/phpmyadmin.conf&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Restart Postfix:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/init.d/postfix start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;lets-encrypt&quot;&gt;Let’s encrypt&lt;/h1&gt;
&lt;p&gt;In order to SSL free certificates with let’s encrypt install the powerful (and simple) dehydrated tool:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /root
git clone https://github.com/lukas2511/dehydrated.git
cd dehydrated
touch domains.txt
cp docs/examples/config .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Prepare Apache2 configuration for letsencrypt:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/apache2/conf-available/dehydrated.conf
Alias /.well-known/acme-challenge /var/www/dehydrated
&amp;lt;Directory /var/www/dehydrated&amp;gt;
        Options None
        AllowOverride None

        # Apache 2.x
        &amp;lt;IfModule !mod_authz_core.c&amp;gt;
                Order allow,deny
                Allow from all
        &amp;lt;/IfModule&amp;gt;

        # Apache 2.4
        &amp;lt;IfModule mod_authz_core.c&amp;gt;
                Require all granted
        &amp;lt;/IfModule&amp;gt;
&amp;lt;/Directory&amp;gt;
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Enable new config and reload Apache&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a2enconf dehydrated
systemctl reload apache2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;log-rotation&quot;&gt;Log rotation&lt;/h1&gt;
&lt;p&gt;In order to correctly log files you need to adjust lograte configuration for Apache:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt;&amp;gt; /etc/logrotate.d/apache2
/var/www/vhosts/*/logs/access*.log
{
    rotate 30
    missingok
    size 10M
    compress
    delaycompress
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload &amp;gt; /dev/null
    endscript
}

/var/www/vhosts/*/logs/error*.log
{
    rotate 3
    missingok
    compress
    delaycompress
    size 2M
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload &amp;gt; /dev/null
    endscript
}
EOF
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;prepare-environment&quot;&gt;Prepare environment&lt;/h1&gt;
&lt;p&gt;Create all needed directories and files&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /root/cron_scripts
mkdir -p /var/www/vhosts
mkdir -p /etc/vsftpd/users
touch /etc/vsftpd/passwd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now download all tools to manage the server locally:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/ADD_ALIAS.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/ADD_DOMAIN.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/ADD_FTP_VIRTUAL_USER.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/ADD_SSL.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/ALIAS_LIST.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/CLEAN_VARNISH_CACHE.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/DEL_ALIAS.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/DEL_DOMAIN.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/DEL_FTP_VIRTUAL_USER.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/DOMAIN_LIST.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/MYSQL_CREATE.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/UPDATE_ALL_FTP_PASSWORD.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/UPDATE_FTP_PASSWORD.sh
chmod 770 *.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Download also the tools that will be used with cron:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /root/cron_scripts
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/cron_scripts/backup_mysql.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian9/LAMP/cron_scripts/mysql_optimize.sh
chmod 770 *.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Edit &lt;em&gt;/root/ADD_DOMAIN.sh&lt;/em&gt; and change &lt;strong&gt;ADMIN_EMAIL&lt;/strong&gt; variable with your email address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;configure-cron&quot;&gt;Configure CRON&lt;/h1&gt;
&lt;p&gt;Edit &lt;em&gt;/etc/crontab&lt;/em&gt; and add the following lines at the bottom:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# mysql optimize tables
3  4  *  *  7   root    /root/cron_scripts/mysql_optimize.sh

# mysql backup
32 4  *  *  *   root    /root/cron_scripts/backup_mysql.sh

# letsencrypt
50 2 * * *      root    /root/dehydrated/dehydrated -c &amp;gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>How to backup MySQL data and schema in PHP</title>
   <link href="http://matteomattei.com/how-to-backup-mysql-data-and-schema-in-php/"/>
   <updated>2017-02-25T00:00:00+01:00</updated>
   <id>http://matteomattei.com/how-to-backup-mysql-data-and-schema-in-php</id>
   <content type="html">&lt;p&gt;&lt;img src=&quot;/public/posts_images/phpmysql_logo.gif&quot; alt=&quot;phpmysql&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For a project I am working on, I needed to create a PHP script to export a full MySQL database data and schema. This is probably not the best solution because for these types of things the right tools to use are &lt;strong&gt;mysqldump&lt;/strong&gt; and &lt;strong&gt;phpmyadmin&lt;/strong&gt; but if you need to do it programmatically using only PHP this might help you.&lt;/p&gt;

&lt;p&gt;Here below you can find the code I created for this purpose using &lt;strong&gt;PHP-PDO&lt;/strong&gt;:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/matteomattei/908cb5459f74038d962f1c8ace040b51.js&quot;&gt; &lt;/script&gt;

</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>How to shrink raspberry pi backup images</title>
   <link href="http://matteomattei.com/how-to-shrink-raspberry-pi-backup-images/"/>
   <updated>2016-09-13T00:00:00+02:00</updated>
   <id>http://matteomattei.com/how-to-shrink-raspberry-pi-backup-images</id>
   <content type="html">&lt;p&gt;When I backup my raspberry pi SD card one problem I always faced is how much storage space I have to use because using &lt;em&gt;dd&lt;/em&gt; command the resulting backup image is exactly the same size of the whole SD card and having memory cards of 32GB or more, the storage of my pc would end pretty soon.&lt;/p&gt;

&lt;p&gt;That said I wrote a little script that takes the &lt;em&gt;big&lt;/em&gt; image, resize it to the minimal and compress it using gzip.&lt;/p&gt;

&lt;p&gt;Just for completeness, this is the command I use to create the image of the SD card:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo dd if=/dev/mmcblk0 of=/path/to/image.img bs=1M
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can use the following script to shrink the image:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo ./raspberrypi_image_resize.sh /path/to/image.img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;script src=&quot;https://gist.github.com/matteomattei/86e06f24808f7c549b615935fb178a5d.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;The process takes some time and at the end you will find that the size of the compressed image is drastically reduced. This is an example of a 8GB SD card before and after the compression:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-rw-r--r-- 1 matteo matteo 8026849280 Sep 10 15:45 image.img
-rw-r--r-- 1 matteo matteo  468097056 Sep 12 12:57 image.img.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So from a 8GB file, we have obtained 460MB file.&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Secure PHP installation disabling dangerous functions</title>
   <link href="http://matteomattei.com/secure-php-installation-disabling-dangerous-functions/"/>
   <updated>2016-08-09T00:00:00+02:00</updated>
   <id>http://matteomattei.com/secure-php-installation-disabling-dangerous-functions</id>
   <content type="html">&lt;p&gt;Attacks through PHP vulnerabilities are very common and every sysadmin should protect and enforce as much as possible the server infrastructure and PHP configuration to prevent as much as possible these types of attack. Today I show you how to tune PHP configuration to disable some &lt;strong&gt;dangerous&lt;/strong&gt; functions and report as less information as possible to outside.&lt;/p&gt;

&lt;p&gt;All changes we are going to do are located in &lt;em&gt;php.ini&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;expose_php = Off          # we don't want to let the clients know we are using PHP
display_errors = Off      # in case of error we don't want to show it
register_argc_argv = Off  # for better performance
allow_url_fopen = Off     # no external URL access
allow_url_include = Off   # no external URL access
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source # potential dangerous functions to disable
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After that, restart the web server and create a &lt;code class=&quot;highlighter-rouge&quot;&gt;phpinfo()&lt;/code&gt; page to make sure the new values have been correctly set.&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Keep your websites protected with Maldet</title>
   <link href="http://matteomattei.com/keep-your-websites-protected-with-maldet/"/>
   <updated>2016-08-09T00:00:00+02:00</updated>
   <id>http://matteomattei.com/keep-your-websites-protected-with-maldet</id>
   <content type="html">&lt;p&gt;The LMD (Linux Malware Detect) also called &lt;em&gt;maldet&lt;/em&gt; is a malware scanner developed by (rxfn.com)[https://www.rfxn.com] for Linux released under the GNU GPLv2 license, that is designed around the threats faced in shared hosted environments.&lt;/p&gt;

&lt;p&gt;This guide show you how to install, configure and run maldet once a day in a cronjob:&lt;/p&gt;

&lt;p&gt;First of all download the latest version of the maldetect, decompress, and install it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /usr/local/src
wget http://www.rfxn.com/downloads/maldetect-current.tar.gz
tar -zxvf maldetect-current.tar.gz
cd maldetect-*
./install.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit the configuration file &lt;em&gt;/usr/local/maldetect/conf.maldet&lt;/em&gt; and set the following values:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;email_alert=&quot;1&quot;
email_addr=&quot;your-email@example.com&quot;
quarantine_hits=&quot;1&quot;
quarantine_clean=&quot;1&quot;
default_monitor_mode=&quot;/path/to/monitor&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The default monitor_mode is used by &lt;strong&gt;inotify&lt;/strong&gt; in case you want real-time protection, otherwise you can relay only on the cronjob that is already configured in &lt;em&gt;/etc/cron.daily&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For real-time protection start maldet inotify monitor: &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/init.d/maldet start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now update malware definitions and run your first scan:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;maldet -d                  # update the program
maldet -u                  # update malware definitions
maldet -a /path/to/scan    # scan all files in the path
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last command might take lot of time depending on the number of files to analyze.&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Install GoAccess to monitor web server statistics</title>
   <link href="http://matteomattei.com/install-and-configure-goaccess-on-centos-to-monitor-apache-statistics/"/>
   <updated>2016-08-09T00:00:00+02:00</updated>
   <id>http://matteomattei.com/install-and-configure-goaccess-on-centos-to-monitor-apache-statistics</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;https://goaccess.io/&quot;&gt;GoAccess&lt;/a&gt; is a nice tool that parses Apache logs and create a report in various format extracting lot of interesting data and statistics. This guide has been tested on CentOS 6.x but it should be very similar also for other distributions.&lt;/p&gt;

&lt;p&gt;First of all install some dependencies given we are going to compile the sources:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum install glib2 glib2-devel glibc make
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Download the goaccess source code, copile and install it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /usr/loca/src
wget http://tar.goaccess.io/goaccess-1.0.2.tar.gz
tar xzf goaccess-1.0.2.tar.gz
cd goaccess-1.0.2
./configure
make
make install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now run goaccess and select the format of the Apache log file from the list it proposes. In case you already know how the Apache output file is generated, you can edit the configuration file &lt;em&gt;/usr/local/etc/goaccess.conf&lt;/em&gt; with the appropriate patterns for &lt;strong&gt;time-format&lt;/strong&gt;, &lt;strong&gt;date-format&lt;/strong&gt; and &lt;strong&gt;log-format&lt;/strong&gt;. In my case I have the following:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] &quot;%r&quot; %s %b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create a &lt;em&gt;goaccess&lt;/em&gt; folder inside a virtualhost document root (so that it is accessible from the web):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir /var/www/vhosts/myhost.com/public_html/goaccess
chown myuser.myuser /var/www/vhosts/myhost.com/public_html/goaccess
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now edit &lt;em&gt;/etc/crontab&lt;/em&gt; and add a cronjob for goaccess that runs every 10 minutes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*/10 * * * *    myuser    /usr/local/bin/goaccess -f /usr/local/apache/logs/access_log -a -d -o /var/www/vhosts/myhost.com/public_html/goaccess/index.html &amp;amp;&amp;gt; /dev/null 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Generally is a good idea to protect the goaccess folder with a password so that nobody except you can access and see the statistics of the web server.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat &amp;lt;&amp;lt; EOF &amp;gt; /var/www/vhosts/myhost.com/public_html/goaccess/.htaccess
AuthType Basic
AuthName &quot;GoAccess&quot;
AuthUserFile /home/myuser/goaccess_htpasswd
Require valid-user
EOF

htpasswd -c /home/myuser/goaccess_htpasswd myuser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now every 10 minutes the statistics of your Apache (or Nginx) web server are correctly parsed and served in a nice HTML web interface!&lt;/p&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
 <entry>
   <title>Enforce Apache security and performance</title>
   <link href="http://matteomattei.com/enforce-apache-security-and-performance/"/>
   <updated>2016-08-09T00:00:00+02:00</updated>
   <id>http://matteomattei.com/enforce-apache-security-and-performance</id>
   <content type="html">&lt;p&gt;Production Apache web servers need to be well configured for what regards security and performance. Here below a quick tips to make your servers more secure and performant.&lt;/p&gt;

&lt;p&gt;First of all you need to verify if you are using &lt;strong&gt;prefork&lt;/strong&gt; module:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apachectl -V | grep -i mpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If prefork is enabled, you should see a line like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Server MPM:     prefork
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If it is, I wrote a simple script to calculate the number of &lt;strong&gt;MaxClients&lt;/strong&gt; your server can support:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/matteomattei/d2335a3ee5d13d0d1acb285806624ea9.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;Basically this number is calculated with this formula:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(TOTAL_RAM - MYSQL_RAM - 50MB) / APACHE_RAM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, edit &lt;em&gt;/etc/apache2/apache2.conf&lt;/em&gt; on Debian/Ubuntu and &lt;em&gt;/etc/httpd/conf/httpd.conf&lt;/em&gt; on RedHat/CentOS and set the prefork section like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;IfModule prefork.c&amp;gt;
    StartServer 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxClients 300            # value calculated
    MaxRequestPerChild 3000   # 3000 is a good number, avoid to leave it at 0
&amp;lt;/IfModule&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Set now some parameters that affects security and performances.
Depending on your distribution they can be already set in the following files:&lt;/p&gt;

&lt;p&gt;Debian/Ubuntu:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;/etc/apache2/apache2.conf&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;/etc/apache2/conf.d/security&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RedHat/CentOS:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;/etc/httpd/conf/httpd.conf&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;/etc/httpd/conf/extra/httpd-default.conf&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ServerTokens Prod
ServerSignature Off
HostnameLookups Off
Timeout 45
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now test apache configuration and if all goes well, restart the web server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apachectl configtest

/etc/init.d/httpd restart    # RedHat/CentOS
/etc/init.d/apache2 restart  # Debian/Ubuntu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
   <author>
     <name>Matteo Mattei</name>
     <uri>http://matteomattei.com/about</uri>
   </author>
 </entry>
 
</feed>
