How to manage linux VPS and setup or transfer website

Many people are faced with a problem when they order unmanaged Virtual Private Server for the first time. The only information you receive from your hosting service provider is an IP address of your server and root password. And then you need to configure everything by yourself to launch your website online.

Don’t forget to point your domain name A record to new server’s IP address. More information about DNS administration you can read in upcoming articles.

After you registered VPS account and got authentication information to your server, you need to connect to it using SSH (cryptographic network protocol).

Microsoft Windows users can download terminal emulator to connect via SSH to your server, such as PuTTY (free open source software) or Xshell by Netsarang (free for home and school use).

For Linux / Mac users just type from your terminal:

ssh root@123.12.12.123 -p 22

where root – username, 123.12.12.123 – change with provided IP address of your server, you can skip option -p 22, if provided SSH port is 22 (default), otherwise specify the right one.

Now run that command, confirm your authentication by providing password and if everything went successful you should see welcome message (depends on VPS operation system)

LAMP or web panel?

Now you have two options:

  1. For ease of use, you can install one of available web panels and avoid in future working in terminal, reading a lot of guides, learning commands and configurations.
  2. If you want to take advanced control over your server, learn and understand how it works, optimize server resources, increase security and in case of server failure, quickly resolve any possible problem – that is your choice.

We will consider both options, as there could be different situations, when you need to install web panel or manually configure services on server.

Install LAMP (Linux Apache MySQL PHP)

In this example we installing Apache 2.4, MariaDB, PHP 7 and Free SSL certificate – Let’s Encrypt on Debian 9.2

First, you need to create a user. Run command:

root@s1:~# adduser user

Instead user, you can name it whatever you want. You should see confirmation messages and you will be prompted to enter a password for created user:

Adding user `user' ...
Adding new group `user' (1001) ...
Adding new user `user' (1001) with group `user' ...
Creating home directory `/home/user' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for user
Enter the new value, or press ENTER for the default
  Full Name []: User
  Room Number []: 
  Work Phone []: 
  Home Phone []: 
  Other []: 
Is the information correct? [Y/n]

Now add created user to sudoers group to allow executing programs with security privileges of root.

root@s1:~# adduser user sudo

Confirmation:

Adding user `user' to group `sudo' ...
Adding user user to group sudo
Done.

To switch user, just type command su and desired username:

su user

Depends on Operation System installed on your VPS, some package managers (tools) and commands can be different, to install software from repository.

Here a couple examples on updating package database. It’s highly recommended to do this before installing or upgrading packages.

Debian / Ubuntu sudo apt-get update
sudo apt update
CentOS sudo yum update
Fedora sudo dnf update

Install Apache Web Server

Run

sudo apt install apache2 apache2-mod-php7.0

If you’re asked for a [sudo] password for user, enter the same one you entered, when created this user.
You will see:

0 upgraded, 28 newly installed, 0 to remove and 0 not upgraded.
Need to get 12.9 MB of archives.
After this operation, 63.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Press Enter to continue installation and wait until proccess is finished.
Now you can check if Apache web server working by opening your server in browser, simply type your IP address in address bar, like this:

http://123.12.12.123/

You should see Apache default page, titled ” It works!”.

Install MariaDB (MySQL) database server

To install MariaDB server run:

sudo apt install mariadb-server

Then complete the same confirmations as you did earlier with Apache installation.

To secure your database server run next tool:

sudo mysql_secure_installation

Follow the instructions on screen, but keep in mind: changing root password for database, isn’t the same password, as for your server’s root access, it’s two different access credentials, so write them down):

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] 
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]  
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

After you finished MariaDB installation process:

Let’s connect to database server, create a new user and new database for your website.

Connect to DB with next command and enter previously changed password:

sudo mysql -u root -p

You’ll see MariaDB welcome message:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

To create new database, run next command in MariaDB console, don’t forget about closing semicolon at the end:

CREATE DATABASE anydbname;

If you did right, should see following confirmation:

MariaDB [(none)]> CREATE DATABASE anydbname;
Query OK, 1 row affected (0.00 sec)

As for server we created new user, we’ll do it the same on database. Working from root is always bad idea. Let’s create one:

CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbpassword';

