Web Developer Workflow (8 of 10) – Production Server: Apache and VirtualHosts

hey everyone in this video we’re going to be setting up a patchy and virtual host so we’re finally going to be able to make some websites and host them and this is the video that I’m really excited for because this is this is what you know we want to set up server spores to host websites the previous two videos if you didn’t get a chance to see them we’re all about setting up our server and getting us to a certain point so that we can have certain levels of security so you might want to watch those but if you’re if you already passed that point on your own then we’re pretty much ready to go and set up apache and some virtual hosts okay so right now i’m logged into my server and i’m at my home directory it doesn’t really matter where you are for these next few steps though but that’s where i am and the first thing that we’re going to do is sudo apt-get update and a apt-get is a program that basically downloads other programs for us so if we’re going to be installing apache we’re going to be using apt-get for that if we’re going to be installing PHP we might be using app get for that too apt-get is going to be used in a lot of cases and so we want to update it so that our repository has all the latest information and the repository is like a big list of trusted software for this particular version of ambu too okay so we’re going to do a clear here and the next step if it’s to install Apache you might want to check and make sure that you need to do this because perhaps you already have apache installed if you’re doing this rackspace thing like i’m doing then you won’t have a patchy installed but if you’re doing a different hosting company then maybe there’s a good chance that you do so let’s do a really quick peek into the Apache folder and so if you do ET c apache to this folder should not exist if you if you don’t have apache installed and for me it does exist because i did set up a patchy just a few minutes ago before i made this video so it does exist for me and if you see this list of stuff here than it does exist for you too but just in case it doesn’t exist the only thing that i did to get to this point was sudo apt-get install apache2 and i’ll do that again with you here for me it’ll just be an update perhaps for you it’ll be an install and so for me it just takes a few seconds for it to realize that there’s nothing new to update but for you if you’re doing an install it might take a few seconds maybe maybe even a few minutes okay so we’re pretty much ready now you should be able to do this this LS for UTC apache2 okay so let’s go over to that folder where we’re actually hosting our websites okay we’re going to go over to VAR dub dub dub and this is also a folder that didn’t exist before you install the patch me if we do an LS here you can see that there’s an HTML folder and if we kind of peek inside of there let’s let’s see the inside of here and do an LS you can see that there’s an index file let’s open that index file up okay so here’s what it is not that you need to study this too much but just to show you that Apache by default does set up their whole environment as if you know you’re only going to have like one website going on it’s going to be like this default website and we’re actually going to be making multiple websites and so we’re going to keep this thing around for just a few minutes but just I just want to kind of show you what they have going on here so if we CD up one level back into the dub dub dub folder we’re going to actually make our own few websites we’re going to make one called example so we’re going to do a make der called example you can call it whatever your project name is oops and we don’t have permission to do that and that’s because the root user I think or the Apache user whichever one owns the varge dub dub dub folder and the Brad user can’t just go around making new things inside of it unless I use the pseudo in command and it turns out that we’re going to be wanting to make lots of things inside of here so let’s actually change the ownership of the VAR dub dub dub folder and all of its children folders to be owned by Brad so we’re going to do CH 0 W in which stands for change owner and we’re going to want to change recursively which means we want all of the subfolders of far w Bubba also to be changed and the user we want to change is ourselves so we have to do this this is the this is the user and this is the users group all users have a group by default and if you want to learn more about that you can somewhere else in another video because I’m not going to go into it any more than that and then here you do basically the path that you want to change ownership for for Brad

