[经验分享] Linux下使用Apache搭建Web网站服务器 [复制链接]
电梯直达
1 #
发表于 2015-9-1 10:21:41 |只看该作者|只看大图
实验目标apache服务器常见概念
apache服务器安装及相关配置文件
实战:
例1:为公司内网搭建一个web服务器 例2:取消apache默认欢迎页: 例3:使用rpm搭建lamp 环境 例4, 使用别名,引用网站根目录以外的路径。 例6:打开软链接功能。 通过软件链接直接引用网站根目录以外的内容 例5,当一个目录下没有默认首页时,访问http://192.168.1.63/phpmyadmin/禁止显示目录列表 例7:通过用户认证的方式,对网站下/usr/local/phpmyadmin/目录进行保护。 实验环境 apache服务端:xuegod-63 IP:192.168.1.63 apache客户端:xuegod-64 IP:192.168.1.64 实验概述 web服务概述:WEB服务器也称为WWW(WORLDWIDE WEB,万维网)服务器,主要功能是提供网上信息浏览服务。
[iyunv@xuegod63~]# yum install httpd -y
[iyunv@xuegod-63 ~]# yum install elinks –y 主配置文件 [iyunv@xuegod-63 ~]# ls/etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf #我们又是怎么知道httpd的注配置文件是在你那里的呢? 查看httpd注配置文件位置 [iyunv@xuegod-63 ~]# rpm -pql/mnt/Packages/httpd-2.2.15-15.el6.x86_64.rpm /etc/httpd /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/welcome.conf /etc/httpd/conf/httpd.conf#这里的就是httpd的主配置文件 [iyunv@xuegod-63~]#rpm -pqiopenssh-server-5.3p1-70.el6.x86_64.rpm 重新启动服务 [iyunv@xuegod-63 ~]# service httpd restart 停止 httpd: [失败] 正在启动 httpd:httpd: Could not reliably determine the server‘s fully qualified domainname, using 192.168.1.63 for ServerName [确定] 或者 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动httpd:httpd: Could not reliably determine the server‘s fully qualifieddomain name, using 192.168.1.63 for ServerName [确定] 开机加载与显示 [iyunv@xuegod-63 ~]# chkconfig httpd on [iyunv@xuegod-63 ~]# chkconfig --list httpd httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 查看httpd的端口 [iyunv@xuegod-63 ~]# netstat -anutp | grephttpd tcp 0 0 :::80 :::* LISTEN 10629/httpd #可以看到httppd的端口是80,在没有重新启动服务之前是不会出现端口号的 也可以反向查看端口 [iyunv@xuegod-63 ~]# lsof -i:80 或者 [iyunv@xuegod-63 ~]# netstat -anutp | grep80 此服务的使用方法 1:通过浏览器访问,直接输入网址就OK了 或 2:通过elinks,elinxs192.168.1.63 实战举例 例1: 搭建一台测试web服务器 案例: 部门内部搭建一台WEB服务器,采用的IP地址和端口为192.168.1.63:80,首页采用index.html 文件。管理员E-mail地址为 mv@xuegod.cn,网页的编码类型采用UTF-8,所有网站资源都存放在/var/www/html目录下,并将Apache的配置文件根目录设置为/etc/httpd目录。 脚本步骤 [iyunv@xuegod-63~]# vim /etc/httpd/conf/httpd.conf 修改配置文件相关参数: [iyunv@xuegod63 chroot]#vim /etc/httpd/conf/httpd.conf #以下参数需要我们掌握 #ServerRoot"/etc/httpd"#apache配置文件的根目录,”ServerRoot-服务主目录“ #这里是pid文件 #Timeout 60#超时时间 #Listen 80 监听的端口,这里监听的是httpd的端口。 #ServerAdminroot@localhost#设置管理员,e-mail地址 #ServerName192.168.1.63:80 #服务器主机名. # DirectoryIndexindex.html index.php #指定默认首页 #这是他的字符 #Options Indexes #当一个目录没有默认首页时,允许显示此目录列表 #DocumentRoot"/var/www/html" #网站页面根目录 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 网页上测试 #这样就看到了测试页面,其实这个测试页面会暴露很多重要的信息,因此我们要将其关掉! 例2:取消apache默认欢迎页: [iyunv@xuegod-63~]# vim /etc/httpd/conf.d/welcome.conf #将下面的几行给注释掉 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止httpd: [确定] 正在启动 httpd: [确定] 修改启动页面 [iyunv@xuegod-63 ~]# echo ‘welcome towww.xuegod.cn!‘>/var/www/html/index.html 重新测试 删除页面 [iyunv@xuegod-63 ~]# rm -rf /var/www/html/index.html 重新测试 例2: 使用rpm搭建lamp 环境 LAMP = linux+apache+mysql数据库+php 安装数据包 [iyunv@xuegod-63 ~]# yum install httpdmysql-server php php-mysql -y Httpd==阿帕奇 mysql-server==数据库 php ==php php-mysql==php相关的插件 或者 mysql-server rpm -ihv /mnt/Packages/perl-DBD-MySQL-4.013-3.el6.x86_64.rpm rpm -ihv/mnt/Packages/perl-DBD-MySQL-4.013-3.el6.x86_64.rpm php rpm -ivh/mnt/Packages/php-common-5.3.3-26.el6.x86_64.rpm rpm -ivh/mnt/Packages/php-cli-5.3.3-26.el6.x86_64.rpm rpm -ivh /mnt/Packages/php-5.3.3-26.el6.x86_64.rpm php-mysql m -ivh/mnt/Packages/php-pdo-5.3.3-26.el6.x86_64.rpm rpm -ivh/mnt/Packages/php-mysql-5.3.3-26.el6.x86_64.rpm 启动服务 [iyunv@xuegod-63 ~]# service mysqld start 开机加载并测试 [iyunv@xuegod-63 ~]# chkconfig mysqld on [iyunv@xuegod-63 ~]# mysql #可以看到测试的数据库已经出现,用exit退出,在每一个命令后面都要加“;”。 创建一个测试页 [iyunv@xuegod-63 ~]# vim/var/www/html/index.php#在这下面进行编辑 <?php phpinfo(); ?> 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试php有没有起来 #可以看到php已经起来了 这样lamp就搭建完成了 例3: 修改网根目录及对应参数,设置访问权限 创建目录并拷贝文件 [iyunv@xuegod-63 ~]# mkdir /var/www/html/bbs [iyunv@xuegod-63 ~]# cp -r /boot/grub//var/www/html/bbs/ 修改配置文件 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf 测试 例4: 使用别名功能,引用网站根目录以外的路径。 将/usr/local/phpmyadmin目录通过虚拟目录功能添加到网站根目录。当访问http://192.168.1.63/phpmyadmin/ 时,就可以访问目录/usr/local/phpmyadmin中的内容。 注:apache的别名也叫虚拟目录 语法: Alias URL路径 PATH物理路径 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf Alias /icons/ "/var/www/icons/" /icons/ "#引用的别名 /var/www/icons/"#真实的路径 查看路径 创建目录与拷贝 [iyunv@xuegod-63 ~]# mkdir/usr/local/phpmyadmin [iyunv@xuegod-63 ~]# cp -r /boot/grub//usr/local/phpmyadmin/ 添加 [iyunv@xuegod-63 ~]# echo "test1"> /usr/local/phpmyadmin/a.html 修改配置文件 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 真正的地址 [iyunv@xuegod-63 ~]# ls/usr/local/phpmyadmin a.htmlgrub 例5: 当一个目录下没有默认首页时,访问http://192.168.1.63/phpmyadmin/禁止显示目录列表 #现在我们是可以访问到这里的,我们要实现目标,不能够访问到这里 修改配置文件: [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf #在Indexes前加-减号,此目录下没有默认首页时,禁止显示目录 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 #现在可以看到,而且已经打不开这里了 #在后面追加a.html,仍然能够打开。 例6: 打开软链接功能。 通过软件链接直接引用网站根目录以外的内 创建一个目录并拷贝 [iyunv@xuegod-63 ~]# ls/usr/local/phpmyadmin/ a.html grub [iyunv@xuegod-63 ~]# mkdir /web2 [iyunv@xuegod-63 ~]# cp /etc/passwd /web2 创建一个软连接 [iyunv@xuegod-63 ~]# ln -s /web2//usr/local/phpmyadmin/web2 [iyunv@xuegod-63 ~]# ls/usr/local/phpmyadmin/ a.html grubweb2 #我们可以看到web2以被成功设置成软连接 这个时候在将主配置文件中的禁用给消除,并且重启服务 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 #点击web2软连接 #有我们刚才所拷贝的passwd的文件了例7:
通过用户认证的方式,对网站下/usr/local/phpmyadmin/目录进行保护-设置密码。 设置/usr/local/phpmyadmin/目录,只能通过用户名密码方式访问。
实验前 #在实验前我们是可以进入/usr/local/phpmyadmin/这里的 方法一: 修改配置文件 [iyunv@xuegod-63 ~]# vim /etc/httpd/conf/httpd.conf #在需要使用用户验证的目录的相关Directory段落添加以下红色标记内容: <Directory "/usr/local/phpmyadmin/"> Allow from all authtype basic#这些都是后添加的命令 authname "MySQL adminsoftware......." authuserfile /etc/httpd/conf/passwd.secret #require user tom #require usertom bob require valid-user </Directory> 参数说明: authtype basic# 1)authtype命令:指定认证类型为:basic。 authname "MySQLadmin software......." #请不要动我的奶酪 #AuthName命令:指定认证区域名称。区域名称是在提示要求认证的对话框中显示给用户的。如图:mk:$apr1$7SAJo3uT$2IXlCcw4lpOmvO3Jt2itj0
#第一个生成用户名和密码,需要创建文件,因此需要加-c参数 查看htpasswd的命令参数 [iyunv@xuegod-63 ~]# htpasswd -h Usage: -cCreate a new file. #创建文件 -n Don‘t update file; display results on stdout. -m Force MD5 encryption of the password. #加密码 -d Force CRYPT encryption of the password (default). 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 #现在我们登陆的时候都需要账户和密码才可以登录进去的 。 方法二: 实验前: 将方法一的配置文件给删掉,测试登陆不需要账户密码验证 修改配置文件 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf#在需要使用用户验证的目录的相关Directory段落添加在要保护的目录下创建一个存放进行用户认证信息的文件:
[iyunv@xuegod-63 ~]# vim/usr/local/phpmyadmin/.htaccess #在这下面写入一些代码内容 authtype basic authname "MySQL admin software by.htaccess" authuserfile /etc/httpd/conf/passwd.secret #require user tom require valid-user 创建用户并加密 [iyunv@xuegod-63 ~]# htpasswd -cm/etc/httpd/conf/passwd.secret xiaoyu New password: 123456 Re-type new password:123456 Adding password for user xiaoyu [iyunv@xuegod-63 ~]# htpasswd -m/etc/httpd/conf/passwd.secret laoyuyu New password: 123456 Re-type new password:123456 Adding password for user laoyuyu 查看用户信息 [iyunv@xuegod-63 ~]# cat/etc/httpd/conf/passwd.secret xiaoyu:$apr1$QIoDvMMg$FSjvjuQFONXrRushKtset. laoyuyu:$apr1$uYUPxAGL$m0tBTAH47zYdkWA8Q3TVZ0 #可以看到用户都是加密的 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 例8: 配置apache虚拟主机,实现在一台服务器上运行多个网站 apache虚拟主机实现有三种方法: 1、通过不同的IP地址 2、通过不同的域名 3、通过不同的端口号 三种都需要把虚拟主机功能打开: 修改配置文件---开启虚拟主机 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf 改: #NameVirtualHost *:80 为: NameVirtualHost *:80 方法1:1、通过不同的IP地址,解析不同域名 给服务器增加IP(另一个域名解析出来的那个IP) 添加一个网卡并加ip地址 [iyunv@xuegod-63 ~]# ifconfig eth0:1192.168.1.56 netmask 255.255.255.0 [iyunv@xuegod-63 ~]# mkdir /var/www/html/bbs#创建一个目录 将两个网址加入进去 [iyunv@xuegod-63 ~]# echo"bbs.xuegod.cn" > /var/www/html/bbs/index.html [iyunv@xuegod-63 ~]# echo"www.xuegod.cn" > /var/www/html/index.html 查看 [iyunv@xuegod-63 ~]# ls /var/www/html/ bbs index.html 修改配置文件 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [失败] 正在启动 httpd:[Fri Aug 21 18:28:24 2015] [warn] NameVirtualHost *:80 has no VirtualHosts [确定] 测试 #在方法1.1中是具有缺陷的,它是比较占网络资源的。 方法二:通过不同域名: 在域名管理后台,修改DNS配置,www.xuegod.com与bbs.xuegod.com解析成相同的IP。 修改配置文件—在方法一的配置文件基础之上进行修改 [iyunv@xuegod-63 ~]# vim/etc/httpd/conf/httpd.conf 修改的脚本如下 DocumentRoot /var/www/html/ ServerName www.xuegod.cn ErrorLoglogs/www.xuegod.cn-error_log CustomLoglogs/www.xuegod.cn-access_log common <VirtualHost *:80> ServerAdminwebmaster@dummy-host.example.com DocumentRoot /var/www/html/bbs/ ServerName bbs.xuegod.cn ErrorLoglogs/bbs.xuegod.cn-error_log CustomLoglogs/bbs.xuegod.cn-access_log common Hosts进行解析ip 重启服务 [iyunv@xuegod-63 ~]# /etc/init.d/httpdrestart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试 在windows下 路径如下 以文本的格式打开,在后面添加 注:对于这个文本文件在没有给权限之前是无法保存的,所以要点击属性,中找到安全,给权限! 测试 方法3:基于端口配置虚拟主机: 修改配置文件 [iyunv@xuegod63 conf]# vim/etc/httpd/conf/httpd.conf 改: Listen 80 为: Listen 80 Listen 81 #更改为你要添加的多个端口 重启服务 [iyunv@xuegod-63 ~]#/etc/init.d/httpd restart 停止 httpd: [确定] 正在启动 httpd: [确定] 测试
Linux下使用Apache搭建Web网站服务器
原文地址:https://www.cnblogs.com/zhangkuilong/p/9363933.html