Difference between revisions of "Apache"
(60 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | + | ''Apache'' is the industry standard web server for Linux distributions. It is highly configurable and has a wide range of modules ready for different needs. | |
+ | |||
+ | {{XKCD | ||
+ | |name=permanence | ||
+ | |id=910 | ||
+ | }} | ||
== What is a web server? == | == What is a web server? == | ||
Line 7: | Line 12: | ||
For example, when you visit this wiki, you are sending a request over the internet to some machine that is probably located somewhere in EIT (the user seldom knows exactly where the machine is located). The web server receives your request, and it processes the data you sent. Finally, the server prepares a response (the web page), and sends it back to you. | For example, when you visit this wiki, you are sending a request over the internet to some machine that is probably located somewhere in EIT (the user seldom knows exactly where the machine is located). The web server receives your request, and it processes the data you sent. Finally, the server prepares a response (the web page), and sends it back to you. | ||
− | == | + | == Installing Apache == |
− | + | {{RequiredInstructions|content= | |
− | + | In yum, Apache is distributed under the package name '''httpd''' (for ''hypertext transfer protocol daemon''). Use the package manager associated with your distribution to install Apache. (For more information on how to use yum, see [[Linux#Repository-Based Package Managers|the Linux guide]].) | |
− | + | For example: | |
− | + | <source lang="bash"> | |
+ | $ sudo yum update | ||
+ | $ sudo yum install httpd | ||
+ | </source> | ||
− | + | You'll need to run this command to add Apache as a startup item: | |
− | + | <source lang="bash"> | |
+ | $ sudo /sbin/chkconfig --levels 235 httpd on | ||
+ | </source> | ||
− | + | In RHEL, most Apache configurations are stored in ''/etc/httpd/conf/httpd.conf'' and others are located in the directory ''/etc/httpd/conf.d/''. | |
− | + | At this point, Apache has been installed, but is not yet running. To start the webserver, run this command: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<source lang="bash"> | <source lang="bash"> | ||
− | $ | + | $ sudo /usr/sbin/apachectl start |
− | |||
− | |||
− | |||
</source> | </source> | ||
− | + | }} | |
− | |||
− | |||
− | + | == Apache Directives == | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
You define your settings for Apache using ''directives''. Some of the directives you will likely encounter include: | You define your settings for Apache using ''directives''. Some of the directives you will likely encounter include: | ||
Line 141: | Line 56: | ||
* '''Alias:''' Map a directory URL to some other location on your filesystem. Requires that the ''Alias'' module be loaded. | * '''Alias:''' Map a directory URL to some other location on your filesystem. Requires that the ''Alias'' module be loaded. | ||
− | + | === .htaccess Files === | |
You can also specify some Apache configurations without delving into the master configuration file. To do this, put a file named ''.htaccess'' in any directory that Apache is serving. All directives in it will be interpreted as if they were in a Directory directive in the master configuration file. | You can also specify some Apache configurations without delving into the master configuration file. To do this, put a file named ''.htaccess'' in any directory that Apache is serving. All directives in it will be interpreted as if they were in a Directory directive in the master configuration file. | ||
− | '''VERY IMPORTANT:''' The directory containing ''.htaccess'' must not have the '''AllowOverride None''' directive in the master configuration file in order for '''.htaccess''' to be read | + | '''VERY IMPORTANT:''' The directory containing ''.htaccess'' must not have the '''AllowOverride None''' directive in the master configuration file in order for '''.htaccess''' to be read. |
− | + | === Directory Directive === | |
− | |||
Use the Directory directive to assign other directives to a specific directory. For example: | Use the Directory directive to assign other directives to a specific directory. For example: | ||
Line 170: | Line 84: | ||
Note that this directory is actually the root directory of the web server. | Note that this directory is actually the root directory of the web server. | ||
− | == | + | == The UserDir Module == |
+ | |||
+ | {{RequiredInstructions|content= | ||
The UserDir module lets you access files for any user on the server with a ~, e.g., http://ec2-xxx-xxx-xxx-xx.compute-1.amazonaws.com/~paul/ | The UserDir module lets you access files for any user on the server with a ~, e.g., http://ec2-xxx-xxx-xxx-xx.compute-1.amazonaws.com/~paul/ | ||
Line 176: | Line 92: | ||
This module comes installed, but not activated by default. | This module comes installed, but not activated by default. | ||
− | + | === Enabling UserDir in RHEL === | |
− | If you are using | + | If you are using an RHEL-based distribution for your server (like the Amazon AMI), you need to edit the master Apache configuration file. |
− | + | Open ''/etc/httpd/conf.d/userdir.conf'' in your favorite text editor. For more information on command-line text editors, refer to [[Linux#File Editors|the Linux guide]]. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Find the line that says | |
− | + | <source lang="apache">UserDir disabled</source> | |
− | + | and change it to | |
− | + | <source lang="apache">UserDir disabled root</source> | |
− | + | Additionally, find the line that says | |
− | + | <source lang="apache">#UserDir public_html</source> | |
− | |||
− | |||
− | |||
− | |||
− | |||
and uncomment it; that is, remove the # so that you have | and uncomment it; that is, remove the # so that you have | ||
− | + | ||
+ | <source lang="apache">UserDir public_html</source> | ||
This tells Apache that the directory containing each user's html files is a subdirectory of their home directory called public_html. | This tells Apache that the directory containing each user's html files is a subdirectory of their home directory called public_html. | ||
− | === Apache Logs == | + | '''Note:''' Users need to manually create public_html. It is not created automatically. |
+ | |||
+ | You may also need to change the permissions of your user directory and your public_html directory to allow Apache to read and execute inside them. To do this, run the following commands: | ||
+ | |||
+ | <source lang="bash"> | ||
+ | $ sudo chmod o+x /home/<myusername> | ||
+ | $ sudo chmod o+rx /home/<myusername>/public_html | ||
+ | $ | ||
+ | </source> | ||
+ | |||
+ | Finally, [[#Restarting Apache and Testing|restart Apache]]. | ||
+ | |||
+ | }} | ||
+ | |||
+ | === Remapping UserDir === | ||
+ | |||
+ | If you want to change the name of the UserDir web server root from '''public_html''' to something else like '''.html''', follow these instructions. (You do not need to do this for the purposes of CSE 330; this serves as a reference.) | ||
+ | |||
+ | # Rename your '''public_html''' to '''.html''' | ||
+ | #* The "." in front of the directory name means that the directory is a hidden one. You will not see it with the normal "ls" command. Use "ls -a" to see hidden files as well. | ||
+ | # Edit the UserDir configuration file. | ||
+ | #* In RHEL, edit the userDir configuration file at ''/etc/httpd/conf.d/userdir.conf'' | ||
+ | # Find the line that reads <code>UserDir public_html</code> and change it to <code>UserDir .html/</code> | ||
+ | # [[#Restarting Apache and Testing|Restart Apache]]. | ||
+ | |||
+ | == Apache Logs == | ||
+ | |||
+ | Apache creates two log files: one for all access attempts to your server, and one for errors. The locations of these log files are: | ||
+ | |||
+ | * '''Access Log:''' /var/log/httpd/access_log | ||
+ | * '''Error Log:''' /var/log/httpd/error_log | ||
− | + | You might find it helpful to see access and errors appear "live" in your terminal window as they are created. To do this, you can use the [[Linux#Command Reference|tail -f]] command. Since the log files have strict permissions, you will need to also use sudo. Example: | |
− | + | <source lang="bash"> | |
+ | $ sudo tail -f /var/log/httpd/access_log | ||
+ | </source> | ||
− | + | == Virtual Hosts == | |
'''Virtual Hosts''' are used to run multiple Apache web servers from the same machine. Virtual hosts can listen for connections on different ports and/or different hostnames, serving completely different web sites to each. For example: | '''Virtual Hosts''' are used to run multiple Apache web servers from the same machine. Virtual hosts can listen for connections on different ports and/or different hostnames, serving completely different web sites to each. For example: | ||
Line 232: | Line 170: | ||
</source> | </source> | ||
− | This configuration enables any requests that use a host name of ''cse330.dyndns.org'' will use ''/home/www/cse330'' as the root document directory. Make sure that the DocumentRoot directory exists and is readable by the httpd process. In RHEL, Apache runs as the '''apache''' user. | + | This configuration enables any requests that use a host name of ''cse330.dyndns.org'' will use ''/home/www/cse330'' as the root document directory. Make sure that the DocumentRoot directory exists and is readable by the httpd process. In RHEL, Apache runs as the '''apache''' user. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | It is good practice to put raw server configuration files in ''/etc/httpd/sites-available''. To activate a site, create a symlink from the configuration in ''sites-available'' to a sibling directory called ''sites-enabled''. | |
− | + | == Restarting Apache and Testing == | |
− | + | Whenever you make changes to the Apache configuration files, you will need to restart Apache for the changes to take effect. There are several different ways to restart Apache; they all functionally do (almost) the same thing, so choose your favorite: | |
− | |||
− | |||
<source lang="bash"> | <source lang="bash"> | ||
− | $ | + | $ sudo /usr/sbin/apachectl restart |
− | + | $ sudo apachectl restart # if /usr/sbin is in your PATH (which it is *not* by default in RHEL) | |
− | + | $ sudo /etc/init.d/httpd restart | |
− | $ | + | $ sudo /sbin/service httpd restart |
+ | $ sudo service httpd restart | ||
</source> | </source> | ||
− | + | If you're torn for which version is the "best" to use, the commands involving '''apachectl''' (think "Apache Control") are written by the Apache folks themselves. This has a couple advantages: | |
+ | * They show you errors in the startup process (if there are any) | ||
+ | * They give you the option to perform a "soft" restart; that is, a restart that allows any pending connections to complete. To perform a soft restart, use ''graceful'': <code>sudo apachectl graceful</code> | ||
+ | In my anecdotal experience, the '''apachectl''' commands are also faster than the '''service''' or '''init.d''' commands. | ||
− | + | To make sure everything is working, create a test file in your home directory under public_html, and then point your browser to it: http://ec2-xxx-xx-xx-xxx.compute-1.amazonaws.com/~yourUserName/hello.txt | |
− | |||
− | [[Category:Module | + | [[Category:Module 2]] |
Latest revision as of 05:44, 10 January 2023
Apache is the industry standard web server for Linux distributions. It is highly configurable and has a wide range of modules ready for different needs.
Contents
What is a web server?
A web server is software that listens for connections to your machine, and when a connection is receive, processes the request and responds with the appropriate information. Most web servers listen on port 80, which is reserved for the purpose, and use the HTTP protocol.
For example, when you visit this wiki, you are sending a request over the internet to some machine that is probably located somewhere in EIT (the user seldom knows exactly where the machine is located). The web server receives your request, and it processes the data you sent. Finally, the server prepares a response (the web page), and sends it back to you.
Installing Apache
In yum, Apache is distributed under the package name httpd (for hypertext transfer protocol daemon). Use the package manager associated with your distribution to install Apache. (For more information on how to use yum, see the Linux guide.)
For example:
$ sudo yum update
$ sudo yum install httpd
You'll need to run this command to add Apache as a startup item:
$ sudo /sbin/chkconfig --levels 235 httpd on
In RHEL, most Apache configurations are stored in /etc/httpd/conf/httpd.conf and others are located in the directory /etc/httpd/conf.d/.
At this point, Apache has been installed, but is not yet running. To start the webserver, run this command:
$ sudo /usr/sbin/apachectl start
Apache Directives
You define your settings for Apache using directives. Some of the directives you will likely encounter include:
- DocumentRoot: The path to the directory where the top level web files are going to be stored.
- IfModule: The following block would be included if specified module exists.
- User: Which user apache2 will run as.
- Group: Which group will have group access to default web files.
- AccessFileName: The name of the access file (that specifies user names/passwords and other limitations to files/directories).
- ErrorLog: Where any errors will be written.
- Include: Include some other files.
- LogFormat: How to write a log message.
- ErrorDocument: Files to display for some HTTP errors (500, 404, 402 etc.).
- Alias: Map a directory URL to some other location on your filesystem. Requires that the Alias module be loaded.
.htaccess Files
You can also specify some Apache configurations without delving into the master configuration file. To do this, put a file named .htaccess in any directory that Apache is serving. All directives in it will be interpreted as if they were in a Directory directive in the master configuration file.
VERY IMPORTANT: The directory containing .htaccess must not have the AllowOverride None directive in the master configuration file in order for .htaccess to be read.
Directory Directive
Use the Directory directive to assign other directives to a specific directory. For example:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
RedirectMatch ^/$ /apache2-default/
</Directory>
This sets options for the /var/www directory.
- The Options directive says that:
- If no index page is present in a directory, display a directory index page instead
- Apache will follow symbolic links in the directory
- AllowOverride None says that .htaccess files cannot alter the Apache options in this directory and all sub-directories
- Order allow,deny and Allow from all specifies that anybody is allowed to access this server via HTTP.
Note that this directory is actually the root directory of the web server.
The UserDir Module
The UserDir module lets you access files for any user on the server with a ~, e.g., http://ec2-xxx-xxx-xxx-xx.compute-1.amazonaws.com/~paul/
This module comes installed, but not activated by default.
Enabling UserDir in RHEL
If you are using an RHEL-based distribution for your server (like the Amazon AMI), you need to edit the master Apache configuration file.
Open /etc/httpd/conf.d/userdir.conf in your favorite text editor. For more information on command-line text editors, refer to the Linux guide.
Find the line that says
UserDir disabled
and change it to
UserDir disabled root
Additionally, find the line that says
#UserDir public_html
and uncomment it; that is, remove the # so that you have
UserDir public_html
This tells Apache that the directory containing each user's html files is a subdirectory of their home directory called public_html.
Note: Users need to manually create public_html. It is not created automatically.
You may also need to change the permissions of your user directory and your public_html directory to allow Apache to read and execute inside them. To do this, run the following commands:
$ sudo chmod o+x /home/<myusername>
$ sudo chmod o+rx /home/<myusername>/public_html
$
Finally, restart Apache.
Remapping UserDir
If you want to change the name of the UserDir web server root from public_html to something else like .html, follow these instructions. (You do not need to do this for the purposes of CSE 330; this serves as a reference.)
- Rename your public_html to .html
- The "." in front of the directory name means that the directory is a hidden one. You will not see it with the normal "ls" command. Use "ls -a" to see hidden files as well.
- Edit the UserDir configuration file.
- In RHEL, edit the userDir configuration file at /etc/httpd/conf.d/userdir.conf
- Find the line that reads
UserDir public_html
and change it toUserDir .html/
- Restart Apache.
Apache Logs
Apache creates two log files: one for all access attempts to your server, and one for errors. The locations of these log files are:
- Access Log: /var/log/httpd/access_log
- Error Log: /var/log/httpd/error_log
You might find it helpful to see access and errors appear "live" in your terminal window as they are created. To do this, you can use the tail -f command. Since the log files have strict permissions, you will need to also use sudo. Example:
$ sudo tail -f /var/log/httpd/access_log
Virtual Hosts
Virtual Hosts are used to run multiple Apache web servers from the same machine. Virtual hosts can listen for connections on different ports and/or different hostnames, serving completely different web sites to each. For example:
<VirtualHost cse330.dyndns.org>
ServerAdmin webmaster@localhost
ServerName cse330.dyndns.org
DocumentRoot /home/www/cse330/
ErrorLog /var/log/httpd/error_log
LogLevel warn
CustomLog /var/log/apache2/access_log combined
ServerSignature On
</VirtualHost>
This configuration enables any requests that use a host name of cse330.dyndns.org will use /home/www/cse330 as the root document directory. Make sure that the DocumentRoot directory exists and is readable by the httpd process. In RHEL, Apache runs as the apache user.
It is good practice to put raw server configuration files in /etc/httpd/sites-available. To activate a site, create a symlink from the configuration in sites-available to a sibling directory called sites-enabled.
Restarting Apache and Testing
Whenever you make changes to the Apache configuration files, you will need to restart Apache for the changes to take effect. There are several different ways to restart Apache; they all functionally do (almost) the same thing, so choose your favorite:
$ sudo /usr/sbin/apachectl restart
$ sudo apachectl restart # if /usr/sbin is in your PATH (which it is *not* by default in RHEL)
$ sudo /etc/init.d/httpd restart
$ sudo /sbin/service httpd restart
$ sudo service httpd restart
If you're torn for which version is the "best" to use, the commands involving apachectl (think "Apache Control") are written by the Apache folks themselves. This has a couple advantages:
- They show you errors in the startup process (if there are any)
- They give you the option to perform a "soft" restart; that is, a restart that allows any pending connections to complete. To perform a soft restart, use graceful:
sudo apachectl graceful
In my anecdotal experience, the apachectl commands are also faster than the service or init.d commands.
To make sure everything is working, create a test file in your home directory under public_html, and then point your browser to it: http://ec2-xxx-xx-xx-xxx.compute-1.amazonaws.com/~yourUserName/hello.txt