Complete guide: Install, Run and Manage Webserver on Ubuntu: Chapter 1
A Brief Introduction to the whole Tutorial Series:
Target Audience: Absolute Beginners to Web Development, especially for learning the basics of web server management. Written in detailed manner to make newcomers interested in linux web development and linux in general.
Target Operating System: Ubuntu/Kubuntu (Mostly, only the installation differs but configuration setup remains the same for any Linux based OS). So, if you know how to install packages in your linux distro then there should be no problem following this guide.
Software Packages List to be installed in this tutorial series: Apache, PHP, MySQL, PHPMyAdmin, cURL, Nginx are currently planned in this guide. This list will updated when chapters are planned for more packages.
Usage: Host a complete working live website, hosted either on a Virtual Private Server or on your own computer.
Other requirements: Good internet connection to download necessary softwares for the server if installing on your own computer OR decent resources of the VPS if installing on a VPS.
"Your own website on your own domain (like .com or free domain like .tk) on your own PC which pops into action as soon as you turn on your computer, and dies as soon as you switch off your computer"
This guide won't give up until you 'setup and host' a website completely from your own computer or a VPS. This guide is written to be interactive (uses "Guide Search" to quickly move to different sections), so feel free to pull out any step / subsection you did not make through or did not understand. We can discuss about that in more detail. Just quote out a forklet (sub section) you need help with. Although the setup instructions are written for Ubuntu Operating System (OS), the instructions for configuring the server should work for other Linux based OS'es too, as the program configurations are 'generally' program specifc and not OS specific.
So, why would you want to host on your own PC?
Hosting on ones own PC gives the freedom to do rigourous tests on a live webstite. How?A typical PC today has hardware resources many times higher than the resources offered under shared hosting or even Virtual Private Servers (VPS). Thus, we can limit the resources of our own computer to test under various resource requirements and choose appropriate hosting.If the website is your personal space, then there is no better place to host on, other than your own PC, so that you can access data anywhere in your network, like Laptop, Sound Systems, Desktop PC, Handheld devices like smartphones, etc.There is no need to upload files to remote server, so development speed is greatly enhanced while testing out on a live website.If you have a PC which stays powered ON all the time, then you can easily host a small to medium site.
If not your own PC, then on a Virtual Private Server:
A Virtual Private Server (VPS) is a virtual operating system intstalled inside a big powerful physical server. Generally, there are many VPS's installed on a Physical server. Even though all the VPS's installed use the same physical server, the resources are allocated separately for each of the VPS. A VPS behaves like a completely independent PC, where we can install own OS and in fact the softwares installed inside a VPS knows only about its parent (VPS). A VPS has no connection to the big physical server (grandparent).
I chose Ubuntu as the target OS for this guide, since this is a beginners guide and assumes little or no experience of managing web servers, its configuration etc and as Ubuntu being a newcomer friendly, it is easy as well secure enough to host your own website. I chose a standard Ubuntu 10.04 LTS CD as the base. It does not come with any server softwares to run a web server, and hence the article shows you how to install it and configure it.
A note about Handy command search used in this guide (Guide Search):
To make it easy to know often used commands, I use a simple method to categorize and enable you to easily and quickly move between the command at hand and its description (given on every page of guide at the start of the chapter). This avoids repitions of common commands. This also makes you remember and make you think about the command by not showing the command initially, but at the same time allowing you to quickly know about it if you did not remember it and get back to section you were reading.
So, later in the guide, when I want to show you the commands listed in 'commands list', I will show there a 'guide quote', something like @restart-apache9. Then to quickly get to that command listed in commands list at the top, type @restrat-apache@ in your browser search (Hit Ctrl+F). And to get back to the section you were reading type: @restart-apache9 and you will be back where you left. The last character (like 9) is a number which makes it unique to reach the exact point you left.
So lets get started.
Note: The instructions are provided in as detailed manner as possible in the beginning of the guide. As the guide moves on futher, these basic general instructions are not repeated so as to prevent deviating from the topic at hand.
Chapter 1: Installation, Configuration and setting up a html website in Apache
Step 1: Installing a Web Server (Apache)
List of commands used by 'Guide Search' in this page:
List of handy apache 2 commands. Replace the angle brackets (<>) and text inside it appropriately (Each of the commands are explained later in this page). The text after '#' is a comment written to describe the command. I use 'Guide Search' in this whole tutorial series to be able to quickly find more about a command at hand./etc/init.d/apache2 restart #@restart-apache@ #Apache: Restarts the apache server a2enmod <MOD NAME> #@enable-module@ #Apache: Enables a module a2dismod <MOD NAME> #@disable-module@ #Apache: Disables a module a2ensite <SITE NAME> #@enable-site@ #Apache: Enables a site a2dissite <SITE NAME> #@disable-site@ #Apache: Disables a site
Apache is the most popular web server. Another popular, compact and efficient server is Nginx. In later chapters we will see how to run Nginx as an alternative to Apache or running both Apache and Nginx. I will Since Apache 2 is widely used we will go ahead and install it now.
First open up the terminal. Go to 'Applications' menu > 'System' and click terminal.
TIP 1: Since we will be using the terminal a lot, we can create a quick launch by dragging it on to the task bar.
Note: I will be entering all the commands in this guide by entering into sudo mode. To enter into sudo mode, type 'sudo su' and then enter your user password. Now you can enter any commands, even which requires root permission, since we are logged in as root for that terminal session.
Use apt-get to install apache by typing the following in the command prompt:apt-get install apache2Enter your user password. Press y to confirm downloading the Apache2 package. Cogratulations, you just installed the web server. Start apache by typing/etc/init.d/apache2 startORservice apache2 startWe can test if apache is working by entering the url 'LOCALHOST' in a browser. You can also use your hostname instead of localhost. To know your hostname, type hostname in terminal. So now, the server should display a default test page. We will see how to add files to the website soon.
The following packages are installed:apache2apache2-mpm-workerapache2-utilsapache2.2-binapache2.2-commonlibapr1libaprutil1libaprutil1-dbd-sqlite3libaprutil1-ldapapache2-mpm-worker http://httpd.apache.org/docs/2.0/mod/worker.html implements Multi Processing Module. Thus, each process can have multiple threads running. libaprl packages are support libraries.
Here are some notable directories and files which apache creates during installation:$ /etc/apache2All the configuration settings of apache stored here. We will mainly edit the configration settings by editing files in this directory.
/usr/share/apache2/Some default data stored here. No need to edit anything here.
/var/cache/apache2/Stores cached documents
/var/www/Default directory to store files of the website. We will see how to change this root directory of our website later.
Step 2: Configuring Apache2
Where are website files saved?
Ok, so now we have our webserver running at LOCALHOST. But where are the website files saved, so that we can add to, delete or modify the pages? By default, the directory is: '/var/www/'.
How to add/modify/delete the website's files?
The directory '/var/www' is under root users control, so you need to edit/add/delete files as super user (sudo <COMMAND>). If you need to quickly learn the common linux commands for file and directory management on command line, check out this brief cheatsheet: http://freeengineer.org/learnUNIXin10minutes.html
Another more easier and graphical way is to run a file browser like Nautilus in Ubuntu or Dolphin in Kubuntu as a root user. To open Nautilus (for Ubuntu) as root, type the following in terminal and then enter the:gksu nautilusTo open dolphin (for kubuntu) as root, type:kdesudo dolphin
TIP2: There are lot of useful directories which you need to browse often like apache configurations folder (/etc/apache2/) and server root directory (/var/www). You can add these directories to 'Places'(favorites/bookmarks), so you can quickly change to that directory by clicking it in the Places panel.
How to change the root directory for storing website files?
Since '/var/www' is under root user's control, you may want to move this to somewhere else something like to our home folder, say like '/john/servroot/'. Open /etc/apache2/sites-available/default as root in an editor. Change the root directory specified in DocumentRoot directive:DocumentRoot /var/wwwto wherever you would like to (say /home/john/servroot/htdocs/localhost). NOTE: Do not add trailing slash.
And also change the 9th line:<Directory /var/www/>appropriately, with a trailing slash. Restart apache to update changes. @restart-apache1
Apache 2 Security settings (/etc/apache2/conf.d/security):
Here we will see how to modify some minor security related settings. Open the file /etc/apache2/conf.d/security in either the console or file browser with root access.
How to hide server details from showing in error pages? (Server Tokens)
By default apache is set to show the OS details in the error pages like this:
Unless you are working in a development environment, its recommended to hide these type of details. You can set the the directive of server tokens from:ServerTokens OStoServerTokens Prod
Restart apache to update changes. @restart-apache2
But you can still see the server, hostname and the port it is running on. To remove this too, turn off ServerSignature Directive. This setting applies to the entire server.ServerSignature OntoServerSignature Off
Restart Apache. @restart-apache3
Now, for you can see error pages without any server info like this:
Adding another site to Apache:
As of now, we can access our default website by going to LOCALHOST. What if we wanted to add more sites which would be accessible by visiting say, http://mysite.com/? We will now see how to add more sites by adding a name-based virtualhost.
Apache has two directories sites-available and sites-enabled present in /etc/apache2/. To add a site you create a configuration file with the name of your website and save it into sites-available directory. When you wish to enable the site, so that you can actually use it, you use the command:a2ensite <SITE-NAME>Restart Apache @restart-apache4
SITE-NAME is the name of the file in sites-available directory, that is, your websites name.
NOTE: To disable the site use:a2dissite <SITE-NAME>
The command a2ensite actually creates a symlink (similar to shortcuts in windows) to the file present in sites-available and saves that symlink in sites-enabled directory. Conversely a2dissite deletes the symlink. When apache starts it looks for the symlinks present in sites-enabled directory, it loads its configuration and enables that site.
So talking about the configuration file itself, here is a sample configuration to be saved in sites-available directory:<VirtualHost *:80> ServerName example.com ServerAlias http://www.example.com/ DocumentRoot /home/john/servroot/htdocs/example.com ErrorLog /var/log/apache2/example.com-error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. #LogLevel crit # recommended LogLevel debug # only for development CustomLog /var/log/apache2/example.com-access.log combined</VirtualHost>
Change the directory paths appropriately and save it with a name like example.com. 'ServerName' is the name of your website. This directive tells apache that whenever the apache server receives a url of example.com, it should catch that url and connect it to the appropriate web file in DocumentRoot. ServerAlias is an alias to the default name. In this case, we have created a www version of alias. So, going to example.com/file.html or http://forums.xisto.com/no_longer_exists/ will show the same file. Note that, an alias does not redirect to the original domain name. We need to use other method like rewrite method saved in a htaccess file. We will cover this in the later chapter where we will see how to connect our domain name with the PC.
There are two log (ErrorLog and LogLevel) directives mentioned in the configuration file above. The name of the log file can be anything, apache will create it automatically in '/var/log/apache2' directory. These log files are very useful for debugging purposes and also to keep an eye on website activity.
Do not forget to enable your site after saving the configration file. @enable-site1
Hey! Wait. But how do I open the site I just created, since I have not yet bought example.com? So if we enter example.com in browser we do not see our own website. Yes you are right, in a later chapter we will see how to link your own domain name to the website on your PC. But for now, to test the website without an actual domain, we will fool our PC and make our test domain example.com redirect to our newly added virtualhost site above.
Open /etc/hosts and add:127.0.0.1 example.comNow, only on your PC, when you visit example.com, it is redirected to your website on your PC. It is thus very easy to work with real URL's while the website is actually still in development. Especially useful where you rely on real website url's in your/others scripts.
In this chapter we installed apache, learnt about various confiugrations. In the next chapter we will install PHP (Hypertext Preprocessor), so that we can run dynamic useful scripts instead of hosting just plain vanila html files. We will also see more apache related configurations as we move on.I'm not sure doing this is a great idea.
Open /etc/hosts and add:127.0.0.1[tab][/tab]example.comNow,
only on your PC, when you visit example.com, it is redirected to your
website on your PC. It is thus very easy to work with real URL's while
the website is actually still in development. Especially useful where
you rely on real website url's in your/others scripts.
I think that your PC needs 127.0.0.1 to be "localhost".
So, you should not replace the "localhost" line with the "example.com" line.
You should better add the alias example.com after the word "localhost"
and your /etc/hosts (or %%windir\system32\drivers\etc\host for Windows users) should rather look like127.0.0.1 localhost example.comRegards
|VIEW DESKTOP VERSION||REGISTER||GET FREE HOSTING|