未加星标

Apache on Ubuntu Linux For Beginners

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

The Apache HTTP server is a mighty beast that powers the majority of websites. It has evolved into a complex server that slices, dices, dances, and sings. It powers vast hosting centers, and it is also splendid for running small personal sites.

The trick with Apache is knowing which configurations you need as it has plenty to choose from. We'll start with setting up a single site, and then in part 2 set up SSL, which is vitally important, and we'll learn a bit about the .htaccess file. Yes, .htaccess , that wonderful file that allows us to make per-directory configurations, but which uses such a contorted syntax it reduces the most stoic server admin to tears and whimpers. The good news is you never need to use .htaccess if you have access to your Apache configuration files. But when you don't, for example on shared hosting, you need .htaccess .

In this series we'll tackle Debian/Ubuntu/Mint and Fedora/CentOS/Red Hat separately, because the various linux families are all special flowers that feel they must organize the Apache configuration files in their own unique ways.

On Debian/etc. install Apache with this command:

$ sudo apt-get install apache2

This installs Apache, utilities, configuration files, and other items you can see with apt-cache show apache2 . Debian/Ubuntu start Apache automatically after installation. Point your web browser to http://localhost to verify that it's running; you should see the default index page (Figure 1).

default-page.png
Apache on Ubuntu Linux For Beginners

Figure 1: Apache2 Ubuntu default page.

Used with permission

When you see this you're ready to put it to work.

Where is Everything

Configuring Apache appears complex at first, but when you study the configuration files you see a nice modular scheme that makes it easy to manage your configurations. The configuration files are in /etc/apache2 . Spend some time studying your configuration file structure and reading the default configuration files. You should have these files and directories:

apache2.conf conf-available conf-enabled envvars magic mods-available mods-enabled ports.conf sites-available sites-enabled

The main server configuration file is /etc/apache2/apache2.conf . You won't spend much time editing this file as it ships with a complete configuration, and it calls other configuration files for the bits you need to customize. Look in here to see the location of server configuration files, your HTTP user and group, included files, log file locations, and other global server configurations.

conf-available contains additional server configurations. Keep any additional global configurations in this directory, rather than adding them to apache2.conf .

mods-available contains configurations for all installed modules.

sites-available contains configurations for your virtual hosts. Even if you're running only a single site you will set it up in a virtual host configuration.

None of the configurations in these directories are active until they are symlinked to their respective *-enabled directories. You must use Apache commands to set up new sites correctly, which we will get to in the next section.

envvars contains Apache's environment variables.

magic has file MIME types, so that Apache can quickly determine the file type from its first few bytes.

ports configures the TCP ports that Apache listens to.

Single Site

The installation comes with a default site that is configured in sites-available/000.default.conf. You could modify and use this, but it's better to leave it alone because it's a useful testing tool. Let's be like real nerds and make our new site from scratch. First create its document root (which in this example is test.com ), enter the new directory, then create and test your sample index page:

$ sudo mkdir -p /var/www/test.com $ cd /var/www/test.com $ sudo nano index.html

Feel free to copy this for your new test page. It must be named index.html and go in your document root:

<head> <title>Test.com index page</title> </head> <h1>Hello, welcome to test.com! It works!</h1> <h2>That is all I have to say. If you don't see this then it doesn't work.</h2> </body> </html>

Test your new index page by opening it in a web browser (Figure 2), which in this example is file:///var/www/test.com/index.html .

test-page.png
Apache on Ubuntu Linux For Beginners

Figure 2: Test page.

Used with permission

So far so good! Now create the virtual hosts file so that Apache can serve it up. All virtual hosts files must have the .conf extension, because this is how they are defined in apache2.conf .

$ cd /etc/apache2/sites-available/ $ sudo nano test.com.conf

Copy this example configuration, substituting your own email address:

<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/test.com ServerName test.com ServerAlias www.test.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Apache calls this name-based virtual hosting , which allows you to serve multiple virtual hosts on a single IP address. IP-based hosting requires that each virtual host have its own IP address. Now you must activate the new configuration with a2ensite :

$ sudo a2ensite test.com.conf Enabling site test.com. To activate the new configuration, you need to run: service apache2 reload

Restart Apache, then point your web browser to http://test.com (Figure 3).

test-com-live.png
Apache on Ubuntu Linux For Beginners

Figure 3: Test page live.

Used with permission

Hurrah, it works! At this point you can go super-nerd and set up DNS for your test domain. Or you could do it the easy way with entries in your /etc/hosts file:

127.0.1.2 test.com 127.0.1.3 www.test.com

These are localhost addresses, so your sites are not accessible outside of your PC. For more LAN testing you could use the /etc/hosts file on multiple computers in the same subnet, and enter the LAN IP address of your Apache server instead of a localhost address. Another option is to use Dnsmasq; creating a LAN server is easy with Dnsmasq, so check out Dnsmasq For Easy LAN Name Services to learn how.

If you want a publicly-accessible web server over the big bad Internets, then you have a whole lot more work to do with registering domain names, setting up proper DNS, and building a good firewall. Do please study this intensively, and be careful!

Setting up More Sites

Want to set up more sites? Just repeat these steps, creating different document roots and domains.

The fine Apache documentation is exhaustively thorough, and it makes more sense when you have a live server running, and have some idea of how things work. Come back for part 2 to learn about the vitally-important SSL configuration, and how to bend .htaccess to your will.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: UbuntuLinuxDebianApache HTTP
分页:12
转载请注明
本文标题:Apache on Ubuntu Linux For Beginners
本站链接:http://www.codesec.net/view/481432.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(34)