Setting up server on Ubuntu 16

Servers on Ubuntu 16 for now is more famous than Ubuntu 14 or Ubuntu 18. And, I think, even more stable than others. As a web-server it’s perfect for my tasks. So, let’s setup one more!

First, we need to connect to the server we have via SSH-client, for example, GIT-bash, using command:

ssh -i "<key location>" ubuntu@<IP of the server>

To receive all rights as superadmin we need to type command

# sudo -i

To update APT (software installer) use this commands:

# apt-get update
# apt-get upgrade

For more useful using the server, better to install files maganer and editor:

# apt-get install mc nano

Then install WWW-server:

# apt-get install apache2

Let’s turn on also some modules:

# a2enmod rewrite
# a2enmod ssl

And restart the www-server:

# systemctl restart apache2

Then let’s install PHP 7.0 and also some packages for PHP:

apt-get install php php-mysql php-curl php-gd php-intl php-pear php-imagick php-imap php-memcache  php-pspell php-recode php-sqlite3 php-tidy php-xmlrpc php-xsl php-mbstring php-gettext php-json php-cgi  php libapache2-mod-php

The database (SQL) server MySQL:

# apt-get install mysql-server

Next is to install PHPMyAdmin for easier work with MySQL databases:

# apt-get install phpmyadmin php-mbstring php-gettext

To secure server from hacking PHPMyAdmin, let’s hide it’s subdirectory:

# nano /etc/phpmyadmin/apache.conf

In the line “Alias /phpmyadmin /usr/share/phpmyadmin” change “/phpmyadmin” to any other, better to something like “jhsdfy78sjfhjfddg”

For sending emails through PHP, we will install this package:

# apt-get install php-mail sendmail

Then setup this function in PHP-config:

# nano /etc/php/7.0/apache2/php.ini

And edit the line:

sendmail_path =  /usr/sbin/sendmail -t -i

To work with virtual hosts, make this bash-script:

# touch /addvhost.sh
# nano /addvhost.sh

Then paste this text to the script:

#!/bin/bash
domain=$1
user=www-data group=www-data
dm=`ls /etc/apache2/sites-available/$domain.conf`
if [[ "$dm" = "/etc/apache2/sites-available/$domain.conf" ]]
then echo "Host already exist"
else echo "127.0.0.1 $domain" >> /etc/hosts
echo "Adding host $domain"
mkdir -p /var/www/$domain
mkdir -p /var/www/$domain/www
chown -R $user:$group /var/www/$domain
echo "<VirtualHost *:80>
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/$domain/www
<Directory /var/www/$domain/www>
Allow from All
Options -Indexes
AllowOverride All
</Directory>
</VirtualHost>" >> /etc/apache2/sites-available/$domain.conf
echo "Add host to Apache"
a2ensite $domain
echo "Restart Apache"
/etc/init.d/apache2 reload
chmod -R 777 /var/www/$domain
echo "Done!"<br> fi
exit 0

Save the file of the script, and make it executable:

# chmod +x /addvhost.sh

To add vistual host, use this command:

# /addvhost.sh domain.com

To secure server from many tries to login, install this software:

# apt-get install fail2ban

For secure more WWW-server, install packges:

# apt-get install libapache2-mod-evasive

And add rooles:

# nano /etc/apache2/mods-available/mod-evasive.conf

Add this text:

<IfModule mod_evasive20.c>
DOSHashTableSize 4096
DOSPageCount 5
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
</IfModule>

Enjoy!

(Visited 21 times, 1 visits today)