找回密码
 注册

QQ登录

只需一步,快速开始

查看: 667|回复: 1

Apache 2 Web Server on Ubuntu 12.04 LTS (Precise Pangolin)

[复制链接]
发表于 2013-5-19 09:55:42 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2013-5-20 02:11 编辑

This tutorial explains how to install and configure the Apache webserver on Ubuntu 12.04 (Precise Pangolin). All configuration will be done through theterminal; make sure you are logged in as root via SSH.If you have not followed the getting startedguide, it is recommended that you do so prior to beginning this guide.Also note that if you're looking to install a full LAMP stack,you may want to consider using our LAMP guides.
Contents

Set the Hostname
Before you begin installing and configuring the components described in this guide, please makesure you've followed our instructions for setting your hostname.Issue the following commands to make sure it is set properly:
hostname
hostname -f

The first command should show your short hostname, and the second should show yourfully qualified domain name (FQDN).

Install Apache 2
Make sure your package repositories and installed programs are up todate by issuing the following commands:
  1. <pre class="literal-block">apt-get update
  2. apt-get upgrade --show-upgraded</pre>
复制代码
Enter the following command to install the Apache 2 web server, itsdocumentation and a collection of utilities.
  1. apt-get install apache2 apache2-doc apache2-utils
复制代码
Install Support for Scripting
The following commands are optional, and should be run if you want tohave support within Apache for server-side scripting in PHP, Ruby,Python, or Perl.
To install Ruby support, issue the following command:
  1. apt-get install libapache2-mod-ruby
复制代码
To install Perl support, issue the following command:
  1. apt-get install libapache2-mod-perl2
复制代码
To install Python support, issue the following command:
  1. apt-get install libapache2-mod-python
复制代码
If you need support for MySQL in Python, you will also need to install Python MySQL support:
  1. apt-get install python-mysqldb
复制代码
Your PHP application may require additional dependencies included in Ubuntu.To check for available PHP dependencies run "apt-cache search php", whichwill provide a list of package names and descriptions.
To install, issue thefollowing command:
  1. apt-get install libapache2-mod-php5 php5 php-pear php5-xcache
复制代码
Issue the following command to install the php5-suhosin package, which provides additional security to your PHP installation:
  1. apt-get install php5-suhosin
复制代码
If you're also hoping to run PHP with MySQL, then also install MySQLsupport:
  1. apt-get install php5-mysql
复制代码
Configure Name-based Virtual Hosts
There are different ways to set up Virtual Hosts, however we recommend themethod below. By default, Apache listens on all IP addresses available to it.
First, issue the following command to disable the default Apachevirtual host.
  1. a2dissite default
复制代码
Each additional virtual host needs its own file in the/etc/apache2/sites-available/ directory. In this example, you'llcreate files for two name-based virtually hosted sites,"example.net" and "example.org".
First create bucknell.net (/etc/apache2/sites-available/example.net) sothat it resembles the following.
File:/etc/apache2/sites-available/example.net
  1. <VirtualHost *:80>
  2.      ServerAdmin webmaster@example.net
  3.      ServerName example.net
  4.      ServerAlias www.example.net
  5.      DocumentRoot /var/www/example.net/public_html/
  6.      ErrorLog /var/www/example.net/logs/error.log
  7.      CustomLog /var/www/example.net/logs/access.log combined
  8. </VirtualHost>
复制代码
If you would like to enable Perl, add the following lines to theVirtualHost entry above.
File excerpt:/etc/apache2/sites-available/example.net
  1. Options ExecCGI
  2. AddHandler cgi-script .pl
复制代码
Next, create ducklington.org (/etc/apache2/sites-available/example.org) sothat it resembles this:
File:/etc/apache2/sites-available/example.org
  1. <VirtualHost *:80>
  2.      ServerAdmin admin@example.org
  3.      ServerName</span> example.org
  4.      ServerAlias</span> www.example.org
  5.      DocumentRoot /var/www/example.org/public_html/
  6.      ErrorLog /var/www/example.org/logs/error.log
  7.      CustomLog /var/www/example.org/logs/access.log combined
  8. </VirtualHost>
复制代码
You'll note that some basic options are specified for both sites,including where the files for the site will reside (under/var/www/). You can add (or remove) additional configurationoptions, such as the Perl support, on a site-by-site basis to thesefiles as your needs dictate.
Create required directories for these sites by issuing the following commands:
  1. mkdir -p /var/www/example.net/public_html
  2. mkdir /var/www/example.net/logs

  3. mkdir -p /var/www/example.org/public_html
  4. mkdir /var/www/example.org/logs</pre>
复制代码
Enable the sites by issuing these commands:
  1. a2ensite example.net
  2. a2ensite example.org
复制代码
Finally, restart the Apache server to initialize all the changes,with this command:
  1. service apache2 restart
