How to setup VPS for web hosting with Apache, PHP & MySql?

How to setup VPS for web hosting with Apache, PHP & MySql?

In this post, we will tell you How to setup VPS for web hosting with Apache, PHP, Perl/cgi, and MySQL software.  Lately we will install WordPress as a basic website. You can install anything you like though if you don’t prefer WordPress.

We will also explain some important security topics how how you can mitigate some of the more common threats. This tutorial is written to be suitable for all skill levels. So you may find some of the early steps almost obvious. Just remember you did not learn your skills overnight and everyone needs to get a start somewhere. 😉

The first step is choosing a VPS provider. You want a good host that has reliable support. We  recommend Vultr.com as they have several VPS options at very affordable prices.

Once you have signed up and paid, your VPS is built immediately. You should receive your logon information in an email upon sign-up.

How to setup VPS for web hosting with Apache, PHP & MySql?

How to setup VPS for web hosting with Apache, PHP & MySql?

SSH To the VPS

Now that you have your new VPS system you need to logon to it. Simply SSH as the user ‘root’ to the IP address you were provisioned. See the example command below, and replace x.x.x.x with your IP address. You can use putty to login by SSH.

Change the root password

Once you are logged in, I highly suggest you change your root password ASAP! Since you were emailed the default initial password, its not secure until you change it. Follow this example to change the root password. If all goes well you will get a message saying “passwd: all authentication tokens updated successfully”.

Update System Packages

Next, you should go ahead and get all the base OS packages updated before we go installing Apache or PHP. For this we will use a program called ‘yum’. Yum is the default package manager for RedHat Linux and CentOS. Follow the next example to update your packages using yum.

Once its finished you will get a message saying ‘Complete!’.

Installing Apache, PHP, Perl, and MySQL

Next up you need to install your web server (Apache), code interpreters (PHP & Perl/CGI), and Database Server (MySQL). You can do this all with one command using yum.

 Configure Apache

If you are just hosting one site you can store your files in /var/www and call it a day, but why waste the power of your VPS? You can easily setup multiple websites on the same VPS by using Apache Virtual Hosts.

I like to create a user per website and then host the files for that website in the users ‘public_html’ folder inside their home folder. This makes it easy to keep track of and offers additional security. Should one of your sites become compromised, it should not impact the others. One more layer to the security onion….

First lets create a user called ‘example’, create their ‘public_html’ folder, and give them permissions to it.

Next you need to edit ‘/etc/httpd/conf/httpd.conf’ using vi (you can also use pico if you find it easier).

Scroll down to the bottom of the file and add the following lines. Change the X.X.X.X to your servers IP address and anywhere you see example.com to your domain name. Also modify the home directory paths for your username instead of /home/example/. Simply add another virtual host section if you want to host another domain.

Save and exit the file, then start apache. Lastly make sure you set it to start on boot.

Now point the A record for your URL in your DNS to your IP address (tip: use CloudFlare’s DNS for Free). Clear your local DNS cache or wait a few minutes after changing your DNS A record. Then browse to your domain and you should be presented with a default apache webpage. Its the default page because you have not placed any files in the public_html directory. This OK for now. We will place something there a little later in this tutorial. For now lets move on to the next step, How-to Setup MySql on a VPS.

Configure MySQL

MySQL has a root account that is separate from the system root account. It has a separate password which you should set.

First Start Mysql and make sure its set to start automatically on boot.

Now you need to connect to the MySQL server and set your root password. You can issue the following commands to do this.

Next you should create a MySQL user and give that user access to their own database for each site you want to create that needs a database.  Again be sure to change ‘example’ and ‘NEWPASSWORD’ to your username and your password. Don’t forget this password. You will need it when setting up your website.

Thats all there is to Setting up MySQL!

 Configure IPTables on a VPS

As an additional security measure you should enable a firewall on your VPS. The IPTables Firewall is available by default on all CentOS Installations, but by default it is disabled. Before we just turn it on, you need to make sure you create some firewall rules first.

On your VPS make sure the firewall is stopped and then edit /etc/sysconfig/iptables. if you never started IPTables before this file likely won’t exist yet. Go ahead and create it and then paste the following lines into the file and save it.

Now start IP Tables and set it to start on boot.

To make sure your rules stuck, you can run the following command. You should see your rules display.

 Setup WordPress on a VPS

Now that you have configured Apache and MySQL, you are ready to load a website in your users public_html directory. For this example we will use WordPress, but you can use any CMS you choose. Installation routines are similar for all the major Blogging platforms, but your results may vary.

First you need to download the latest version of WordPress from their website. For convenience here is a direct link to their latest version download.

If you think “How to setup VPS for web hosting with Apache, PHP & MySql?” is hard way, you can change other simple ways:

Be the first to comment

Leave a Reply

Your email address will not be published.


*