Apache Virtual Hosts on VPS

Apache Virtual Hosts on VPS

How To Set Up Apache Virtual Hosts on Ubuntu

  • Access to VPS

Before you start install Ubuntu or other Os like Fedora,Debian etc,Your new Droplet is all set to go! You can access it using the following credentials:

example

IP Address: 1xx.xx.1.123
Username: root
Password: xxxxxxx

you will get ip address via mail or  vps provider website,Open your terminal using Ctrl +Alt + T or Access console in hosting provider, type
ssh [email protected]
then enter your password, after you logged they will ask to change your password , change it
“You are required to change your password immediately (root enforced)”


 

  • Install Apache

sudo apt-get update
sudo apt-get install apache2

Hide Apache Information on Ubuntu VPS/Dedicated Web server


 

 

  • Install MySQL

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.
sudo mysql_install_db
Once you have installed MySQL, we should activate it with this command
sudo mysql_install_db
Finish up by running the MySQL set up script:
sudo /usr/bin/mysql_secure_installation
The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):
OK, successfully used password, moving on

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.


Remove anonymous users? [Y/n] y … 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] y … Success!

 

By default, MySQL 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] n … skipping.

 

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

 

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

Cleaning up…


 

  • Install PHP

PHP is an open source web scripting language that is widely use to build dynamic webpages.
To install PHP, open terminal and type in this command.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

After you answer yes to the prompt twice, PHP will install itself.
It may also be useful to add php to the directory index, to serve the relevant php index files:
sudo nano /etc/apache2/mods-enabled/dir.conf

Add index.php to the beginning of index files. The page should now look like this:
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm


 

  • Install phpMyAdmin

sudo apt-get install phpmyadmin apache2-utils
After the installation has completed, add phpmyadmin to the apache configuration.
sudo nano /etc/apache2/apache2.conf
Add the phpmyadmin config to the file.
Include /etc/phpmyadmin/apache.conf

Restart apache:
sudo service apache2 restart

You can then access phpmyadmin by going to youripaddress/phpmyadmin. The screen should look like this


 

After these steps are complete, we can get started.There is two options available

  1. Create on Var/www/ directory, all sites in root folder and it’s just like Shared hosting, access any sites using shell etc.
  2. Create on Home directory, under different folders just like Reseller hosting ,no way to access other sites

 

 

Create on Var/www/ directory

For instance, for our sites, we’re going to make our directories like this:
sudo mkdir -p /var/www/example.com/public_html

Permissions
sudo chown -R $USER:$USER /var/www/example.com/public_html

also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:
sudo chmod -R 755 /var/www

Create Demo Pages
nano /var/www/example.com/public_html/index.html

Check this article  :How to use nano editor

Create the First Virtual Host File

 <body>
    <h1>Success!  The example.com virtual host is working!</h1>
  </body>

Start by copying the file for the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/example.com.conf

our virtualhost file should look like this:

ServerAdmin [email protected]
 ServerName example.com
 ServerAlias www.example.com
 DocumentRoot /var/www/example.com/public_html 
 ErrorLog ${APACHE_LOG_DIR}/error.log 
 CustomLog ${APACHE_LOG_DIR}/access.log combined

 

Enable the New Virtual Host Files

We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite example.com.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
Test your Results
http://example.com

 

Create on Home directory

Create a New User
Once you are logged in as root, we’re prepared to add the new user account that we will use to log in from now on.

This example creates a new user called “demo”, but you should replace it with a user name that you like:
adduser demo

Root Privileges
gpasswd -a demo sudo

then add your new accout privilege using visudo

sudo visudo

# User privilege specification
root ALL=(ALL:ALL) ALL

newuseraccount ALL=(ALL:ALL) ALL

Then we need to create directory in /home/newuseraccount/public_html
sudo mkdir -p /home/newuseraccount/public_html

example newuseraccount : facebook
make it sudo mkdir -p /home/facebook/public_html

Add Permissions

sudo chown -R /home/newuseraccount/public_html
chown -R newuseraccount /home/facebook/public_html

or

 sudo chown -R newuseraccount:newuseraccount /home/newuseraccount/public_html
 sudo chmod -R 755 /home/newuseraccount/

Create Demo Pages
nano /home/newuseraccount/public_html/index.html

Check this article :How to use nano editor

Start by copying the file for the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

example newuseraccount : facebook.com

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/facebook.com.conf

 

Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/example.com.conf

our virtualhost file should look like this:

ServerAdmin [email protected] 
 ServerName example.com 
 ServerAlias www.example.com 
 DocumentRoot /home/newuseraccount/public_html 
 ErrorLog ${APACHE_LOG_DIR}/error.log 
 CustomLog ${APACHE_LOG_DIR}/access.log combined

Also add ifmodule before

  <IfModule mpm_itk_module> 
AssignUserId newuseraccount newuseraccount 
 </IfModule>

Update apache.conf

sudo nano /etc/apache2/apache2.conf

add this lines

<Directory /home/newuseraccount/public_html/>  
Options Indexes FollowSymLinks  
AllowOverride None  
Require all granted  
</Directory>

Enable the New Virtual Host Files

We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite example.com.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
Test your Results
http://example.com


Posted

in

by

Tags:

Comments

Leave a Reply