复制代码
When you create or edit any virtual host file, you'll need to reload theconfig, which you can do without restarting the server with the following command:
  1. sudo service apache2 reload
复制代码
Congratulations! You now have Apache installed on your Ubuntu LinuxVPS and have configured the server for virtual hosting.



 楼主| 发表于 2013-5-19 09:56:02 | 显示全部楼层
本帖最后由 demo 于 2013-5-20 02:18 编辑

Install Apache Modules
One of Apache's prime strengths is its extreme customiz ability and flexibility. With its support for a large number of modules,there are few web serving tasks that Apache cannot fulfill.
By default,modules and their configuration files are installed in the/etc/apache2/mods-available/ directory. Generating a list of this directory will tell you what modules are installed. To enable a module listed in this directory, use the following command:
  1. a2enmod [module-name]
复制代码
Note that in the /etc/apache2/mods-available/ directory, files have a.load and .conf extension. Module names do not include the extension.
To disable a module that is currently enabled, use the inverse command:
  1. a2dismod [module-name]
复制代码
To get a list of available Apache modules modules in the Ubuntu repository use the following command:
  1. apt-cache search libapache2*
复制代码
To install one of these modules use the command:
  1. apt-get install [module-name]
复制代码
Modules should be enabled and ready to use following installation, thoughyou may have to apply additional configuration options to have accessto the modules' functionality. Consult theApache module documentationfor more information regarding the configuration of specific modules.

Configuration Options
One of the strengths, and obstacles, of Apache is the immense amount offlexibility offered in its configuration files. In the defaultinstallation of Apache 2 on Ubuntu, the main configuration islocated in the /etc/apache2/apache2.conf files, but Apacheconfiguration directives are loaded from files in a number of differentlocations, in a specific order. Configuration files are read in thefollowing order, with items specified later taking precedence overearlier and potentially conflicting options:
/etc/apache2/apache2.conf
Files with .load or .conf extensions in/etc/apache2/mods-enabled/ directory.
/etc/apache2/httpd.conf (Blank by default.)
/etc/apache2/ports.conf
Files within the /etc/apache2/conf.d/ directory.
Files within the /etc/apache2/sites-enabled/ directory.
Per-directory .htaccess files in the directory.

Remember, later files take precedence over earlier-cited files. Withina directory of included configuration files, files will be read inorder based on the sort of their file names.
Apache will follow symbolic links to read configuration files, soyou can create links in these directories and locations to files thatare actually located elsewhere in your file system.
Best practices for most installations dictate that we don't recommendmodifying the following default configuration files:/etc/apache2/httpd.conf, files in /etc/apache2/mods-enabled/,and in most cases /etc/apache2/apache2.conf. This is to avoidunnecessary confusion and unintended conflicts in the future.
Generally, as specified in our LAMP guidesand elsewhere, files that configure virtual hosts should be located in the/etc/apache2/sites-available/ directory (and symbolically linked tosites-enabled/ with the a2ensite tool. This allows for a clearand specific per-site configuration.
In practice, the vast majority of configuration options will probablybe located in site-specific virtual host configuration files.If you need to set a system-wide configuration option or aren't using virtualhosting, the best practice is to specify options in files createdbeneath the conf.d/ directory.

Multi-Processing Module
The default Apache configuration uses a tool called MPM-worker, this multi-processingmodule can handle a large number of requests quickly by utilizing multiple threads perworker process. However, this use of multiple threads is not compatible with some PHPextensions. When PHP is installed MPM-worker is replaced with MPM-prefork, which allowsApache to handle requests without threading for greater compatibility with some software.Furthermore, using MPM-prefork allows Apache to isolate requests in separate processesso that if one request fails for some reason, other requests will be unaffected.
For more complex setups, however, we recommend that you consider usingan alternate MPM module called "ITK." mpm-itk is quite similar toprefork, but it goes one step further and runs the processes for each siteunder a distinct user account. This is particularly useful insituations where you're hosting a number of distinct sites that youneed to isolate sites on the basis of user privileges.
Begin by installing the mpm-itk module:
  1. apt-get install apache2-mpm-itk
复制代码
Now, in the <VirtualHost > entries for your sites (thesite-specific files in /etc/apache2/sites-available/) add the followingsub-block:
File excerpt:Apache Virtual Host Configuration

  1. <IfModule mpm_itk_module>
  2.    AssignUserId webeditor webgroup
  3. </IfModule>
复制代码
In this example, webeditor is the name of the user of the specific site inquestion, and webgroup is the name of the particular group that"owns" the web server related files and processes. Remember that youmust create the user accounts and groups using the useraddcommand.

More Information
You may wish to consult the following resources for additionalinformation on this topic. While these are provided in the hope that they willbe useful, please note that we cannot vouch for the accuracy or timeliness ofexternally hosted materials.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-10 15:20 , Processed in 0.020868 second(s), 17 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表