Linux 配置Apache服务器 上(安装,虚拟目录)
Apache是应用最广泛的Web服务器
Apache的特点:
实现动态共享对象(DSO),允许在运行时动态装载功能模块
采用预生成模式的技术提高响应速度
跨平台:Unix、Linux、Windows等平台
支持HTTP 1.1协议、支持HTTP认证
简单而强有力的基于文件的配置
支持虚拟主机
集成了代理服务器
可定制的服务器日志
支持SSL
用户会话过程的跟踪能力
支持CGI、Perl、SSI、PHP、Java Serverlets
支持第三方软件开发商提供的大量功能模块
Apache服务器的配置:
场景:
配置一台基于Apache的Web服务器,运行两个不同域名的网站。
主机名: | oa.abc.local | crm.abc.local |
IP地址: | 192.168.188.22 | 192.168.188.22 |
您实验环境的IP |
一.实验环境:
操作系统的安装:根据博文附件中的《RHEL 安装-文本最小化安装.docx》进行的最小化安装,其中安装了core及base两个组。
[root@112 ~]# cat /etc/redhat-release // 查看版信息 |
[root@112 ~]# uname -a //查看详细信息 |
服务器的基本配置:
根据你的实验环境进行配置、修改IP
[root@112 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 //配置修改IP |
按“i”进行修改
DEVICE="eth1"
BOOTPROTO=static
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR=192.168.188.22
NETMASK=255.255.255.0
GATEWAY=192.168.188.2
修改后按下“ESC”后输入“:x”回车(保存并退出)
[root@112 ~]# vi /etc/sysconfig/network //修改主机名 |
NETWORKING=yes
HOSTNAME=li.abc.local
[root@112 ~]# service network restart //重启服务 |
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[root@112 ~]# service iptables stop //关闭防火墙 |
[root@112 ~]# chkconfig iptables off //开机自动关闭防火墙 |
[root@112 ~]# vi /etc/sysconfig/selinux //关闭安全策略管理 |
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
重新启动以便生效
[root@112 ~]# reboot //重启 |
二.Apache服务器的安装于基本配置
安装Apache服务组件,安装的方法有两种:一种是RPM安装 ,一种是YUM安装。在这里我选择RPM安装。
[root@li ~]# mkdir /mnt/cdrom //在mnt中创建目录cdrom |
[root@li ~]# mount /dev/cdrom /mnt/cdrom/ //挂载到新建的目录 |
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@li ~]# cd /mnt/cdrom/Packages/ //进入目录 |
[root@li Packages]# ls http* // 查看http开头的文件 |
httpd-2.2.15-26.el6.x86_64.rpm
httpd-devel-2.2.15-26.el6.x86_64.rpm
httpd-tools-2.2.15-26.el6.x86_64.rpm
httpd-devel-2.2.15-26.el6.i686.rpm
httpd-manual-2.2.15-26.el6.noarch.rpm
[root@li Packages]# rpm -Uvh httpd-2.2.15-26.el6.x86_64.rpm //安装RPM包 |
error: Failed dependencies:
/etc/mime.types is needed by httpd-2.2.15-26.el6.x86_64
apr-util-ldap is needed by httpd-2.2.15-26.el6.x86_64
httpd-tools = 2.2.15-26.el6 is needed by httpd-2.2.15-26.el6.x86_64
libapr-1.so.0()(64bit) is needed by httpd-2.2.15-26.el6.x86_64
libaprutil-1.so.0()(64bit) is needed by httpd-2.2.15-26.el6.x86_64
解决包的依赖性
[root@li Packages]#rpm -Uvh httpd-2.2.15-26.el6.x86_64.rpm \ httpd-tools-2.2.15-26.el6.x86_64.rpm \ apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm \ apr-util-1.3.9-3.el6_0.1.x86_64.rpm \ apr-1.3.9-5.el6_2.x86_64.rpm \ mailcap-2.1.31-2.el6.noarch.rpm |
Preparing... ########################################### [100%]
1:apr ########################################### [ 17%]
2:apr-util ########################################### [ 33%]
3:apr-util-ldap ########################################### [ 50%]
4:httpd-tools ########################################### [ 67%]
5:mailcap ########################################### [ 83%]
6:httpd ########################################### [100%]
启动HTTP服务:
[root@li Packages]# vi /etc/httpd/conf/httpd.conf //修改配置文件 |
# redirections will not work. See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
#ServerName www.example.com:80
ServerName localhost:80 ←把默认端口设置成80
#
[root@li Packages]# chkconfig httpd on //开机自动开启 |
[root@li Packages]# service httpd start //启动服务 |
Starting httpd: [ OK ]
[root@li Packages]# ps aux |grep httpd //查看当前HTTPD的进程 |
root 1766 0.0 0.7 173484 3676 ? Ss 16:21 0:00 /usr/sbin/httpd
apache 1768 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1769 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1770 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1771 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1772 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1773 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1774 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
apache 1775 0.0 0.4 173484 2424 ? S 16:21 0:00 /usr/sbin/httpd
root 1824 0.0 0.1 103244 828 pts/0 S+ 18:26 0:00 grep httpd
用浏览器访问,能看到测试页面
配置HTTPD服务器:
安装Apache时,会自动创建一个新的账户
[root@li Packages]# tail /etc/passwd //查看passwd的后十行 |
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user1:x:500:500::/home/user1:/bin/bash
user2:x:501:501::/home/user2:/bin/bash
user3:x:502:502::/home/user3:/bin/bash
user4:x:503:503::/home/user4:/bin/bash
staff:x:504:504::/home/staff:/bin/bash
yunhe:x:505:505::/home/yunhe:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
[root@li Packages]# rpm -qc httpd //查看已个httpd包的配置文件 |
[root@li Packages]# rpm -qc httpd
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd
/var/www/error/HTTP_BAD_GATEWAY.html.var
/var/www/error/HTTP_BAD_REQUEST.html.var
/var/www/error/HTTP_FORBIDDEN.html.var
/var/www/error/HTTP_GONE.html.var
/var/www/error/HTTP_INTERNAL_SERVER_ERROR.html.var
/var/www/error/HTTP_LENGTH_REQUIRED.html.var
/var/www/error/HTTP_METHOD_NOT_ALLOWED.html.var
/var/www/error/HTTP_NOT_FOUND.html.var
/var/www/error/HTTP_NOT_IMPLEMENTED.html.var
/var/www/error/HTTP_PRECONDITION_FAILED.html.var
/var/www/error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
/var/www/error/HTTP_REQUEST_TIME_OUT.html.var
/var/www/error/HTTP_REQUEST_URI_TOO_LARGE.html.var
/var/www/error/HTTP_SERVICE_UNAVAILABLE.html.var
/var/www/error/HTTP_UNAUTHORIZED.html.var
/var/www/error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
/var/www/error/HTTP_VARIANT_ALSO_VARIES.html.var
/var/www/error/contact.html.var
/var/www/error/include/bottom.html
/var/www/error/include/spacer.html
/var/www/error/include/top.html
配置每个用户的Web站点:
配置每个用户的Web站点目的是使在安装了Apache计算机上,拥有用户账号的每个用户都能够架设自己的单独的Web站点
[root@li Packages]# cd /etc/httpd/conf |
[root@li conf]# cp httpd.conf httpd.conf.origin //备份 |
[root@li conf]# vi /etc/httpd/conf/httpd.conf //进入编辑模式对其修改 |
可以通过vi的搜索功能快速找到要编辑的内容。在命令模式下输入
/UserDir (注意大小写)
Apache的默认的配置是通注释符号#来禁用用户个人Web站点的,可以将相应的注释// 符号删除
<IfModule mod_userdir.c>
// 用于安全的考虑,禁用root用户使用自己的个人站点
UserDir disable root
// 配置第个用户的Web站点的目录名
UserDir public_html
</IfModule>
// 设置每个用户的Web站点目录的访问权限,将下面的配置行前#去掉
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
修改后保存退出
[root@li conf]# service httpd restart //重启httpd服务 |
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@li conf]# useradd li //创建一个新的用户li |
[root@li conf]# su - li //切换到新用户 |
[li@li ~]$ mkdir public_html //创建目录pudlic_html |
[li@li ~]$ ll -d /home/li/ //查看文件信息 |
drwx------ 3 li li 4096 Aug 16 19:08 /home/li/
[li@li ~]$ chmod 711 /home/li/ //修改文件的权限 |
[li@li ~]$ ll -d /home/li/ |
drwx--x--x 3 li li 4096 Aug 16 19:08 /home/li
[li@li ~]$ cd ~/public_html/ |
[li@li public_html]$ vi index.html //创建页面编辑文字 |
LI
[li@li public_html]$ cat index.html //查看内容 |
LI
用浏览器进行访问
创建虚拟目录:
虚拟目录是一个目录的显示名或别名,它可以在在本地或远程的计算机。用户可以通过虚拟目录来访问Web页面。
使用虚拟目录确保用户不需要知道Web服务中文件的真实位置,因此能够使Web站点更安全
在Apache中,创建虚拟目录有两种方法
场景:我们安装一套BBS系统,安装目录为/opt/bbs。需要使用虚拟目录将其融入到我们的Web站点中。
模拟安装一套BBS系统
root@li ~]# mkdir /opt/bbs //创建bbs目录 |
[root@li ~]# ls -ld /opt/bbs/ //查看bbs文件的详细信息 |
drwxr-xr-x 2 root root 4096 Aug 16 19:56 /opt/bbs/
[root@li ~]# echo 'li`123'> /opt/bbs/index.html //创建一个测试网页 |
[root@li ~]# ln -s /opt/bbs/ /var/www/html/bbs //创建软连接 |
[root@li ~]# ls -l /var/www/html/bbs //进行查看 |
lrwxrwxrwx 1 root root 9 Aug 16 20:03 /var/www/html/bbs -> /opt/bbs/
打开浏览器输入:HTTP://192.168.188.22/bbs
测试结束后,删除符号链接以下进入一步实验
[root@li ~]# rm /var/www/html/bbs //删除bbs目录 |
rm: remove symbolic link `/var/www/html/bbs'? y
由于文字超过上限,分成上下两篇,下篇主要是虚拟主机的创建.
谢谢您的光临!
转载于:https://blog.51cto.com/87158787/1542610