设为首页 收藏本站
查看: 1834|回复: 0

[经验分享] Apache网站服务器

[复制链接]

尚未签到

发表于 2015-8-1 13:12:30 | 显示全部楼层 |阅读模式
  源自《Linux 运维之道》丁一明编著 一书的总结
  Apache HTTP Server项目是在Windows和UNIX等平台上可以运行的跨平台开源HTTP服务器的软件,该项目的目标是提供安全、高效、可扩展的HTTP服务。Apache HTTP Server非常重要的是它采用了模块化设计模型,Apache模块分为静态模块和动态模块,静态模块是Apache是最基本的模块,是无法随时添加和卸载的模块,静态模块在编译软件时设定。动态模块是可以随时添加和删除的模块,这样的设计师的企业部署Apache时可以获得最大的灵活性。Apache模块将被编译为动态共享对象(DSO),这些动态共享对象独立于httpd程序,DSO模块可以在编译Apache添加的,也可以在后期随时通过Apache Extension Tool(apxs)工具编译添加模块。可以使用httpd –M命令查看模块加载清单。
  下载并安装
  [iyunv@localhost tempal]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.9.tar.gz
  [iyunv@localhost tempal]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.3.tar.gz
  [iyunv@localhost tempal]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-1.5.1.tar.gz
  安装依赖的软件:
  [iyunv@localhost tempal]# yum -y install gcc autoconf automake make pcre pcre-devel openssl openssl-devel
  [iyunv@localhost tempal]# tar -xzf httpd-2.4.9.tar.gz -C /usr/src/
  [iyunv@localhost tempal]# tar -xzf apr-1.5.1.tar.gz -C /usr/src/
  [iyunv@localhost tempal]# tar -xzf apr-util-1.5.3.tar.gz -C /usr/src/
  [iyunv@localhost tempal]# cd /usr/src/apr-1.5.1/
  [iyunv@localhost apr-1.5.1]# ./configure
  [iyunv@localhost apr-1.5.1]# make && make install
  [iyunv@localhost apr-1.5.1]# cd /usr/src/apr-util-1.5.3/
  [iyunv@localhost apr-util-1.5.3]# ./configure --with-apr=/usr/local/apr/
  [iyunv@localhost apr-util-1.5.3]# make && make install
  [iyunv@localhost httpd-2.4.9]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --with-mpm=worker --with-suexec-bin --with-apr=/usr/local/apr/
  [iyunv@localhost httpd-2.4.9]# make && make install
  可以通过./configure –help查看脚本支持的
  选项
  描述
  --prefix
  指定Apache httpd程序的主安装目录
  --enable-so
  开启模块化功能,支持DSO(动态共享对象)
  --enable-ssl
  支持SSL加密
  --enable-rewrite
  支持地址重写
  --with-mpm
  设置Apache httpd工作模式
  --with-suexec-bin
  支持SUID、SGID
  --withapr
  支持apr程序绝对路径
  启动服务:
  [iyunv@localhost ~]# /usr/local/apache2/bin/apachectl start
  [iyunv@localhost ~]# netstat -tulnp | grep http
  [iyunv@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  [iyunv@localhost ~]# service iptables save
  安装后,Apache会提供名为apachectl启动脚本。脚本在/usr/local/apache2/bin目录下,该脚本用来进行Apache httpd的启动、关闭以及测试功能,具体的参数见下表。
  参数
  描述
  Start
  启动httpd程序
  Stop
  关闭httpd程序
  Restart
  重启httpd程序
  Graceful
  启动httpd,不中断现有的http连接请求
  Graceful-stop
  关闭httpd,不中断现有的http连接
  Status
  查看httpd程序当前状态
  Configtest
  检测httpd主配置语法
  没有修改配置文件的情况下用start启动httpd程序,可能返回错误提示:
  [iyunv@localhost ~]# /usr/local/apache2/bin/apachectl start
  AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
  (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
  (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
  no listening sockets available, shutting down
  AH00015: Unable to open logs
  但是确实服务已经启动了。
DSC0000.png
  配置文件解析:
  配置文件默认在/usr/local/apache2/conf目录下,该目录的主要配置文件是httpd.conf文件。还有一些位于extra目录下的附加配置文件。主配置文件以容器组成,容器使用开始,以结尾,容器的指令一般仅在容器内有效。
  ² SeverRoot指令:设置Apache软件的安装主目录,如果采用源码安装,这默认路径是/usr/local/apache2
  ² Listen指令:设置服务器监听IP和端口号。语法格式为:Listen[IP地址:]端口[协议],其中ip地址与协议可为可选项。可以多吃使用listen指令来开启多个端口。
  ² LoadModule指令:在编译时加上—enable-so将mod_so以静态方式编译到Apache核心模块中,LoadModule指令的作用就是加载模块。
  ² LoadFile指令:通过绝对路径加载modules目录下的模块文件。
  ² serverAdmin指令:提供一个解决问题的邮件地址。
  ² ServeName指令:设置服务器本机的主机名称和端口,对URL地址的重定向很重要。
  ² DocumentRoot指令:设置Web服务对客户端开放可见的文档根目录,也就是客户端访问网站的根路径,默认为/usr/local/apache2/htdocs。
  ² ErrorLog指令:定位服务器错误日志的位置,默认使用相对路径,为ServerRoot目录下的“logs/error_log”文件。
  ² ErrorLogFormat指令:设置错误日志的格式,Apache HTTP Server预先定义了很多格式字串可以直接引用。
  ² CustomLog指令:该指令设置客户端的访问日志文件以及日志格式,格式为“logs/access_log”,语法格式为CustomLog 文件名 格式
  ² LogFormat指令:该指令描述用户日志文件格式,可以直接使用Apache预先设置的格式字串,一般我们会为LogFormat指令设置的日子格式格式创建别名。
  ² Include指令:语法格式为:Options[+|-]选项[[+|0-]选项]。选项可以设置为None,代表不启动任何额外的功能。也可以使用如下常用选项:All,开启除MultiViews之外的所有选项;ExecCGI:允许执行Options指定目录下的所有CGI脚本;FollowSymlinks:允许Options指定目录下的文件链接到目录外的文件或目录。Indexes:如果与URL对应的Options目录下找不到DirectoryIndex指定的首页文档,则Apache将会把当前目录的所有文件索引出来。
  ² Order指令:控制默认访问状态以及Allow和Deny的次序,如果使用Order deny,allow,则先检查拒绝,然后在允许。如果有冲突,允许优先,默认规则为允许。如果使用Order allow,deny,则先检查允许,然后在拒绝。如果有冲突,拒绝优先,默认规则为拒绝。例如:
  案例一,所有都拒绝:
  Order deny,allow
  Deny from all
  案例二,除192.168.118.254以外都拒绝:
  Order allow,deny
  Allow from 192.168.118.254
  案例三,拒绝所有:
  Order allow,deny
  Allow from 192.168.118.254
  Deny from All
  案例四:除192.168.118.254以外都拒绝:
  Order deny,allow
  Deny from all
  Allow from 192.168.118.254
  ² ifDefine容器:仅在启动Apache时测试条件为真才会被处理,测试条件需要在启动Apache时通过httpd –D定义。语法格式为:指令
  
  LoadModule mem_cache_module modules/mod_mem_cache.so
  
  说明只有在启动Apache时,如果使用了httpd –D Memcache,这Apache将加载mod_mem_cache模块,如果没有则Apache将不加载这些模块。
  ² ifModule容器
  使用IfModule容器可以封装仅在条件满足时才会处理的指令。语法格式:指令
  例子:
  
  User daemon
  Group daemaon
  
  仅在加载了unixd_module模块后,User daemon和Group daemon才会被Apache处理。
  ² Directory容器:仅应用特定的文件系统目录、子目录以及目录下的内容,语法格式:指令。路径可以使用~匹配正则表达式。
  例子:
  
  Options Indexs FollowSymLinks
  
  Options Indexs FollowSymLinks仅对/usr/local/apache2/htdocs目录有效。
  ² :类似于Directory,但可以直接使用正则表达式匹配。无需要使用~符号才可以匹配。
  ² File容器:类似于Directory,但Files容器内的指令应用于特定的文件,语法格式为:指令
  ² FilesMatch容器:类似于File,但可以直接使用正则表达式匹配。无需要使用~符号才可以匹配。
  ² Location容器:仅对特定的URL有效,语法格式:指令
  ² LocationMathch容器和VirtualHost容器
  虚拟主机应用案例:
  将主配置文件的语句开启:
  [iyunv@localhost ~]# gedit /usr/local/apache2/conf/httpd.conf
  去掉注释
  Include conf/extra/httpd-vhosts.conf
  更改httpd-vhosts.conf文件
  
  ServerAdmin webmaster@dummy-host.example.com
  DocumentRoot "/usr/local/apache2/htdocs/example"
  ServerName www.example.com
  ServerAlias web.example.com
  ErrorLog "logs/example.com-error_log"
  CustomLog "logs/example.com-access_log" common
  
  
  ServerAdmin webmaster@dummy-host.example.com
  DocumentRoot "/usr/local/apache2/htdocs/test"
  ServerName www.test.com
  ServerAlias web.test.com
  ErrorLog "logs/test.com-error_log"
  CustomLog "logs/test.com-access_log" common
  
  开启:
  [iyunv@localhost ~]# mkdir -p /usr/local/apache2/htdocs/{example,test}
  [iyunv@localhost ~]# echo "example.com">/usr/local/apache2/htdocs/example/index.html
  [iyunv@localhost ~]# echo "test.com">/usr/local/apache2/htdocs/test/index.html[iyunv@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  [iyunv@localhost ~]# service iptables save
  iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
  [iyunv@localhost ~]# /usr/local/apache2/bin/apachectl restart
  网站安全应用案例:
  TLS(Transport Layer Security)是对SSL的扩展和优化,它可以提供数据安全,同时确保数据的完整性。Apache HTTP Server通过mod_ssl模块来实现对SSL/TLS的支持。
  部署证书
  [iyunv@localhost ~]# openssl genrsa -out server.key 2048
  Generating RSA private key, 2048 bit long modulus
  ...................................+++
  ...+++
  e is 65537 (0x10001)
  [iyunv@localhost ~]# openssl req -new -x509 -key server.key -out server.crt
  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) []:Guangzhou
  Locality Name (eg, city) [Default City]:Guangzhou
  Organization Name (eg, company) [Default Company Ltd]:ABC
  Organizational Unit Name (eg, section) []:tech
  Common Name (eg, your name or your server's hostname) []:web1
  Email Address []:abc@gmail.com
  [iyunv@localhost ~]# cp {server.key,server.crt} /usr/local/apache2/conf/
  修改Apache配置文件,主配置文件中需要加载mod_ssl以及mod_socache_shmcb两个模块,同时使用Include指令读取conf/extra目录下的http-ssl配置文件。
  [iyunv@localhost ~]# gedit /usr/local/apache2/conf/httpd.conf
  去掉注释
  LoadModule ssl_module modules/mod_ssl.so
  LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
  [iyunv@localhost ~]# gedit /usr/local/apache2/conf/extra/httpd-ssl.conf
  修改内容:
  Listen 443
  SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
  SSLPassPhraseDialog builtin
  SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
  SSLSessionCacheTimeout 300
  
  DocumentRoot "/usr/local/apache2/htdocs/secret"
  ServerName www.abc.com:443
  ServerAdmin you@example.com
  ErrorLog "/usr/local/apache2/logs/error_log"
  TransferLog "/usr/local/apache2/logs/access_log"
  SSLEngine on
  SSLCertificateFile "/usr/local/apache2/conf/server.crt"
  SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
  
  SSLOptions +StdEnvVars
  
  
  SSLOptions +StdEnvVars
  
  BrowserMatch "MSIE [2-5]" \
  nokeepalive ssl-unclean-shutdown \
  downgrade-1.0 force-response-1.0
  CustomLog "/usr/local/apache2/logs/ssl_request_log" \
  "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
  
  [iyunv@localhost ~]# mkdir -p /usr/local/apache2/htdocs/secret
  [iyunv@localhost ~]# echo "secret">/usr/local/apache2/htdocs/secret/index.html
  [iyunv@localhost ~]# /usr/local/apache2/bin/apachectl restart
  [iyunv@localhost ~]# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
  [iyunv@localhost ~]# service iptables save
  iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
  [iyunv@localhost ~]# gedit /etc/hosts

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93033-1-1.html 上篇帖子: 如何判断Apache服务器的工作模式 下篇帖子: linux下安装apache svn ssl
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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