If query was successful:

MariaDB [(none)]> CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbpassword';
Query OK, 0 rows affected (0.01 sec)

We created a user dbuser for localhost and set the password dbpassword
Write it down, with those credentials your website will connect to database.

Now, we need to grant our new user – priveleges to work with our new database. Let’s do it:

GRANT ALL PRIVILEGES ON anydbname.* TO 'dbuser'@'localhost';

Then reload privileges table by executing:

FLUSH PRIVILEGES;

When done, type quit; to exit MariaDB console.
It’s recommended to verify if we have done everything right, let’s connect to database with user we just created.

username: dbuser
password: dbpassword

sudo mysql -u dbuser -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| anydbname          |
| information_schema |
+--------------------+
2 rows in set (0.01 sec)

MariaDB [(none)]> use anydbname;
Database changed
MariaDB [anydbname]> show tables;
Empty set (0.00 sec)

Authorization went successful.
With command show databases; we noticed our anydbname database, switched to it with command use anydbname; and saw that it’s empty with command show tables;

Install PHP

Run command:

sudo apt install php7.0 php7.0-mysql

Now we finished installing LAMP (Apache MySQL and PHP on Linux).

sudo apt install php7.0 php7.0-mysql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  php7.0 php7.0-mysql
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 173 kB of archives.
After this operation, 564 kB of additional disk space will be used.
Get:1 http://ams2.mirrors.digitalocean.com/debian stretch/main amd64 php7.0 all 7.0.19-1 [48.7 kB]
Get:2 http://ams2.mirrors.digitalocean.com/debian stretch/main amd64 php7.0-mysql amd64 7.0.19-1 [124 kB]
Fetched 173 kB in 1s (166 kB/s)   
Selecting previously unselected package php7.0.
(Reading database ... 33743 files and directories currently installed.)
Preparing to unpack .../php7.0_7.0.19-1_all.deb ...
Unpacking php7.0 (7.0.19-1) ...
Selecting previously unselected package php7.0-mysql.
Preparing to unpack .../php7.0-mysql_7.0.19-1_amd64.deb ...
Unpacking php7.0-mysql (7.0.19-1) ...
Setting up php7.0-mysql (7.0.19-1) ...

Creating config file /etc/php/7.0/mods-available/mysqlnd.ini with new version

Creating config file /etc/php/7.0/mods-available/mysqli.ini with new version

Creating config file /etc/php/7.0/mods-available/pdo_mysql.ini with new version
Setting up php7.0 (7.0.19-1) ...
Processing triggers for libapache2-mod-php7.0 (7.0.19-1) ...

Restart Apache web service to reload it with installed modules:

sudo service apache2 restart

Let’s test our web server functionality.
Open your server’s page, as we did on Apache installation. You should see the same default Welcome Page.

http://123.12.12.123/

In your terminal go to default host directory, where Welcome page is located and remove it:

user@s1:~$ cd /var/www/html/
user@s1:~l$ sudo rm index.html 
user@s1:/var/www/html$ ls -lh
total 0

If you refresh server’s page, you should see now:

Apache root page

To test php functionality and MySQL connection we’ll create new page with name “index.php” in the default apache directory:

user@s1:~$ cd /var/www/html/
user@s1:/var/www/html/$ sudo nano index.php

If you don’t have nano editor, install it with

sudo apt install nano

Or alternative editors like vim

In the editor paste this php code:

<?php

$dbname = 'anydbname';
$dbuser = 'dbuser';
$dbpass = 'dbpassword';
$dbhost = 'localhost';

$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;

while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

?>

Refresh your web page. If you see message:

It means that PHP and MySQL configured and works! (but there is still no tables to show it’s count).
Now…

How to add a domain name to web server. Apache VirtualHost confguration.

First, enter Apache directory:

$ cd /etc/apache2/
$ ls -lh