tone recursively okay so it’s going to be barred dub dub dub and actually this command has to be ran with sudo as well so we need to make sure we do that okay so now if we do our make der for example it lets us do it and if we do an LS we can see the example right there and so let’s CD into that and as you can see it’s empty obviously because we just made it so we need to make two new folders inside of here one of them is going to be called htdocs and another one is going to be called logs and notice that you can make two folders at a time with make der as long as there’s a space in between right here hit enter and those are our two folders okay so here’s the idea logs is where we’re going to log any errors for the site and also all the access logs will go inside of there and htdocs will essentially be our web rubes okay htdocs is where you’re going to make your index file for the website and eventually so let’s go and set up apache to acknowledge that we have this new site set up we’re going to CD over to UTC apache2 and then into a folder called sites available an inside of here you can see that default one that they set up for us and the default site comes with two files the the regular configuration file but also the ssl configuration file we’re going to be completely ignoring ssl in this video but let’s pay attention to this one right here this is the file that tells Apache that we have a site set so we’re actually going to make two sites right the example site so we need to copy that file and make a new one very similar to it so we’re going to do maybe CP and we’re going to copy this file into a new file called example dot conf oops and we need to add permissions for this as well this is not a folder that I want to change the ownership for to be myself so we need to do the sudo command but just so you know I don’t think I’ve shown this in any of the previous videos if we need to run the previous command as the pseudo user I guess you could type the whole thing over again or you can use the up arrow and use the back arrow to get to the beginning and type the word sudo but there’s actually a more clever way you could just type sudo and then to explanation points and that will run the previous command as if you had type sudo all along ok so now if we do our RLS you can see here is the example conf folder so we’re going to get into this and you know what i need to do sudo and this is what a virtual hosts file looks like so we’re going to actually clean it up a little bit because it has a lot of notes that we don’t need if you want to read them on your own that’s fine but I’m going to use ctrl k to delete things line by line and I’m going to get rid of all these things they have the pound sign in front of them except for this one because we actually want to use this server name so I’m going to enable that and this is where you basically get to say what your domain name is for this particular website on the server so we’re going to keep it as example calm because that’s exactly what we want and we’re going to just go down here and set up the server admin to be myself and if you were to set up email software on this server potentially the server now would email this email address if there were any problems with the server it’s not going to do that now because i didn’t set up that near that email software right now but in the future maybe I will okay this is a really important part this is where you get to say what the web root is of your project okay so we’re going to do VAR double double and then remember we set up htdocs and I kind of forget if I explain what that meant HT top stands for hypertext documents and it’s a pretty common name for someone to name their web root you can actually name it whatever you want but people would would understand what it was if you’re in conversation and they saw the htdocs folder they would anticipate that’s probably your web root because it’s pretty standard ok so then down here we’re going to get rid of a few things again okay we’re going to do our control k and we don’t need the ssl stuff so we can get rid of that as well and then going down here we can get rid of all these things as well okay so this is all you need and we don’t really need any documentation because it’s not really that much and it’s pretty easy to understand so far so here is where we set up the location of our air log and our access log the air log is going to get filled up with any time there’s an error on the server such as a missing

file like a 404 error or perhaps PHP has an error all those things will get logged into a file called air log inside of well right now at least is saying inside of Apaches default directory for logs and then inside of an access log is going to be every single time someone accesses the site so if you keep this just like this and then we set up another virtual host for let’s say our next project and you do this thing the same way we’re basically saying that we want all air logs and all access logs to be kind of combined into into one kind of master login access log for all of our websites and that’s not really how I like to do it because I like to have each project have its own so that’s why we set up that that logs folder so we’re going to do bar dub dub dub example logs and then I know that that file doesn’t exist yet inside of there because all we did was create the folder but Apache will create that for us when it needs to okay so we’re going to exit out of here and we’re going to save and if you didn’t open up that file using sudo then it wouldn’t let you save right now I would say you don’t have permissions and so that’s the significance there okay so now that we have everything to this point we need to tell apache that we have this new file example and it’s a new virtual host and we want to enable the site so the way we do that is we do well sudo a2n site and then we mentioned the configuration file name a to incite stands for patchy to enable site so we do that and that enables the site but then we have to restart apache so we’re going to do sudo service apache to reload okay and we’re good to go so now we have two sites going we have the default site and we have our example site just wanted to show you something real quick though let’s actually you know what we didn’t even set up a home page I think we need to do that so let’s go over too far dub dub dub and let’s do example and then the htdocs folder okay so we’re going to CD over there and right now you can see that it’s empty and let’s make a little index file real quick so there’s a few ways to do this actually this is a good opportunity for me to show you a few ways to kind of set up a file you know any time you ever want to make a file you could do nano and just make up a file name right here and you can open it up and say that make changes and that’s one way to do it another thing you can do is you can type touch and then any file name you want and that will actually make a file it’ll be empty and then you could nano into it and make some changes okay I’m going to remove that but there’s actually kind of a clever way to not only make a file really quick but put some content inside of it so what you do is you type echo and then we’re going to say new actually will say example website example website make sure you have the quotes there and then we want to put that inside of index dot HTML kind of cool okay so now we can just do an LS and it makes our index HTML file and if we nano into that you can see that it has that content okay so now that we have that how do we access this site in a browser well if we go out to the browser and if you still have your Rackspace account open you have your IP address right here and it’s also listed down here and if you go to that IP address in the browser it pulls up this page this is basically that index file that was inside of that default virtual host that we didn’t set up the one that was already there okay so please CD up two levels now we’re at bar dub dub dub and we do an LS remember this HTML folder and remember this index file okay so that’s essentially the DNX file that we’re looking at in a browser when we go to the IP address so why is it that we have two websites set up right now but when we go to the IP address it is choosy about which one wants to show well it wants to choose whichever one is alphabetically the lowest so in other words the 000 default whichever one i guess i should say whichever one has a configuration file that is first alphabetically which is why I think Apache calls that default file 000 to start it off with because they want that to be your you know your lowest alphabetically now in the past

