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

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

Step 2: Install development tools on Centos

Step 3: Install some useful tools

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

Step 4.2: Start Apache service

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

Step 5: Install MySQL

We are going to install MySQL 5.5 here.

Step 5.1: Add repos and install MySQL 5.5

Step 5.2: Start MySQL service

Step 5.3: Configure MySQL

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

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

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.

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

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.

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.

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.

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.

Create virtual host directory for your subdomain/domain.

Get into the folder.

Create error and requests log files.

Create folder to host files for this virtual host.

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

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

Step 8.4: Flush iptables

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!

How to create Apache htpasswd file in Linux OS?

What is htpasswd?

At times during web development, we will need to prevent the whole world from being able to access a particular website. That’s when Apache htpasswd comes into the picture. htpasswd is usually used together with htaccess.

Tutorial

This tutorial will teach you how to create a htpasswd file using command line.

Creating a htpasswd for the first time

Example

Appending new user to existing htpasswd file

Example

Using htpasswd in htaccess

 

That’s it you are done!

Install OpenCart 2 on CentOS

This guide focuses on how to install OpenCart 2 on CentOS 6. Before proceeding with this guide, make sure your server have met the following requirements.

1. Download OpenCart

Download OpenCart 2 from the following link, http://www.opencart.com/?route=download/download.

Then unzip the downloaded package.

2. Upload OpenCart to server

Use any FTP client (FileZilla, Transmit, etc) to upload your OpenCart files that you have just unzip to the server.

After uploading the files, renaming the following files:

  • /config-dist.php to /config.php
  • /admin/config-dist.php to /config.php

Note: For Apache servers, the directory that you are to upload to is /var/www/.

3. Enable file read/write permissions

Modify the file read/write permissions for the following files/directories:

  • /var/www/store/config.php
  • /var/www/store/admin/config.php
  • /var/www/store/system/cache/
  • /var/www/store/system/logs/
  • /var/www/store/system/download/
  • /var/www/store/system/upload/
  • /var/www/store/image/
  • /var/www/store/image/cache/
  • /var/www/store/image/catalog/
  • /var/www/store/system/modification/

Command to modify file read/write permissions

And one more additional command to run on image/cache directory

 

4. Begin Installation

Now head over to your web browser and access the following url.

5. Installation Step 1

OpenCart Installation Step 1
OpenCart Installation Step 1

Click on Continue to proceed.

6. Installation Step 2

OpenCart Installation Step 2
OpenCart Installation Step 2

If you have followed the previous steps correctly, then you should see all green ticks and all files/directories status are labelled as Writable.

Click on Continue to proceed.

7. Installation Step 3

OpenCart Installation Step 3
OpenCart Installation Step 3

Fill up your database connection details and an account for administration.

Then click on Continue to proceed.

8. Installation Step 4

OpenCart Installation Step 4
OpenCart Installation Step 4

That’s it! You have successfully setup an E-Commerce store online for yourself.

Feel free to drop me an email at [email protected] or leave a comment if you have any questions.

Setting up an SSL secured Webserver with CentOS

This guide will teach you how to configure and setup your own SSL certification on your CentOS web server.

1. Install required software

Use yum to get the following software if its not yet installed on your web server.

yum install mod_ssl openssl

2. Generate a self-signed certificate

The following steps guides you on how to generate your own self-signed certificate.

Generate private key

openssl genrsa -out ca.key 2048

Generate CSR

openssl req -new -key ca.key -out ca.csr

Generate self-signed key

openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

Copy the files to the respective locations

cp ca.crt /etc/pki/tls/certs

cp ca.key /etc/pki/tls/private/ca.key

cp ca.csr /etc/pki/tls/private/ca.csr

In cased you have moved the files and not copied them, use the following command to correct SELinux

restorecon -RvF /etc/pki

Update Apache SSL configuration file

sudo nano +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf

Modified the paths to match where the new Key is stored

SSLCertificateFile /etc/pki/tls/certs/ca.crt

Then set the correct path for Certification Key file

SSLCertificateKeyFile /etc/pki/tls/private/ca.key

Finally quit and save the file then restart Apache

sudo service httpd restart

3. Configure firewall

The last step to setting up your own SSL certificate is to configure the firewall to accept requests from port 443, which is used for SSL connections.

Command to accept port 443 requests

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Save ip table

/sbin/service iptables save

iptables -L -v

 

That’s it! Your web server is now configured and ready for SSL connections.

Feel free to drop me any questions you have at [email protected]

 

Install APC (Alternative PHP Cache) in CentOS 5/6/7 and Fedora 20/21

APC (Alternative PHP Cache) is a free and open source tool to cache PHP codes.

phpapc

1. Install Dependency Packages for APC

2. Install APC using PECL (PHP Extension Community Library)

Press “Enter” for the following options

2a. [Alternative]

If the above command fails, you can use the following method to install APC as well.

3. Enable APC PHP Extension

4. Restart Apache Service for APC

CentOS

Fedora

5. Verify APC Installation

Create a file named phpinfo.php in Apache web root directory. For example our Apache web root directory is /var/www

And add the following code to the php file

[Optional] If you do not have nano installed

Open the following url in your web browser

Screen Shot 2015-08-02 at 2.48.22 pm

 

6. Enable PHP APC administration

Copy the following file to any web directory that you want. For example, in this case we will copy it to the web root directory.

Open the file apc.php 

In nano editor, use “Ctrl-w” to search for the username and password field to modify.

Open the following link in your web browser.

You should see your APC stats and that’s it!

 

Installing Webmin on CentOS/RHEL 5/6/7 and Fedora 20/21

Wedmin is a web-based interface for you to manage your linux systems. It allows you to setup user accounts, configure cron jobs, manage Apache and many more.

webmin-logo

1. Download Webmin RPM

2. Install Webmin using RPM Command

3. Access Webmin in Browser

Additional Information

Below are the commands to start, stop, restart and get status of Webmin service