本文共 9255 字,大约阅读时间需要 30 分钟。
http背景
超文本传输协议(HypeText Transfer Protocol)互联网上应用最广泛的网络协议。1960年美国人Ted Nelson构思的一种通过计算机处理文本信息的方法,称为'超文本',这成为HTTP超文本传输协议标准架构的发展根基。
技术架构
HTTP是一个客户端和服务器端请求和应答的标准(TCP),通常,由HTTP客户端发起一个请求,建立一个到服务器指定端口(默认是80端口)的TCP连接。HTTP服务器则在那个端口监听客户端发送过来的请求。一旦收到请求,服务器(向客户端)发回一个状态,消息可能是请求的文件、错误消息,或其他信息。
软件包
http 服务端口: 80/tcp(http)https 服务端口: 443/tcp(https,http+ssl)
配置文件
'使用rpm包安装的httpd程序环境'/etc/httpd/conf/httpd.conf '主配置文件'/etc/httpd/conf.d/*.conf '辅助配置文件'/etc/httpd/conf.d/welcome.conf '默认测试页面' /var/log/httpd/access.log '访问日志' /var/log/httpd/error_log '错误日志'/var/www/html/ '站点文档目录'/usr/lib64/httpd/modules/ '模块文件路径'/etc/httpd/conf.modules.d/*.conf '模块配置文件'mpm:以DSO机制提供,配置文件为/etc/httpd/conf.modules.d/00-mpm.conf
编译安装httpd生成的配置文件
/usr/local/apache/logs '日志文件'/etc/httpd24/httpd.conf '主配置文件'/usr/local/apache/htdocs '站点文档目录''启动服务可以使用绝对路径与相对路径'绝对路径:[root@yaoxiaorong apache]# /usr/local/apache/bin/httpd httpd (pid 56281) already running相对路径:[root@yaoxiaorong apache]# cd /usr/local/apache/bin/[root@yaoxiaorong bin]# ./apachectl httpd (pid 56281) already running
如果需要解析动态php程序,则需要安装php
'安装PHP'[root@yaoxiaorong ~]# yum install -y php'如果作为Apache的模块运行,并生成对应配置文件'[root@yaoxiaorong ~]# ll /etc/httpd/modules/libphp5.so -rwxr-xr-x. 1 root root 4588376 Apr 13 03:04 /etc/httpd/modules/libphp5.so[root@yaoxiaorong ~]# ll /etc/httpd/conf.d/php.conf -rw-r--r--. 1 root root 691 Apr 13 03:04 /etc/httpd/conf.d/php.conf重启Apache加载PHP[root@yaoxiaorong ~]# systemctl restart httpd'编写php状态页面'[root@yaoxiaorong ~]# cat >> /var/www/html/info.php <phpinfo();> ?>> EOF[root@yaoxiaorong html]# systemctl restart httpd[root@yaoxiaorong html]# ss -antlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
测试访问php状态页面,输入自己的IP,后面在加info.php,在操作这些步骤之前,一定要关防火墙和selinux。
如图:
编译安装httpd-2.4
'准备环境,将防火墙和selinux'[root@yaoxiaorong ~]# setenforce 0[root@yaoxiaorong ~]# systemctl stop firewalld
'安装开发环境'[root@yaoxiaorong ~]# yum groups mark install "Development Tools"Loaded plugins: fastestmirrorRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastThere is no installed groups file.Maybe run: yum groups mark convert (see man yum)Determining fastest mirrors * base: mirrors.nwsuaf.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.comMarked install: Development Tools[root@yaoxiaorong ~]# yum grouplistLoaded plugins: fastestmirrorRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastLoading mirror speeds from cached hostfile * base: mirrors.nwsuaf.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.comAvailable Environment Groups: Minimal Install Compute Node Infrastructure Server File and Print Server Basic Web Server Virtualization Host Server with GUI GNOME Desktop KDE Plasma Workspaces Development and Creative WorkstationInstalled Groups: Development Tools '开发工具'Available Groups: Compatibility Libraries Console Internet Tools Graphical Administration Tools Legacy UNIX Compatibility Scientific Support Security Tools Smart Card Support System Administration Tools System ManagementDone
'创建apache组和用户apache'[root@yaoxiaorong ~]# groupadd -r apache[root@yaoxiaorong ~]# useradd -M -s /sbin/nologin -g apache apache[root@yaoxiaorong ~]# id apacheuid=1000(apache) gid=996(apache) groups=996(apache)
'安装相关的软件包'[root@yaoxiaorong ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
·下载并安装apr-1.4和apr-util-1.4+
'下载wget'[root@yaoxiaorong src]# yum -y install wget[root@yaoxiaorong ~]# cd /usr/src/[root@yaoxiaorong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2[root@yaoxiaorong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
'解压下载安装apr-1.4和apr-util-1.4+的压缩包[root@yaoxiaorong src]# tar xf apr-1.6.3.tar.bz2 [root@yaoxiaorong src]# tar xf apr-util-1.6.1.tar.bz2 [root@yaoxiaorong src]# lsapr-1.6.3 apr-util-1.6.1 debugapr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 kernels
'进入apr-1.6.3将修改configure配置文件'[root@yaoxiaorong apr-1.6.3]# cd apr-1.6.3/[root@yaoxiaorong apr-1.6.3]# vim configure cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 #$RM "$cfgfile" //将此行加入注释,或者删除此行
'指定编译参数,使用./configure --help 命令查看可以使用的选项,一般常用的有 --prefix=PREFIX这个选项的意思是定义软件包安装到哪里'[root@yaoxiaorong apr-1.6.3]# ./configure --prefix=/usr/local/apr'编译并安装'[root@yaoxiaorong apr-1.6.3]# make -j 2 && make install[root@yaoxiaorong apr-1.6.3]# cd /usr/src/apr-util-1.6.1/[root@yaoxiaorong apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@yaoxiaorong apr-util-1.6.1]# make -j 2 && make install
编译安装httpd
[root@yaoxiaorong apr-util-1.6.1]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2[root@yaoxiaorong apr-util-1.6.1]# mv httpd-2.4.34.tar.bz2 /root[root@yaoxiaorong apr-util-1.6.1]# cd[root@yaoxiaorong ~]# lsanaconda-ks.cfg httpd-2.4.34.tar.bz2[root@yaoxiaorong ~]# tar xf httpd-2.4.34.tar.bz2 anaconda-ks.cfg httpd-2.4.34 httpd-2.4.34.tar.bz2[root@yaoxiaorong ~]# cd httpd-2.4.34/[root@yaoxiaorong httpd-2.4.34]# ./configure --prefix=/usr/local/apache \ '配置文件在的绝对路径'> --sysconfdir=/etc/httpd24 \ > --enable-so \> --enable-ssl \> --enable-cgi \> --enable-rewrite \> --with-zlib \> --with-pcre \> --with-apr=/usr/local/apr \> --with-apr-util=/usr/local/apr-util/ \> --enable-modules=most \> --enable-mpms-shared=all \> --with-mpm=prefork[root@yaoxiaorong httpd-2.4.34]# make && make install
将虚拟主机改成相同IP相同端口不同域名
[root@yaoxiaorong httpd-2.4.34]# vim /etc/httpd24/httpd.conf# ServerName gives the name and port that the server uses to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.## If your host doesn't have a registered DNS name, enter its IP address here.#ServerName www.example.com:80 //取消此行前面的#号
在/etc/httpd24/httpd.conf最后一行添加以下内容:#virtual host 1ServerName www.yaoxiaorong.com DocumentRoot "/usr/local/apache/htdocs/yaoxiaorong" ErrorLog "logs/yaoxiaorong/error_log" CustomLog "logs/yaoxiaorong/access_log" combined /# virtual host 2 (前面的/不需要添加)Require all granted Require not ip 192.168.1 ServerName blog.yxr.com DocumentRoot "/usr/local/apache/htdocs/yxr" ErrorLog "logs/yxr/error_log" CustomLog "logs/yxr/access_log" combined Require all granted
'启动httpd服务,并查看80端口是否启动起来'[root@yaoxiaorong httpd-2.4.34]# /usr/local/apache/bin/httpd[root@yaoxiaorong httpd-2.4.34]# ss -antlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
创建网页目录
[root@yaoxiaorong apache]# cd /usr/local/apache/htdocs/[root@yaoxiaorong htdocs]# lsindex.html[root@yaoxiaorong htdocs]# mkdir yaoxiaorong[root@yaoxiaorong htdocs]# lsindex.html yaoxiaorong[root@yaoxiaorong htdocs]# mkdir yxr
创建网页
[root@yaoxiaorong htdocs]# pwd/usr/local/apache/htdocs[root@yaoxiaorong htdocs]# echo 'hello yaoxiaorong' > yaoxiaorong/index.html[root@yaoxiaorong htdocs]# echo 'hello yxr' > yxr/index.html
修改网页目录修改属主属组
[root@yaoxiaorong htdocs]# chown -R apache.apache yaoxiaorong[root@yaoxiaorong htdocs]# chown -R apache.apache yxr[root@yaoxiaorong htdocs]# lltotal 4-rw-r--r--. 1 root root 45 Jun 12 2007 index.htmldrwxr-xr-x. 2 apache apache 24 Aug 9 17:44 yaoxiaorongdrwxr-xr-x. 2 apache apache 24 Aug 9 17:44 yxr
创建相应网页的日志目录
[root@yaoxiaorong apache]# cd logs[root@yaoxiaorong logs]# mkdir yaoxiaorong[root@yaoxiaorong logs]# mkdir yxr[root@yaoxiaorong logs]# chown -R apache.apache /usr/local/apache/logs/
重新启动服务并查看是否有80端口
[root@yaoxiaorong logs]# pkill httpd[root@yaoxiaorong logs]# /usr/local/apache/bin/httpdroot@yaoxiaorong logs]# ss -antlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
在真实主机上验证,修改C:\Windows\System32\drivers\etc配置文件
'最后一行添加以下两行'192.168.228.20 www.yaoxiaorong.com192.168.228.20 blog.yxr.com
在浏览器上输入www.yaoxiaorong.com测试,看能否访问
转载于:https://blog.51cto.com/13835001/2156954