Setup Apache with our first webpage (vhost)

T

his tutorial works for CentOS only. For your specific OS tutorial use the tags or serach function in the sidebar area.

 

Now we'll configure our Apache web server to host our first webpage.

DNS

Before you proceed this tutorial make sure that your domain/subdomain is fully propagated in your DNS service. Sadly using Hostinger DNS it takes up to 48h.

Previously We installed/set-up our Apache server in THIS tutorial.

Single-user mode

Remember, We are still assuming and following a scenario that only one user on our virtual machine hosts multiple webpages. This is why if you create multiple users with their own webpages - you simply fail - sooner or later - into permission errors with this Apache configuration.

So, we choose 'jtkirk' user as a owner of all webpages and apache process will run under that user.

Edit: httpd.conf

$ nano /etc/httpd/conf/httpd.conf

Search for

User apache
Group apache
 

and CHANGE it to:

User jtkirk
Group jtkirk
 

of course use own username.

Save, exit and restart apache.

 

In last tutorial we created a directory called 'domains' in '/var/www/html directory' - and we set it as FTP root directory.

So, log-in now via FTP client and create two dirs /domains/example.com and /domains/example.com/public_html

or

$ mkdir -p /var/www/html/domains/example.com/public_html

of course instead:
          - 'example.com'  : your domain name that points to VPS

 

Next, make directory for domains logs:

mkdir /var/log/httpd/domains

Set the owner and permissions:

$ chown -R jtkirk:jtkirk /var/www/html/
$ chown -R jtkirk:jtkirk /var/lib/php/
$ chmod -R 0755 /var/www/html/domains/
of course instead:

          - 'jtkirk'  : your username

 

Next we upload our webpage, or create test index.html in /var/www/html/domains/example.com/public_html/index.html

CD in your public_html directory, i.e. Paste this code:

$ nano index.html

Save and exit.

Adding virtual host

Now we have to tell the apache what and where is our webpage:

$ nano /etc/httpd/conf/httpd.conf

Search for #NameVirtualHost *:80
and uncomment (remove the hash)

NameVirtualHost *:80
 

At the very bottom of the file ADD:

Save, exit and restart apache.

$ service httpd restart

[root@server][/etc/httpd/conf]
$ service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
 

Visit your webpage in your web browser.

Test apache write permissions

Since we follow single-user scenario and don't want to allow writing for everyone (0666) - it's wise that we check that it's working OK.

Create test.php file in your public_html/ directory and PUT this code in

Now, http://domain.com/test.php ..... You should see only blank screen BUT a file test.txt is created.

You can confirm this refreshing FTP client file list if still logged in or via terminal.

If it's there. You're successfull. Everything works OK.

If there's NOT = that means either

1) you didn't change Apache user/group to the one_and_only_user that will be uploading/managing websites or
2)logged onto FTP server as other user,

in both cases you could see error message in:

$ cat /var/log/httpd/domains/example.com_error.log | grep "PHP Warning"

[root@server][/var/log/httpd/domains]
$ cat /var/log/httpd/domains/example.com_error.log | grep "PHP Warning"
[Tue Aug 13 06:16:49 2013] [error] [client xx.xx.xxx.xx] PHP Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/domains/example.com/public_html/test.php on line 2
 

So, the user you log via FTP and upload files must be the same as user who owns Apache process. Simple.

In future I'll cover multiple-users, Apache shared environment solutions.