total 80K
-rw-r--r-- 1 root root 7.1K Sep 19 18:58 apache2.conf
drwxr-xr-x 2 root root 4.0K Nov  7 00:11 conf-available
drwxr-xr-x 2 root root 4.0K Nov  7 00:11 conf-enabled
-rw-r--r-- 1 root root 1.8K Sep 19 18:56 envvars
-rw-r--r-- 1 root root  31K Sep 19 18:56 magic
drwxr-xr-x 2 root root  12K Nov  7 00:11 mods-available
drwxr-xr-x 2 root root 4.0K Nov  7 00:11 mods-enabled
-rw-r--r-- 1 root root  320 Sep 19 18:56 ports.conf
drwxr-xr-x 2 root root 4.0K Nov  7 00:11 sites-available
drwxr-xr-x 2 root root 4.0K Nov  7 00:11 sites-enabled

apache2.conf – it’s a main apache’s configuration file.

You can notice similar directories like *-available and *-enabled.
In *-available folders, you store all needed files, if it’s modules, configurations or sites (virtual hosts configurations). And if you want to enable some of them the next time you restart the server, you need to create a symbolic link to original file in folder *-enabled, it is much like creating a shortcut in OS Windows.

This is an example of how it looks in both directories.

Directory sites-available:

user@s1:/etc/apache2/sites-available$ ls -lh
total 12K
-rw-r--r-- 1 root root 1.4K Sep 19 18:56 000-default.conf
-rw-r--r-- 1 root root 6.2K Sep 19 18:56 default-ssl.conf

Directory sites-enabled:

user@s1:/etc/apache2/sites-enabled$ ls -lh
total 0
lrwxrwxrwx 1 root root 35 Nov  7 00:11 000-default.conf -> ../sites-available/000-default.conf

Let’s create our new config file for domain name s1.dock.co.nz in sites-available directory with the name s1-dock.conf
Before you continue, may be you should read our article about Domain Name System and how to manage DNS records.

user@s1:/etc/apache2/sites-available$ sudo nano s1-dock.conf

In the editor, configure VirtualHost to listen on any address on port 80, which will provide data from directory “/home/user/dock” when someone request host name “s1.dock.co.nz”. Below we set Directory permissions and configure logging to separate files.

<VirtualHost *:80>
    ServerName s1.dock.co.nz
    DocumentRoot "/home/user/dock"

    <Directory /home/user/dock/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/s1-dock-error.log
    CustomLog ${APACHE_LOG_DIR}/s1-dock-access.log combined
</VirtualHost>

 

Don’t forget to create new directory “dock” in /home/user/ and file index.html inside our new directory.

user@s1:~$ mkdir dock
user@s1:~$ ls
dock
user@s1:~$ cd dock/
user@s1:~/dock$ nano index.html

Edit index.html and write any welcome message.

Now we need to enable this configuration by creating a symlink in directory sites-enabled.

user@s1:/etc/apache2/sites-enabled$ ls
000-default.conf
user@s1:/etc/apache2/sites-enabled$ sudo ln -s /etc/apache2/sites-available/s1-dock.conf s1-dock.conf
user@s1:/etc/apache2/sites-enabled$ ls -lh
total 0
lrwxrwxrwx 1 root root 35 Nov  7 00:11 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 41 Nov 12 15:27 s1-dock.conf -> /etc/apache2/sites-available/s1-dock.conf

In the terminal’s output you can see our new symbolic link located in directory sites-enabled and linked to sites-available.

Before reloading web server configuration, let’s check it’s syntax for errors:

user@s1:~$ sudo apachectl configtest
Syntax OK

Now reload the configs with a command

sudo apachectl graceful

Graceful restart differs from usual restart by not closing opened connections and automatically check the configuration files syntax to avoid web server crash.

Finally we made it.

Type your domain name in browser and watch index.html page we created.

VirtualHost created

 

Now, you can upload your website files.

Also, don’t forget to check our article about how to secure your server with free SSL certificate Let’s Encrypt.

And if you don’t want to get inside this mess with manual configuration using terminal and prefer web based control panel for your VPS or Dedicated server, please read article about cheap web hosting control panel – ISPManager 5 Lite.

Thank you for reading, please leave your comments and share on facebook.

The following versions were used at the time of writing the article: 
Operation System 4.9.0-4-amd64 #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 
Web Server Software Apache/2.4.25 (Debian) 
PHP Version 7.0.19-1 
MySQL Version 15.1 Distrib 10.1.26-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

 

3 Comments

Leave a Reply