Hosting a Website

 Watch a video of this tutorial being followed!

 

So you’ve got a provisioned VPS and you’re ready to start hosting a website! We’re not going to use a 3rd party control panel for this guide - we’re going to do it all through configuration files and commands. This is the best way to learn how to really host a website properly and give you a more in-depth knowledge of Linux. We’re going to install what’s known as a LAMP stack, meaning Linux, Apache, MySQL, and PHP on an Ubuntu/Debian OS. These two operating systems are very similar to each other as Ubuntu is built upon Debian. This tutorial will work on another OS, but the configurations may.

 

Installing the Operating System

Launch a VPS and select Ubuntu as the OS.

Adding a sudoer user

For safety and security, it is a good practice to set up a user aside from the root account and give it “sudoer” access - meaning the account is not root, however it can execute commands as root.

Log into your VPS over SSH. Then let's install the sudo and nano package.

$ apt-get install sudo nano

 

Next, we create a user for ourselves. The user can be named anything.

$ adduser exuser

 

Now we’ll add the new user to the sudoer group.

$ adduser exuser sudo

 

Let's start running commands as exuser!

$ su exuser

 

Installing the Web Server

We need to install software that will interact with the user’s browser, which is called an “httpd”, or “HTTP daemon”. The httpd will send and receive requests for content on your VPS.

 

Step 1: Installing apache

apt-get is a program that manages all of Debian’s repositories for you. Its primary function is to install and uninstall packages from your system.

 

First, install the apache2 package:

 

$ sudo apt-get install apache2 -y

 

It’s a good idea to update apt-get’s repositories to prevent missing packages or from grabbing older packages.

 

Step 2 - Change default configuration

By default, apache2 can be very resource intensive. We’re going to make some changes to the default configuration to prevent this from happening, so lets back up the stock configuration and start working on an independent copy.

 

$ sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak

$ sudo nano /etc/apache2/apache2.conf

 

Verify that these values are set:

 

KeepAlive Off

<IfModule mpm_prefork_module>

StartServers 2

MinSpareServers 6

MaxSpareServers 12

MaxClients 100

MaxRequestsPerChild 3000

</IfModule>

 

Save this new configuration by pressing CTRL + x and pressing y and enter to confirm.

 

Restart Apache2 for these changes to take effect.

$ sudo service apache2 restart

 

Step 3: Configure VirtualHosts (vhosts)

Apache2 is now configured to perform efficiently. Now we can use VirtualHosts to set up one or more websites.

$ sudo a2dissite ‘*default’

$ cd /var/www

 

For this to work, you need an A (or AAAA) DNS record pointing the domain to your VPS’s IP. By default, most domain registrars offer a DNS service. These records can only be changed with your DNS provider. If you are unsure about how to do this, contact your registrar for help. Please keep in mind that setting this may take 24-48 hours to take effect.

 

In this step, we create the directory structure for our site. Replace ‘example.com’ with the hostname of your website. perform these steps for each of the websites you wish to host.

 

$ sudo mkdir example.com

$ sudo mkdir example.com/public_html

$ sudo mkdir example.com/log

$ sudo mkdir example.com/backups

$ sudo chown -R www-data:www-data /var/www/example.com/*

 

If you’re hosting multiple websites, you’ll need to create a new virtual host (the file we create in the next step) for each one.

$ sudo nano /etc/apache2/sites-available/example.com.conf

 

Paste this information into the new file, changing ‘example.com’ to the name of the domain you are hosting.

 

# domain: example.com

# public: /var/www/example.com/public_html

 

<VirtualHost *:80>

        ServerName  www.example.com

        ServerAlias example.com

 

        DirectoryIndex index.html index.php

        DocumentRoot /var/www/example.com/public_html

        LogLevel warn

        ErrorLog /var/www/example.com/error.log

        CustomLog /var/www/example.com/log/access.log combined

</VirtualHost>

 

Press CTRL + x, y, and ENTER to save. Now, lets enable that virtualhost.

 

$ sudo a2ensite example.com.conf

 

Installing the Database (MySQL)

Many dynamic websites rely on databases such as MySQL to quickly store and retrieve data. This part of the tutorial will cover the basic installation of MySQL on your VPS.

 

Step 1: Installing MySQL

Use the following command to install the MySQL package:

 

$ sudo apt-get install mysql-server

 

When prompted, enter a secure password (make sure to keep the password handy!) for the root user of your MySQL database.

 

Secure the installation running the built-in secure installation utility.

 

$ sudo mysql_secure_installation

 

Step 2: Tightening the MySQL configuration

By default, MySQL doesn’t really run at optimum efficiency. This matters especially if you have a VPS with a lower amount of RAM (less than 1GB) where every MB counts. This section will optimize your MySQL installation to be more lightweight.

 

Open up the MySQL configuration file.

$ sudo nano /etc/mysql/my.cnf

 

Change the following lines:

max_connections = 50

key_buffer = 25M

max_allowed_packet = 1M

thread_stack = 128K

table_cache = 25

 

Press CTRL + x, y, and ENTER to save. Now, restart MySQL for these changes to take effect.

$ sudo service mysql restart

 

Great! The MySQL configuration has some basic optimizations now. Lets create an initial database. You could do this using PhpMyAdmin, but for the sake of less overhead we’ll do it from the command line.

 

Login to MySQL using the root password we set upon installing MySQL earlier:

$ mysql -u root -p

 

Here we create a database. You can change initialDB to any name you like - we’re using that name just for the sake of this tutorial:

create database initialDB;

 

Change new_user and new_user_password to the new user’s username and password.

grant all on initialDB.* to ‘new_user’ identified by ‘new_user_password’;

 

Tell MySQL to take the new settings into effect:

flush privileges;

 

We’re done here!

quit

 

Installing PHP

PHP is a dynamic scripting language that is widely used for web applications. Wordpress, phpBB, Drupal, laravel, and other software rely on PHP to serve content. This section will install PHP on your system.

 

Install PHP:

$ sudo apt-get install php5 php-pear

(Optional) Install the PHP-mysql package if you need database support:

$ sudo apt-get install php5-mysql

 

Tightening the PHP Configuration

Like Apache and MySQL, PHP doesn’t come configured perfectly out of the box. PHP is notorious for using lots of RAM, so we’re going to focus on that issue primarily.

 

Open up the configuration file:

$ sudo nano /etc/php5/apache2/php.ini

Change values as necessary here (If you have a larger VPS, feel free to bump up the memory limit as needed - but make sure MySQL and Apache have enough RAM too):

 

max_execution_time = 30

memory_limit = 256M

display_errors = Off

log_errors = On

error_log = /var/log/php/error.log

 

Press CTRL + x, y, and ENTER to save.

 

Create the log directory for PHP and fix its permissions so it’s writable for PHP.

$ sudo mkdir -p /var/log/php

$ sudo chown www-data:www-data /var/log/php

 

Restart apache for PHP and the changes we made to take effect.

$ sudo service apache2 restart

 

Hello, world! (Testing)

Now that we have all necessary software installed, lets make sure that it actually works!

 

$ sudo nano /var/www/example.com/public_html/index.php

 

Insert the following code into the file:

 

<?php

phpinfo();

?>

 

Press CTRL + x, y, and ENTER to save.

 

Visit example.com/index.php in your web browser. If you see the PHP information page, you did everything right! Rock on!

 

Was this answer helpful? 44 Users Found This Useful (82 Votes)