vultr Setup with Ubuntu 16.04

Table of Contents

Vultr Promo Code

How To Add Swap on Ubuntu

Check for Swap Space

sudo swapon -s
#An empty list will confirm that you have no swap files enabled:
root@dreamrunner:~# sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
524288+0 records in
524288+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 2.16994 s, 247 MB/s
root@dreamrunner:~# 
root@dreamrunner:~# sudo mkswap /swapfile
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=df941039-665b-4c02-9d19-bfa10d45a5f4
root@dreamrunner:~# sudo swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
root@dreamrunner:~# sudo chmod 600 /swapfile
root@dreamrunner:~# swapon -s
Filename                                Type            Size    Used    Priority
/swapfile                               file            524284  0       -1

Make your swap file permanent. Add the line below to the bottom of the fstab file.

sudo nano /etc/fstab
#Paste in the following line:
/swapfile   none    swap    sw    0   0

Initial Server Setup with Ubuntu

Step One—Root Login

Once you know your IP address and root password, login as the main user, root.

ssh root@123.45.67.890

Step Two—Change Your Password

The first thing to do is change it to one of your choice.

passwd

Step Three— Create a New User

After you have logged in and changed your password, you will not need to login again as root. n this step we will make a new user and give them all of the root capabilities.

adduser demo

After you set the password, you do not need to enter any further information about the new user. You can leave all the lines blank if you wish

Step Four— Root Privileges

As of yet, only root has all of the administrative capabilities. We are going to give the new user the root privileges.

Let’s go ahead and edit the sudo configuration. This can be done through the default editor, which in Ubuntu is called ‘nano’

visudo

Find the section called user privilege specification. It will look like this:

# User privilege specification
root    ALL=(ALL:ALL) ALL

Under there, add the following line, granting all the permissions to your new user:

demo    ALL=(ALL:ALL) ALL

Step Five— Configure SSH (OPTIONAL)

Now it’s time to make the server more secure. These steps are optional. Please keep in mind that changing the port and restricting root login may make logging in more difficult in the future.

Open the configuration file

nano /etc/ssh/sshd_config

Find the following sections and change the information where applicable:

Port 25000
Protocol 2
PermitRootLogin no
# or 
PermitRootLogin without-password

Port: Although port 22 is the default, you can change this to any number between 1025 and 65536. In this example, I am using port 25000. Make sure you make a note of the new port number. This change will make it more difficult for unauthorized people to log in.

PermitRootLogin: change this from yes to no to stop future root login. You will now only be logging on as the new user.

Add these lines to the bottom of the document, replacing demo in the AllowUsers line with your username. (AllowUsers will limit login to only the users on that line. To avoid this, skip this line):

UseDNS no
AllowUsers demo

Save and Exit

Step Six— Reload and Done!

sudo service ssh restart
ssh -p 25000 demo@123.45.67.890

How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 16.041

Step 1: Install the Nginx Web Server

sudo apt-get update
sudo apt-get install nginx

Try the VPS IP:

http://server_IP

Step 2: Install MySQL to Manage Site Data

sudo apt-get install mysql-server

You will be asked to supply a root (administrative) password for use within the MySQL system.

To secure the installation, we can run a simple security script that will ask whether we want to modify some insecure defaults. Begin the script by typing:

sudo mysql_secure_installation

You will be asked to enter the password you set for the MySQL root account. Next, you will be asked if you want to configure the VALIDATE PASSWORD PLUGIN.

Warning: Enabling this feature is something of a judgment call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an error. This will cause issues if you use a weak password in conjunction with software which automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.

Answer y for yes, or anything else to continue without enabling.

If you've enabled validation, you'll be asked to select a level of password validation. Keep in mind that if you enter 2, for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.

here are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

For the rest of the questions, you should press Y and hit the Enter key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

Step 3: Install PHP for Processing

Since Nginx does not contain native PHP processing like some other web servers, we will need to install php-fpm, which stands for "fastCGI process manager". We will tell Nginx to pass PHP requests to this software for processing

sudo apt-get install php-fpm php-mysql
  • Configure the PHP Processor
    sudo nano /etc/php/7.0/fpm/php.ini
    cgi.fix_pathinfo=0
    

    This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if the requested PHP file cannot be found. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn't be allowed to execute.

    sudo systemctl restart php7.0-fpm
    

Step 4: Configure Nginx to Use the PHP Processor

sudo nano /etc/nginx/sites-available/default

Currently, with the comments removed, the Nginx default server block file looks like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}
  • We will also uncomment the location block dealing with .htaccess files. Nginx doesn't process these files. If any of these files happen to find their way into the document root, they should not be served to visitors

The changes that you need to make below:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Test your configuration file for syntax errors by typing:

sudo nginx -t

When you are ready, reload Nginx to make the necessary changes:

sudo systemctl reload nginx

Step 5: Create a PHP File to Test Configuration

We can do this by creating a test PHP file in our document root. Open a new file called info.php within your document root in your text editor:

sudo nano /var/www/html/info.php

Type or paste the following lines into the new file.

<?php
phpinfo();
http://server_IP/info.php

After verifying that Nginx renders the page correctly, it's best to remove the file you created as it can actually give unauthorized users some hints about your configuration that may help them try to break in.

sudo rm /var/www/html/info.php

Migrate Your Current VPS (Linode, Rackspace, AWS EC2) to Vultr2

Assuming the IP of new VPS: 111.222.333.444

Rsync Installation on both VPS

# both sides
sudo apt-get install rsync

Transfer the SSH key to the new VPS

If use user name + password to transfer files, omit this step.

# old VPS
ssh-copy-id 111.222.333.444

Transferring Site Files

Find the wordpress site folder in the old VPS

# old VPS
rsync -avP wordpress 111.222.333.444:/var/www/html/
# or use username + password + ssh port 25000
rsync -avP -e 'ssh -p 25000' wordpress username@111.222.333.444:/var/www/html/

MySQL Database Transfer

First, we will see what databases we need to dump. Log into MySQL:

# old VPS
mysql -u root -p
show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| wordpress          |
+--------------------+
3 rows in set (0.00 sec)

exit

We would like to transfer our "wordpress" database, which contains our site information, and also our "mysql" database, which will transfer all of our user info, etc. The "informationschema" is just data structure information, and we don't need to hold onto that.

mysqldump -u root -p -QqeR --add-drop-table --databases mysql wordpress | bzip2 -v9 - > siteData.sql.bz2

And be careful with mysql. If they are different versions of MySQL, it may cause some compatible issues. Check: Becareful with mysql.

rsync -avP siteData.sql.bz2 111.222.333.444:/home/username

Log into your new VPS:

# new VPS
cd ~
bunzip2 siteData.sql.bz2
mysql -u root -p < siteData.sql
mysql -u root -p
show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

exit

As you can see, our "wordpress" database is present. The previous "mysql" database has been replaced with the one from our old VPS.

sudo service mysql restart
sudo service nginx restart

More

Choose location

submarine cable map: http://www.cablemap.info/

Footnotes:

Author: Shi Shougang

Created: 2017-02-17 Fri 23:53

Emacs 24.3.1 (Org mode 8.2.10)

Validate