apache的安装配置启用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache的安装配置启用相关的知识,希望对你有一定的参考价值。

参考技术A

Apache 的安装无外乎两种方式:源代码安装和二进制包安装。这两种安装类型各有特色,二进制包安装不需要编译,而源代码安装则需要先配置编译再安装,二进制包安装在一个固定的位置下,选择固定的模块,而源代码安装则可以让你选择安装路径,选择你想要的模块。本文主要介绍二进制DEB包安装方式(此方法只适用于Debian GNU/Linux 及其衍生版)。
系统:GNU/Linux Debian/etch
Apache当前版本: 2.4.2
1、安装:
使用以下命令安装:
tony@tonybox:~$sudo aptitude update aptitude install apache2 apache2-utils
其中apache2-utils提供了我们在配置维护过程中非常有用的一些工具
安装完成后,可以使用下面的命令启动Apache 服务:
tony@tonybox:~$ sudo /etc/init.d/apache2 start
停止Apache服务则是:
tony@tonybox:~$ sudo /etc/init.d/apache2 stop
也可以直接用 kill 命令强制杀死apache2进程
tony@tonybox:~$ sudo killall apache2
如有需要, 可以通过rcconf来控制是否在系统启动时加载Apache 服务
启动完成后打开浏览器, 使用URL http://localhost/ 来访问已经启动的Apache服务器, 服务器将会跳转到 http://localhost/apache2-default/, 向浏览器返回一个Apache安装成功的页面。
注: 这取决于/etc/apache2/sites-available/default 配置文件中, 是否取消了
RedirectMatch ^/$ /apache2-default/
行的注释
2、 配置文件说明
在Debian下, 安装完成后, 软件包为我们提供的配置文件位于/etc/apache2目录下:
tony@tonybox:/etc/apache2$ ls -l
total 72
-rw-r--r-- 1 root root 12482 2006-01-16 18:15 apache2.conf
-rw-r--r-- 1 root root 748 2006-01-16 18:05 envvars
-rw-r--r-- 1 root root 268 2006-06-30 13:56 httpd.conf
-rw-r--r-- 1 root root 12441 2006-01-16 18:15 magic
-rw-r--r-- 1 root root 10 2006-06-30 13:56 ports.conf
-rw-r--r-- 1 root root 2266 2006-01-16 18:15 README
drwxr-xr-x 2 root root 4096 2006-01-16 18:15ssl
其中
apache2.conf
为apache2服务器的主配置文件, 查看此配置文件, 你会发现以下内容
# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
# Include all the user configurations:
Include /etc/apache2/httpd.conf
# Include ports listing
Include /etc/apache2/ports.conf
# Include generic snippets of statements
Include /etc/apache2/conf.d/[^.#]*
由此可见, apache2 根据配置功能的不同, 对配置文件进行了分割, 这样更利于管理
conf.d
下为配置文件的附加片断,默认情况下, 仅提供了 charset 片断,
tony@tonybox:/etc/apache2/conf.d$ cat charset
AddDefaultCharset UTF-8
如有需要我们可以将默认编码修改为 GB2312, 即文件的内容为: AddDefaultCharset GB2312
httpd.conf
是个空文件
magic
文件中包含的是有关mod_mime_magic模块的数据, 一般不需要修改它。
ports.conf
则为服务器监听IP和端口设置的配置文件,
tony@tonybox:/etc/apache2$ cat ports.conf
Listen 80
mods-available
目录下是一些。conf和。load 文件, 为系统中可以使用的加载各种模块的配置文件, 而mods-enabled目录下则是指向这些配置文件的符号连接, 从配置文件apache2.conf 中可以看出, 系统通过mods-enabled目录来加载模块, 也就是说, 系统仅通过在此目录下创建了符号连接的mods-available 目录下的配置文件来加载模块。同时系统还提供了两个命令 a2enmod 和 a2dismod用于维护这些符号连接。这两个命令由 apache2-common 包提供。命令各式也非常简单: a2enmod [module] 或 a2dismod [module]
sites-available
目录下为配置好的站点的配置文件, sites-enabled 目录下则是指向这些配置文件的符号连接, 系统通过这些符号连接来起用站点 sites-enabled目录下的符号连接附有一个数字前缀, 如000-default, 这个数字用于决定启动顺序, 数字越小, 启动优先级越高。 系统提供了两个命令 a2ensite 和 a2dissite 用于维护这些符号连接。这两个命令由 apache2-common 包提供。
/var/www
默认情况下将要发布的网页文件应该置于/var/www目录下,这一默认值可以同过主配置文件中的DocumentRoot 选项修改。
注意:如果你在是windows下应用Apache服务器,并且已经安装IIS,那么在安装Apache时请注意给Apache换个端口来监听比如8080,否则Apache占用的端口会和IIS冲突,造成Apache服务器不能正常启动。
3.启用SSL让apache更安全
apache加密TCP/IP网络产品的标准是SSL ,对于Internet上普遍使用的超文本传输协议(HTTP)而言,其加密后的协议称为 HTTPS,缺省采用443端口。HTTPS数据是加密以后传输的,因此能有效保护在网络上传输的个人隐私信息。
对apache配置支持SSL需要经过如下的操作:
第一步:下载所需的软件并解开到 /usr/local/src 目录
Apache 1.3.24
Mod_ssl 2.8.8-1.3.24
Openssl-0.9.6c
每个 mod_ssl 的版本和特定的 Apache 版本有关,因此要下载相对应的 mod_ssl 版本。
第二步:编译和安装
安装 OpenSSL 到 /usr/local/ssl: # pwd
/usr/local/src/openssl-0.9.6c
# ./config
# make
# make test
# make install
安装 mod_ssl,编译进 Apache 的源码树: # pwd
/usr/local/src/mod_ssl-2.8.8-1.3.24
# ./configure --with-apache=/usr/local/src/apache_1.3.24 \\
--with-ssl=/usr/local/ssl
以 DSO 方式编译 Apache: # pwd
/usr/local/src/apache_1.3.24
# ./configure --prefix=/usr/local/apache --enable-rule=SHARED_CORE \\
--enable-module=ssl --enable-shared=ssl
# make
创建 SSL 证书,证书需要从商业的认证权威机构或者从内部的 CA 得到。
执行下面的步骤生成证书: # pwd
/usr/local/src/apache_1.3.24
# make certificate TYPE=custom
生成证书时会提示两遍下面的信息:<> 内为示范数据。
第一遍: Country Name (2-letters)
State or Province Name
Locality Name
Organization Name
Organizational Unit Name
Common Name
Email Address
Certificate Validity <365>
第一遍会产生一个用于测试的 CA。Common Name 可以为任意文本。第二遍 Country Name (2-letters)
State or Province Name
Locality Name
Organization Name
Organizational Unit Name
Common Name
Email Address
Certificate Validity <365>
第二遍产生的是实际可用的证书,能被商业机构或者内部 CA 认证, Common Name 为 Web 服务器的主机名。
安装并运行 Apache # pwd
/usr/local/src/apache_1.3.24
# make install
启动 Apache ,并测试 # pwd
/usr/local/apache/bin
# ./apachectl stop
# ./apachectl startssl
在浏览器上检查你的站点正常与否即可,至此即可让apache支持安全的SSL。
在Apache 1.4以后的版本,我们还可以用以下命令完成服务的完美重启:
#./apachectl graceful

参考技术B 你,n

你,n
你,n
你,n
你,n

[原创]Apache配置https

Apache配置https

配置https的注意事宜

yum安装httpd服务情况:
  需要安装mod_ssl模块,其安装后会在/etc/httpd/conf.d/目录下生成ssl.conf文件,需要其配置文件中加载相应的模块文件及配置
编译安装httpd服务的情况:
要编译的时候可以使用 --enable-ssl选项启用ssl功能,只需要在httpd的配置文件中, 加载相应的ssl模块及配置相关ssl参数

SSL会议的简化过程

  • 客户端发送可供选择的加密方式,并向服务器请求证书
  • 服务器端发送证书以及选定的加密方式给客户端
  • 客户端取得证书并先进行证书验证
    • 验证证书的合法性,用CA的公钥解密证书上数字签名
    • 验证证书的内容合法性:完整性验证
    • 检查证书的有效期限
    • 检查证书是否被吊销
    • 证书中拥有者的名字,与访问的目标主机是否一致
  • 客户端生成临时会议密码(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,完成密钥交换
  • 服务器用此密码加密用户请求的资源,响应给客户端

https配置实例

配置CA及证书
yum install httpd -y

CA
cd /etc/pki/CA/
[root@Centos7 CA]# ls
certs  crl  newcerts  private
[root@Centos7 CA]# cd private/
[root@Centos7 private]# (umask 077; openssl genrsa -out cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
.........................................+++
........................+++
e is 65537 (0x10001)
[root@Centos7 private]#

[root@Centos7 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:CN
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Mageedu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:ca.mageedu.com
Email Address []:admin@mageedu.com
[root@Centos7 CA]# ls
cacert.pem  certs  crl  newcerts  private

[root@Centos7 CA]# touch index.txt serial
[root@Centos7 CA]# echo 01 > serial
[root@Centos7 CA]# ls
cacert.pem  certs  crl  index.txt  newcerts  private  serial

http server
root@Centos7 ~]# cd /etc/httpd/
[root@Centos7 httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run
[root@Centos7 httpd]# mkdir ssl

root@Centos7 ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
...+++
.................................................................................+++
e is 65537 (0x10001)

[root@Centos7 ssl]# openssl req -new -key httpd.key -out httpd.csr -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:CN
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Mageedu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www.zhenping.com
Email Address []:admin@zhenping.com

[root@Centos7 ssl]# scp httpd.csr root@172.16.36.71:/tmp

CA Server
[root@Centos7 CA]# openssl ca -in /tmp/httpd.csr -out certs/httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jan 17 23:42:58 2016 GMT
            Not After : Jan 16 23:42:58 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = CN
            organizationName          = Mageedu
            organizationalUnitName    = Ops
            commonName                = www.zhenping.com
            emailAddress              = admin@zhenping.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                61:7F:29:8A:68:A6:70:C2:F2:0E:49:15:D7:DD:4D:02:BF:EF:92:6A
            X509v3 Authority Key Identifier:
                keyid:18:F8:A6:71:FB:05:F3:0C:D3:56:9C:90:78:F1:4D:B5:0E:EC:51:5F

Certificate is to be certified until Jan 16 23:42:58 2017 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

[root@Centos7 CA]# scp certs/httpd.crt root@172.16.36.70:/etc/httpd/ssl
The authenticity of host '172.16.36.70 (172.16.36.70)' can't be established.
ECDSA key fingerprint is f7:6e:2f:38:57:8e:8c:0b:12:74:cc:af:44:82:88:17.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.36.70' (ECDSA) to the list of known hosts.
root@172.16.36.70's password:
httpd.crt                                                                                                                                       100% 4606     4.5KB/s   00:00

httpd server

vim /etc/httpd/conf.d/ssl.conf
DocumentRoot "/var/www/html"
ServerName www.zhenping.com
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
http的配置
root@Centos6-ser1 ~]# cat /etc/httpd/conf.d/vhost.conf
    LoadModule status_module modules/mod_status.so
    <Location /server-status>
        SetHandler server-status
        order deny,allow
        deny from all
        allow from 172.16.249.148
    </Location>

    namevirtualhost 172.16.36.60:80
    <VirtualHost 172.16.36.60:80>
        servername wwww.a.com
        Documentroot "/www/a.com/htdocs/"
        alias /download "/www/a.com/htdocs/file"
        Errorlog /www/log/a.com/error.log
        LogLevel warn
        Customlog /www/log/a.com/access.log combined
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
        <Directory "/www/a.com/htdocs/file/">
            Options Indexes
            AllowOverride None
            AuthType Basic
            AuthName "Please enter you username and password...."
            AuthUserFile "/etc/httpd/conf/.htpass"
            Require user zhenping
        </Directory>
    </virtualhost>

    <virtualhost 172.16.36.60:80>
        servername www.b.com
        documentroot "/www/b.com/htdocs"
        Errorlog /www/log/b.com/error.log
        LogLevel warn
        Customlog /www/log/b.com/access.log combined
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
        <Directory "/www/b.com/htdocs">
            Options None
            AllowOverride None
            AuthType Basic
            AuthName "Please Enter your username and password."
            AuthUserFile "/etc/httpd/conf/.htpass"
            AuthGroupFile "/etc/httpd/conf/.htpass_group"
            Require group mygrp
        </Directory>
    </virtualhost>

    <virtualhost 172.16.36.60:80>
        servername www.c.com
        documentroot "/www/c.com/htdocs"
        ErrorLog /www/log/c.com/error.log
        LogLevel warn
        CustomLog /www/log/c.com/access.log combined
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
        <Directory "/www/c.com/htdocs">
            Options None
            AllowOverride None
            order deny,allow
            deny from all
            allow from 172.16.249.148
        </Directory>
    </virtualhost>

号外号外:


以上是今天为大家带来的内容,如果有任何问题,大家也可以添加以下QQ群参与问题的讨论。
  • Ansible中文权威群:372011984(已满)
  • AWK&SED企业实战: 260039357
  • docker企业架构实践:491533668
  • Jumpserver交流群 :399218702
  • Ansible中文权威-2号群:486022616

关于我们:


以上是关于apache的安装配置启用的主要内容,如果未能解决你的问题,请参考以下文章

如何安装配置Apache中的mod

Apache网页小工具

web安全实践

[原创]Apache配置https

安装配置cacti

Apache网页优化和安全优化