what I’ve done is disabled that project so let me show you what we could do to disable it and then i’m going to show you kind of a newer way we’re going to do sudo a two-disc site which stands for patchy to disable site and then we’re going to do the 3000 default config file so we’re going to disable that and then we’re going to do sudo service apache to reload okay so since we’ve disabled that site now the website that is enabled and first alphabetically is our example site so if we were to go to the browser and hit refresh we get our example site but you know as we make more and more sites I don’t think I want it so that the whichever one is lowest alphabetically happens to show up if someone goes straight to the IP address because all of our websites are going to share the same IP address I want people to use domain names to go to these sites and if I have a Miss configuration I just don’t want the first one that’s alphabetical to show it by default so I’m actually going to do this this is what I’ve been doing lately I’m going to enable that site again and I’m going to run that Apache reload okay so now we’re we’re back at that point where the default is the is the default or you know enabled and I’m going to go in here and i’m actually going to remove this index file and I’m going to make my own I’m going to make one right here I’m going to do an echo for you know i’m going to call this wo one okay so it makes that so now when someone comes to my server just by typing in the IP address they’re just going to get this right here which is meaningless to them but it lets us know that we landed on our default so I’m going to leave that just like that okay so then you might ask well how do we access the example page well we need to set up domain names for that if you’re going to have a real website and so that’s actually going to be in one of my later videos but let’s say this is just for example purposes you just want to see like this is maybe a development server and you just want to have a little fake domain name kind of like what we were doing in our previous videos for some stuff so what you can do is let me let me actually open up the local computer so I’m going to keep this this tab open and i’m going to do command T so right now i’m at my macbook and if we do a sudo nano for our hosts file if you get in here you can see some stuff that we did for some previous videos and if we put that IP address that we’ve been using for our new site and we put wwl calm and exit out of here now I think this will work we should be able to do that there we go okay so we are accessing a live web site at Rackspace using a fake domain name but nonetheless accessing the live website and as long as you have this as long as you have this file your hosts file setup you can do a fake domain name don’t do this if you’re going to have a real domain name though ok so then over here if you remember oops I don’t know why that was blank ok so over here on the real server if you remember we were in that that file let’s see let’s go over to UTC I patch you two sites available if we do an LS and we do our let’s just do a cat so we can just kind of peek inside of this example file okay as long as you have this right here set up you can have a domain name point into this virtual host okay so just to kind of show you because that was kind of a slow with all the explanations let’s just show you how quickly you could set up a new website if you didn’t have as much explanations going on in your head so here we are at the sites available and we have example conf so let’s do let’s do a copy of that and let’s make a new one called example two oops permission denied okay there it is okay so we just need to make a few

little changes exit out of here okay so now we can see D over 2 VAR w ee missy the into that ok so here we are a VAR w double to ok CD into here ok so we just set up a patchy and set up our new site the only thing we haven’t done yet is enable this site and restarted apache so we’re going to do that now okay so potentially this website is on the internet right now except for there’s no domain names point into it so just like before we’re going to open up a new tab and get into the our hosts file and that’s it see we can go over here now and if we refresh on example we still see the example website and if we go to example to calm we get example 2 so that’s pretty much it that’s virtual host and now you can have as many websites on the server as you want they will all share the same IP address that usually doesn’t matter but just to let you know stay tuned for our next video hope to see you there