8 Simple Steps to Setup a Web Server with Apache Virtual Hosts & MySQL in Centos

by | 30 Mar 2017 | Amazon Web Services (AWS), Apache, CentOS, Database, Linux, MySQL, OS, Programming, Server, Technology | 0 comments

This tutorial will be guiding you in setting up a web server with pre-installed Centos. In summary, you will be installing Apache and MySQL, and learning how to configure virtual hosts in Apache.

Note: before proceeding with this tutorial. Make sure you figure out how to SSH into your server first.

Step 1: Update Centos

sudo yum update -y

Step 2: Install development tools on Centos

yum -y groupinstall 'Development Tools'

Step 3: Install some useful tools

yum install mlocate nano -y

mlocate – This is used to assist you in looking for files in Centos. You will see it in action later.

nano – This a text editor in Centos that is much easier to use than vi or vim.

Step 4: Install Apache

Step 4.1: Install Apache

yum install httpd -y

Step 4.2: Start Apache service

service httpd start

Step 4.3: Set Apache to run on server boot/restart

chkconfig httpd on

Step 5: Install MySQL

We are going to install MySQL 5.5 here.

Step 5.1: Add repos and install MySQL 5.5

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum --enablerepo=remi,remi-test install mysql mysql-server

Step 5.2: Start MySQL service

service mysqld start

Step 5.3: Configure MySQL

sudo /usr/bin/mysql_secure_installation

After running the above command, you will get a bunch of prompts.

  1. Simply hit enter on initial root password prompt as we don’t have any yet from a fresh installation.
  2. Enter your new password for root user.
  3. Simply enter “y” = yes for the remaining options.

Step 6: Set Timezone for server

sudo cp /etc/localtime /root/old.timezone && rm /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Singapore /etc/localtime

In the above command, I have set the timezone to Singapore, simply because I’m from Singapore. Feel free to change it to your own timezone.

Step 7: Install SSL support

yum install mod_ssl openssl -y

This will enable SSL support on your web server if you wish to add your own SSL certificate here in future.

Step 8: Setup Virtual Host on Apache

Before proceeding, now we can use mlocate which we have installed previously.

sudo updatedb

Running the above command sort of index the files in your server for mlocate to work.

locate httpd.conf

Use mlocate to look for Apache’s configuration file.

You should be getting something like this, “/etc/httpd/conf/httpd.conf”.

So now we can make use of nano that we have installed previously.

nano /etc/httpd/conf/httpd.conf

The above will allow start editing of Apache configuration file.

Step 8.1: Editing Apache Configuration

  1. Hit ctrl+w and type “/var/www” and enter
  2. Change DocumentRoot “/var/www/html” to DocumentRoot “/var/www”
  3. Hit ctrl+w again
  4. Change <Directory “/var/www/html”> to <Directory “/var/www”>
  5. Hit ctrl+w again and type “AllowOverride None” and enter
  6. Change AllowOverride None to AllowOverride All
  7. Hit ctrl+w again and type “NameVirtualHost *:80” and enter
  8. Change #NameVirtualHost *:80 to NameVirtualHost *:80
  9. Add one more line below it, NameVirtualHost *:443
  10. Then hit ctrl+v multiple times till you reach the end of the document

Do not close the file yet.

So what you have done above is doing some basic configuration to Apache for virtual host to work.

Now we need to setup virtual host in Apache configuration.

Below is a sample virtual host configuration.

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/tutorial.mervintan.com/public_html
     ServerName tutorial.mervintan.com
     ServerAlias tutorial.mervintan.com
     ErrorLog /var/www/tutorial.mervintan.com/error.log
     CustomLog /var/www/tutorial.mervintan.com/requests.log common
</VirtualHost>
<VirtualHost *:443>
     ServerAdmin [email protected]
     DocumentRoot /var/www/tutorial.mervintan.com/public_html
     ServerName tutorial.mervintan.com
     ServerAlias tutorial.mervintan.com
     ErrorLog /var/www/tutorial.mervintan.com/error.log
     CustomLog /var/www/tutorial.mervintan.com/requests.log common
     SSLEngine on
     SSLCertificateFile /etc/pki/tls/certs/localhost.crt
     SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
</VirtualHost>

Notice that “tutorial.mervintan.com” is the subdomain/domain you are trying to configure. So simple just change all occurrence of that to your own subdomain/domain.

To proceed, paste that at the end of Apache configuration file.

To finish editing Apache configuration file.

Hit ctrl+x and enter.

Last step, restart Apache.

service httpd restart

Step 8.2: Setup directories and files for a Virtual Host

Remember that we set our Apache document root to “/var/www” above.

Navigate to that directory now.

cd /var/www

Create virtual host directory for your subdomain/domain.

mkdir tutorial.mervintan.com

Get into the folder.

cd tutorial.mervintan.com

Create error and requests log files.

touch error.log
touch requests.log

Create folder to host files for this virtual host.

mkdir public_html

Inside public_html folder, this is where you should place all your .html, .js and .css files for hosting.

Step 8.3: Setting permission for hosting directory

cd /var
sudo chown -R apache:apache www

This will set Apache to be owner of those folders you have just created.

Step 8.4: Flush iptables

iptables --flush

Flushing iptables will remove all firewall records on your server. Then you will be able to access your site.

Finishing Up

If you remember we setup the virtual host for “tutorial.mervintan.com”. Do remember to setup your DNS record that points “tutorial.mervintan.com” to your server ip address.

That’s it you are done!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Posts

AppleiOSMobile PhonesObjective-CProgrammingSwiftTechnology
How to install Cocoapods for Objective-C/Swift development on Mac OS?

How to install Cocoapods for Objective-C/Swift development on Mac OS?

Steps to install Cocoapods Cocoapods is used in Xcode IDE for development from iOS apps using Objective-C/Swift. Cocoapods helps developers add external libraries to their iOS project easily. The following tutorial guides you on the steps to install Cocoapods on...

AndroidAndroidAppleiOSJavaMobile PhonesObjective-CProgramming
iPhone Camera
Complete overview of the mobile app development process (Infographic)

Complete overview of the mobile app development process (Infographic)

Clients always have the misperception that developing a mobile app is as easy as developing a mobile application would be a walk in a park without much effort involved. The following infographic pretty much sums up the total cost and effort for mobile app...