How To Add New Site Into Your Apache-based CentOS Server
| |Add new site and setup Apache virtual hosts file on CentOS server. Before you start, you better firstly know that adding and editing virtual hosts file on an Apache-based server is useful if you wish to use that server for several websites with each different domain and/or subdomain names. Any way, if it is your really first time, so let’s add the first / new domain name to your server.
Prerequisite
- A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
- Windows users should download Putty while Mac and Linux users can simply use Terminal
- You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
- Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL.
- About 15 minutes of your time and a cup of tea if you like.
The How To Steps
Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.
Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.
You may also need to type this command to go to the root directorty:
cd ~
Step 2 – Add new document root directory for the new website. The “document root” here is a directory where you put / host all files of your website. For that, you can use this command:
mkdir -p /var/www/domain.com/public_html
or,..
mkdir -p /var/www/domain.com/htdocs
some notes:
- Replace “domain.com” with your actual domain name. Do this in all part of this tutorial.
- In this example (in screenshot pics) I use “fikitips.com” as my domain. Why? Because that domain is currently unused so I use it as my testing purpose.
- You can use either the “public_html” or “htdocs” as the name of your directory but in this example I’ll use “public_html”.
Step 3 – Grant ownership permissions to the user. If you’ve switched login from user to root (step 1 above), use this command:
chown -R username:username /var/www/domain.com/public_html
example:
If you are still logging in as user (with root privilege), use this command instead:
sudo chown -R www:www /var/www/domain.com/public_html
Step 4 – Change permissions for www directory. This is important to allow anyone (your site’s visitors) to be able to access your site. Use this simple command:
chmod 755 /var/www
it simply looks like this
Step 5 – Activate Apache virtual hosts file for the new domain. Now you have to setup and configure Apache virtual hosts file to add your new website. In this stage you need to edit “httpd.conf” file. Issue this command:
nano /etc/httpd/conf/httpd.conf
Once Nano editor screen appears, scroll down and fine following lines:
#Listen 12.34.56.78:80 Listen 80
tip: You can hit Control+V to jump to the next page.
Found it? Make sure it listen to port 80.
Next, scroll down again or hit Control+V several times until you see this section (in screenshot pic) below:
Now remove / delete the # symbol. So it’ll look like this:
NameVirtualHost *:80 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. #
The line means any IP address going through port 80 will be a virtual host. However if your server has more that one IPs, you can simply replace the * symbol with your IP.
Once done, right below that section you’ll see:
What you have to do:
- Remove all the # symbols before the <VirtualHost *:80> until </VirtualHost>.
- Change email address at “ServerAdmin” line.
- Change the document root path at “DocumentRoot” line.
- ErrorLog and CustomLog lines are optional but you better also set it up to log issues that arise while maintaining the server.
Shortly, it will look like this:
<VirtualHost *:80> ServerAdmin youremail@domain.com DocumentRoot /var/www/domain.com/public_html ServerName www.domain.com ServerAlias domain.com ErrorLog /var/www/domain.com/error.log CustomLog /var/www/domain.com/requests.log </VirtualHost>
An example:
That’s it. Now hit Control+O on your keyboard to save followed by Control+X to exit Nano editor screen.
Step 6 – Finally restart Apache service and all its processes. Issue this command first:
apachectl -k stop
That will kill all running Apache processes. Next issue this familiar command syntax:
service httpd start
or,..
/etc/init.d/httpd start
You’ll see the OK message indicating everything’s just fine and your server is ready to host a live website.
Step 7 – Give it a test. Now your server is basically ready to host your site but you better give it a test before actually deploying / moving your site in. For that purpose you can use this test page:
/var/www/domain.com/public_html/index.html
then add this:
<html> <head> <title>Apache is really working</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> <p>This is test page for domain.com</p> </body> </html>
Save and exit.
Finally, open your most favorite web browser then access your server. You can do that by typing its IP address or domain name if you’ve updated your domain’s DNS records. It should look like this:
Congratulation, you server is now ready. You can now upload your site’s files to your server via your favorite FTP client app. Read my previous guide: how to use SFTP to upload and manage files on your server. Enjoy..
p.s: You can follow the whole steps above to add another website (with new domain name) to your server
Another tip:
Also do following step if you want to enable .htaccess usage for your website. Open Nano editor to edit httpd.conf file again: You’ll only need to do this once.
nano /etc/httpd/conf/httpd.conf
now find following lines:
<directory /> Options FollowSymLinks AllowOverride none </directory>
Change “none” to “all”
<directory /> Options FollowSymLinks AllowOverride all </directory>
Once done, hit Control+O to save followed by Control+X to exit.
Do not hesitate to ask a question related about this topic. Drop me a comment below. Do not also forget to follow me on twitter to get quicker update.
Hey, You’ve been of tremendous help in setting up a *working* server! I’m a baby when it comes to this, and most pages becomes to techy too fast so you fall of the wagon, but you’ve written it so good that it makes it easy to learn! 🙂 Thanks a lot 🙂 At one point I was even looking for a donate button 😉
One thing I was wondering about was the httpd.conf file. I got an error message about too few arguments after setting it up as per your picture. Read through the conf file, and found a place where it listed some arguments about the CustomLog and I added the “combined” argument at the end under the VitualHost set up. Is this the correct way of doing it? I could kill and start up apache again now without any error messages.
Thanks again for a great site! 🙂
/Jim
Thanks 🙂 you are welcome 😀
Hi
can you tell me please, after follow your above tutorial way i face Internal Server Error 500 .????
hey,how you do that? i also got an error “CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional “env=” clause (see docs).. how can i solve this??
“
nano /etc/httpd/conf/httpd.conf
find this:
ServerAdmin youremail@domain.com
DocumentRoot /var/www/domain.com/public_html
ServerName http://www.domain.com
ServerAlias domain.com
ErrorLog /var/www/domain.com/error.log
CustomLog /var/www/domain.com/requests.log
and change this:
CustomLog /var/www/domain.com/requests.log
to this:
CustomLog /var/www/domain.com/requests.log common
…and that’s all
😉
Nice idea. Thanks Julian
Hey Julian,
Thanks it worked
apachectl -k stop —>
“CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional “env=” clause (see docs) httpd (no pid file) not running”
What do you mean by the ““combined” argument”???
thx mom,your tuts works flawlessly.now our VPS is up-running dan domained 😀
Glad knowing it’s useful for you 🙂
hello,,
thanks for all this topics..
now i have one question and it is :
after all now if we wanna add another website , what to do ?
how can we add another website ???
thanks .
fiman
thanks mom 🙂
Hello,
first I like to thanks this website.today I tried ” Add new document root directory for the new website” here and tested it in my unmanaged little VPS with this code,
mkdir -p /var/www/domain.tk/public_html
but it was not showing some result.then I checked “ls -al” and it is showing something like this.
total 32
dr-xr-x— 2 root root 4096 Aug 26 18:47 .
drwxr-xr-x 20 root root 4096 Aug 26 16:30 ..
-rw——- 1 root root 51 Aug 26 17:29 .bash_history
-rw-r–r– 1 root root 18 May 20 2009 .bash_logout
-rw-r–r– 1 root root 176 May 20 2009 .bash_profile
-rw-r–r– 1 root root 176 Sep 23 2004 .bashrc
-rw-r–r– 1 root root 100 Sep 23 2004 .cshrc
-rw-r–r– 1 root root 129 Dec 4 2004 .tcshrc
So,Can’t I create document root directory in this VPS?please help… 🙂
Great tutorial!
Very well detailed.
Good tutorial. Just need to note that could not find index.html
Here is what I did
I use putty.exe as editor
vi /etc/httpd/conf/httpd.conf at very bottom modified to
ServerAdmin info@domain.com
DocumentRoot /var/www/fikitipis.com/public_html
ServerName http://www.fikitipis.com
ServerAlias fikitipis.com
ErrorLog /var/www/fikitipis.com/error.log
CustomLog /var/www/fikitipis.com/requests.log common
So expect that index is at /var/www/fikitipis.com/public_html
When in browser type ip address of server, see Apache 2 Test Page powered by CentOS and so on You may now add content to the directory /var/www/html/
Then
[root@vps ~]# ls /var/www/
see
cgi-bin domain.com error fikitipis.com html icons
Checking content of directories
ls /var/www/domain.com/public_html, ls /var/www/fikitipis.com/public_html, /var/www/html/ are empty
Where is index.html?
Did touch /var/www/fikitipis.com/public_html/index1.html then vi /var/www/fikitipis.com/public_html/index1.html, typed a, then wrote some text in file, then Escape and shift+zz. And in browser http://111.111.11.111/index1.html and see what I had wrote. So until now seems that all works
Great work!
I have a site with subdomains that I’m migrating to VPS.
How can I create sub domains like
m.example.com for folder /var/www/example.com/m
forum.example.com for folder /var/www/example.com/forum
Thanks
Hey you have been great help, at the step where you have to bring up yhe nano screen “nano /etc/httpd/conf/httpd.conf” i put in that command and it says command not found!!!! pls help its all been working till now!
thx
install nano at first. yum install nano
I try to use command
“nano /etc/httpd/conf/httpd.conf”
it open nano editor but this file is empty. what I have to do?
it is either httpd.conf file is not already there for the very first time or you’ve just typed the wrong command. Just try again 🙂 or check whether httpd.conf is already there or not with ls command.
Here’s what I mean:
if httpd.conf not exist what should i do ??
you can simply create that file
You the man … thanks a bunch … !!!
and so ya dude 🙂
For the Text Editor, you can likewise use ” vi ” editor, just as “nano”.
For Example, [ vi /etc/httpd/conf/httpd.conf ] without the quotes, you type ” i ” on your keyboard, this is to enable editing mode.
When done editing, you Hit ” Esc ” tab on your keyboard, this is to return back from editing mode.
Then type ” SHIFT ZZ ” that is shift and ZZ. This will write in your edited files, approve it, save and return back to your Terminal.
Smiles Hope you Enjoy it !
Hi Mom!
Thank you very much for this useful tutorial.
When tried to restart apache after editing the config file I got this error message.
[root@******** ~]# apachectl -k stop
Syntax error on line 1009 of /etc/httpd/conf/httpd.conf:
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional “env=” clause (see docs)
After I commented that line again, I was able to restart apache without any errors.
Is there any fix for it?
Also I did some changes in the conf file those you haven’t mentioned in the tutorial.
1. Under section 2 – I changed ServerAdmin to my mail address and ServerName to my SERVER IP:80
2. # AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None I changed this to All
Will these changes cause any troubles?
No, that should not cause any trouble.
Anyway, in which line exactly did you uncomment and comment it again? 🙂
CustomLog /var/www/domain.com/requests.log
You can simply remove this line
CustomLog /var/www/domain.com/requests.log
or use this one instead
CustomLog /var/www/example.com/requests.log combined
Hi,
I had the same problem, but when I use
CustomLog /var/www/example.com/requests.log combined
I have now this http://prntscr.com/2xqtww
What’s up, I just noticed that sometimes this page renders a 404 error. I thought that you would like to know. Thanks
Dear Mom,
Thanks for this great tutorial. But I have some trouble that I can’t have solved for a week. It’s about “The Write Access is not enabled” I have been installing a CMS, but it always doesn’t figure it out. The alert is
——————————————–
✗ ./data/configuration/ does not have write access or does not exist
✗ ./data/articles/ does not have write access or does not exist
✗ ./data/commentaires/ does not have write access or does not exist
✗ ./data/statiques/ does not have write access or does not exist
✗ ./data/images/ does not have write access or does not exist
✗ ./data/documents/ does not have write access or does not exist
——————————————–
I tried to chmod 755 but no effect at all. would you please give a light?
Thank you..
chmod 755 will work if the directories are owned by running process, in this case is your webserver. You should chown those directories to your webserver “httpd”. Otherwise, you can simply chmod 777.
I don’t understand what the “owned by running process” means. Would you please give me some commands to chown them? I am really baby in this case. I guess the case is a restriction of the server. But when I tried another server from XYZ hosting provider with fresh installation and of course using this tuts but I configured it in the root, the problem is still same.
I’ve googled this before and also about Chmod 777. There are a lot of article said that chmod 777 was a bad idea for security matter.
I am going crazy dealing with this.. 🙁
First of all, I will like to thank you for the tutorial. It’s very helpful and easy to understand. I just have a question about the part you said “You can do that by typing its IP address or domain name if you’ve updated your domain’s DNS records” Can you explain a bit further of how can I update my domain’s DNS records?
I already can access my website by typing its IP address, but I want to access it by it’s domain name too, in case I want to host many website by using many domain names in one single IP address.
To do that, please read my other guide about “pointing domain name to your server“. And if you look for some free DNS manager services, also read “huge list of free DNS hosting providers“. Thanks for your question 🙂
Thanks a lot, Mom. This really help me. 😀
I can’t thank you enough for such a well written and easy to follow guide to LAMP servers. I stumbled onto your site after two weeks of trying to get this working based on the advice from various Linux forums and, after two weeks of unmitigated failure, had a working server in less than two hours. It’s not often that you can find this level of support from the Linux community – so thank you for making an easy to follow, step-by-step guide to get my company intranet server off the ground.
Hi thanks for the beautiful tutorials,
I am stuck in the end, when In last step
” 6 – Finally restart Apache service and all its processes. Issue this command first:
1
apachectl -k stop
That will kill all running Apache processes. Next issue this familiar command syntax: ”
When I enter “apachectl -k stop” I get
Syntax error on line 962 of /etc/httpd/conf/httpd.conf:
Invalid command ‘To’, perhaps misspelled or defined by a module not included in the server configuration
httpd (no pid file) not running
Any ideas? I checked and spelling is perfect everywhere.
Thank you
Try restarting apache with command
service httpd restart
.If that doesn’t work, then you really should double check your httpd.conf file. I believe there is typo or may be some accidentally text inserted.
Hi,
Great tutorial. I learnt heaps. I am unable to see any themes. When I click Add Theme, the screen doesnt return any. Im guessing I have a setting somewhere wrong. Im on LAN behind a proxy. I also have not got any port forwarding configured on the router..
I can see Plugins ok.. just not themes ..?!?
Hello, I want to first say thank you for having such awesome detailed instructions. But second I want to point out a step that might need to be changed. I don’t have the overall experience to say for sure. What I can say for sure is that the Apache service has it’s own user and group. So when you run the command: chown -R sawiyati:sawiyati /var/www/domain.com/public_html, I think you are making a slight mistake. I’m sure the server still functions with that user and group, but Apache has it’s own user and group. I would guess that you mean to type: chown -R apache:apache /var/www/domain.com/public_html
As it turns out, you might have changed the User and Group for the apache service in a previous section or post. I found out apache’s user and group can be changed in the /etc/httpd/conf/httpd.conf file… But I am now wondering why you would change it to anything but apache:apache…. security vulnerability maybe?
yes, Apache’s user and group can be changed. In CentOS, you might sometimes see it runs as httpd:httpd or apache:apache. You are free to chown-ing web directory to any user and group. Most common (easiest) method is to chown that folder to the user and group that your web server is running as. But you better do some additional security tasks and better not to leave your server as it is. I mean, at least you better have suhosin, suphp, modsecurity, etc. installed.
Hello Sawiyati,
its really good documentation. I have 3 sites to host . I need to share username & password to my 3 user so that they can upload file to their respective sites folder.
Can you please help me on this how to configure the same .
Sorry but I don’t quite understand the key point of your question. What kind of configuration did you mean ? 🙂
Sorry for the inconvenience, I need to configure 2 website in same server with Apache(virtual hosting ) . web developers will upload webpages directly to the folders (var/www/domain.com/public_html/x.com & var/www/domain.com/public_html/y.com), means user(developer) can upload webpages directly to there own site directory.
How to do it ? did you get it now ?
you can create unix user for each domain. Or, you can simply use one user and give access to your developers. The main important one is you should create virtual host entry for each domain and it is better to use /var/www/x.com/public_html and /var/www/y.com/public_html so you don’t get confused 🙂
thank you, I will check & get back to you .
Can you tell me a simpler setup where I can host all my sites under one big directory ?
like the htdocs in Xampp?
I don’t need a separate user for each virtl direcotry, only need to host 15-20 websites on one VPS
Can there be a script to create Virtual directory without having to go thru all these steps/permissions etc ?
Then you can try zpanel, vestacp or even WHM/cPanel 🙂
/var/www/fropile.com/public_html/index.html: No such file or directory
my public_html has no index.html.
what should I do?
upload a index using filezilla
use the touch command to create the file:
touch /var/www/website.com/public_html/index.html
then:
nano /var/www/website.com/public_html/index.html
Use sudo if nessesary…
thank you sawiyati 🙂 🙂 🙂 🙂 🙂
Great !!! Thanks you !!!
mom ,
how to add subdomains. :/ :/ i cant understand :'(
my domain is working.but my subdomain is not working.
this is my httpd.conf file -> http://oi61.tinypic.com/3091tm1.jpg
but browser saying “Server Not Found”
Hi servermom, this is an amazing website and the simplest centos tutorial on internet. I am impressed…very nice work.
Unfortunately, creating VirtualHost in httpd.conf does not work in centos 7; v7 httpd.conf instructs you to load config files in the “/etc/httpd/conf.d” directory. I have tried to adapt your guidance to a “.conf” file in that directory, but httpd will not start or restart with my edits. With the default “.conf” file in “/etc/httpd/conf.d”, I can view the test webpage at my server IP.
Can you please author a tutorial on creating VirtualHosts in centos 7? It would be very helpful and much appreciated since there is not yet much written about this subject.
Thank you for your website!
–keith
You can read my other guide to setup LAMP on CentOS 7.
how to connect the domain with the server?
read my other guide here.
Thanks for this useful tutorial! You’re my favorite server administrator tutor!
Hello Sawiyati! Your website is awesome!! I ‘m having problems with my site, it Works great on my internal network and external site withour www, when i put http://www.mydomain.com he asks login and password. i dont know how to stop it! can u help me?
Does this work the same way with subdomains? I have tried it, and changed the DNS to it, but it doesn’t seems work :S
I’m trying to configure the http.conf file, but I cannot see the section for VirtualHostName. It does not exist in my file! What happened?
Hello Sawiyati,
I Googled for CentOS 6 help and your website appeared thus far I’ve not had any issues following your steps until now. I keep getting the following error message and I copied and pasted what I have type in the vi editor any ideas what could be the problem?
How do I host more than one website on my VPS? Is it even possible with only one IP for my server?
Of course that’s possible. That is what common shared hosting does. Simply create another Virtual Hosts entry for your another website/domain
i added 1 more domain to vps and add Vhost conf but my new domain redirect to old domain.
plz help
Hi mom, when i try to execute this, i got an error.
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional “env=” clause (see docs) [FAILED]
Sorry for my last post,.. i fix it already.. my problem now, when try to type the domain.com at the browser it will redirect me to the internet.. How to fix it?
Thanks Mom! This is a great article but I have one question. Exists a script that can automate it? For example command:
domain add mynewdomain.com /var/www/
domain remove myolddomain.com
subdomain add subDomainName mydomain.com
subdomain remove subDomainName mydomain.com
thanks for the reply! 🙂
When I type the last step:
/var/www/alamwisata.com/public_html/index.html
the comment on screen, display:
bash: /var/www/alamwisata.com/public_html/index.html: No such file or directory
What is mean Mom.?
when i do search my website nothing appeared,, but when i put vps ip then it appeared….
I added site, but it keep showing default page, even after I add index page, I tried multiple times pls help me to fix.
double check your virtual hosts configuration file and try to restart httpd
Hi What do i do for step 5 if i want to add an additional domain name (different website) to host it on the same server? Thankss!
Simply create additional Virtual Hosts entry after the first one
Hi, what do i do for step 5 if i want to add a new domain name (different website) to the same server? Thanks!
it says
httpd: apr_sockaddr_info_get() failed for Werniel
httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
httpd (no pid file) not running
im a newbie in linux and i dont know how to fix this please help. p.s. Werniel is my hostname
Please read this:
– Fix Apache could not reliably determine the server’s fully qualified domain name
– How to change hostname to FQDN format
i am getting this error please can any one help me after every step when it issue the command to to http i am getting is
Syntax error on line 1009 of /etc/httpd/conf/httpd.conf:
double-check that file exactly at line 1009, there must be something there you have to edit
Thanks for sharing your thoughts on centos. Regards
This is VERY outdated the conf file has deprecated the NameVirtualHosts line, it doesn’t exist any more. It must have been replaced with something else.
in order for this guide to work it would need to be updated to reflect that.
go with this guide:
http://servermom.wpengine.com/install-lamp-centos-7/1741/
I have followed the four tutorials to get this far (I’m setting up a VPS dedicated to a single WordPress site) but get connection timeouts when I try to browse to the server I’m just getting connection timeouts.
There is an A record for the hostname (production.domain.com), and I’ve also tried the IP address and setting the IP for http://www.domain.com in my HOSTS file.
All the apache logs are empty (apart from the usual apache restart notices).
dear miss sawiyati…i followed all the step mention above,all went well without any error..but still my domain cannot be resolved? i cant load from browser and i cant ping my domain ussually when we ping it return an ip adress right?should we change the domain name server also ?
you should also make sure your domain has been pointed to your server either using your own NS or third-party DNS manager
First of all thank you very much for providing nice post.I have one question.could please help me.I have purchased one domain in godaddy and my vps in net4india.I have followed all the steps what you given.I have configured also.But web site is not openning.Still any step needed to godday. please help me
i got this error
http://www.hostinger.in/error_404?
Great, thankyou so much.
Hi Swayati,
I followed your guide to built a local dedicated server i must say i am gratefull went smoothly, i have a question for you i run centos 6.4 server with LAMP. My website is e moodle learning so when i uploaded it on server some fo my scorm learning packages did not work correctly i was wondering if theres any kind of java that should be on server also some symbols like cubic meter not displaying on scorm packages i was wondering if my server config missing somthing.
I have a question for you i run centos 6.4 server with LAMP. My website is e moodle learning so when i uploaded it on server some fo my scorm learning packages did not work correctly i was wondering if theres any kind of java that should be on server also some symbols like cubic meter not displaying on scorm packages i was wondering if my server config missing somthing.
I’m so sorry for this Rana, because I never have any experience with moodle before.
Hello Sawiyati,
Your lessons are deep, step by step and very helpful. I have different situation than explained/comments above. I have a MAC Database server with a front end on my network while both are setup in linux. Back end database is working fine, while the front end started giving problems. This front end is installed on Fedora 7 on an IDE HDD, the frontend is written on php and located in /var/www/html. I am planning to move the frontend application configuration to CENTOS 7. I have tried the above tutorial/comments. I were able to get the apache default page at one point. But after copying the contents from Fedora /var/www to new CENTOS, the apache home page is not loading any more and the mac frontend login page either. This front server is located in LAN and used with in lan. Can you please walk me through how to configure a php based app on CENTOS. Thanks in advance
Hello everyone.
Im trying to add a new site to my CentOS 7.2 in Azure, but i on Nano Editor, there’s not “VirtualHost” tag and VirtualHostName line.
What should i do in this case?
Hello I try this step on my centos.
But nothings happened.
ill do all steps but when i try to access the link i make. its not workin. but when i try my . IP adress,, its change and said its working,,… how do i do this?