一、Apache服务安装管理
1、安装httpd服务
yum install httpd -y ##apache软件
yum install httpd-manual ##apache的手册
systemctl start httpd
systemctl enable httpd
2、打开httpd服务,并设设置防火墙允许
[root@localhost ~]# systemctl start httpd 打开服务
[root@localhost ~]# firewall-cmd --permanent --add-service=http 火墙允许http
success
[root@localhost ~]# firewall-cmd --reload 在不改变状态的条件下重新加载防火墙
success
3、建立默认发布目录 /var/www/html/index.html##
[root@localhost ~]# vim /var/www/html/index.html
网页访问,可以访问到默认发布录的内容
4、http默认发布文件修改
修改http主配置文件 /etc/httpd/conf/httpd.conf
163 <IfModule dir_module>
164 DirectoryIndex http.html index.html 优先级,谁在前先读谁
165 </IfModule>
修改配置文件后需要重启服务,重启后可以访问到新文件的内容
[root@localhost ~]# cat /var/www/html/http.html
<h1>hello world<h1>
5、http默认发布目录修改
[root@localhost ~]# mkdir /westos/www/test -p 建立一个需要修改成http默认发布的目录
修改主配置文件/etc/httpd/conf/http.conf
119 #DocumentRoot "/var/www/html"
120 DocumentRoot "/westos/www/test"
121 <Directory "//westos/www/test">
122 Require all granted
123 </Directory>
修改完成后重启服务,网页访问看到/westos/www/test/index.html中的内容
!!!!!!!!!!!!!!!!注意!!!!!!!!!!!!!!!
当selinux=disabled时,重启http服务后可看到默认发布文件
但是,当selinux=enforing时,必须修改selinx状态或者修改安全上下文才可以看到默认发布文件
[root@localhost conf]# getenforce
Enforcing
[root@localhost conf]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
[root@localhost conf]# restorecon -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /westos/www context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /westos/www/test context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /westos/www/test/index.html context unconfined_u:object_r:mnt_t:s0->system_u:object_r:httpd_sys_content_t:s0
六、httpd端口修改
httpd默认端口为80
如果需要修改在**/etc/httpd/conf/http.conf**
中修改
修改后火墙修改接口配置
访问http是需要加8080
二、Apache虚拟主机设置
1、还原http的默认配置
2、建立虚拟主机的默认发布目录和文件
[root@localhost ~]# mkdir /var/www/virtual/news/html -p
[root@localhost ~]# mkdir /var/www/virtual/music/html -p
[root@localhost ~]# vim /var/www/virtual/news/html/index.html
[root@localhost ~]# vim /var/www/virtual/music/html/index.html
[root@localhost ~]# cat /var/www/virtual/news/html/index.html
<h1>hello news</h1>
[root@localhost ~]# cat /var/www/virtual/music/html/index.html
<h1>hello music</h1>
3、配置默认,music和news的文件
/etc/httpd/conf.d/
[root@localhost conf.d]# vim default.conf
[root@localhost conf.d]# vim news.conf
[root@localhost conf.d]# vim music.conf
[root@localhost conf.d]# cat default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
[root@localhost conf.d]# cat music.conf
<VirtualHost *:80>
ServerName music.westos.com ##指定站点名称
DocumentRoot /var/www/virtual/music/html ##站点默认发布目录
CustomLog logs/default.log combined ##站点日志
</VirtualHost>
<Directory "/var/www/virtual/html/music"> ##认证
Require all granted
</Directory>
[root@localhost conf.d]# cat news.conf
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot /var/www/virtual/news/html
CustomLog logs/default.log combined
</VirtualHost>
<Directory "/var/www/virtual/html/news">
Require all granted
</Directory>
4、测试
在测试主机做本地域名解析
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.250 content.example.com
172.25.254.177 www.westos.com music.westos.com news.westos.com
浏览器访问:
三、访问认证
1、设定其他用户的访问限制
1)、只禁止77主机访问
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
<Directory "/var/www/html">
Order Allow,Deny 优先级,谁在前,先读谁
Allow from all 白名单
Deny from 172.25.254.77 黑名单
</Directory>
修改完成后用77主机测试
2)、只允许77主机访问:
修改完测试:
2、允许指定用户访问,且需要认证
1)、生成认证文件
[root@localhost conf.d]# htpasswd -cm /etc/httpd/htuser xiaoming -cm新建
New password:
Re-type new password:
Adding password for user xiaoming
[root@localhost conf.d]# htpasswd -m /etc/httpd/htuser xiaohua -m 添加
New password:
Re-type new password:
Adding password for user xiaohua
2)、修改默认访问文件
[root@localhost conf.d]# vim default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
<Directory "/var/www/html">
AuthUserFile /etc/httpd/htuser 读取认证文件
AuthName "Please input UserName and Passwd" 访问页面
AuthType basic 基本的认证方式
Require valid-user xiaoming 只允许xiaoming用户访问 不加用户名表示认证文件里的所有用户
</Directory>
测试:
访问需要认证
认证成功后有钥匙图标
四、php插件管理
配置前还原默认设置;
[root@localhost conf.d]# vim default.conf
[root@localhost conf.d]# cat default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
1、php插件服务
1、安装php插件
[root@localhost conf.d]# yum install php
2、在默认发布目录下建立文件 /var/www/html/index.php
[root@localhost conf.d]# vim /var/www/html/index.php
[root@localhost conf.d]# cat /var/www/html/index.php
<?php
phpinfo();
?>
[root@localhost conf.d]# systemctl restart httpd 重启httpd后,会生成php.conf文件
[root@localhost conf.d]# ls
autoindex.conf music.conf php.conf userdir.conf
default.conf news.conf README welcome.conf
访问页面查看
2、php数据库网页管理
切换到默认发布目录 /var/www/html,建议清空
下载php软件包 phpMyAdmin-3.4.0-all-languages.tar.bz2
解压 tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
重命名 mv phpMyAdmin-3.4.0-all-languages mysql
切换到下层目录
生成配置文件 cp config.sample.inc.php config.inc.php
查找授权码 Documentation.txt
修改配置文件 config.inc.php
查看php支持的类型[PHP Modules] php -m
若不支持mysql,查找并安装相关插件 yum install -y php-mysql.x86_64
重启http的服务即可访问 http://172.25.254.177/mysql
如此,可通过图形化界面管理数据库(命令会随操作生成)
五、apahhe的cgi脚本
1、安装插件
[root@localhost conf.d]# yum search manual
Loaded plugins: langpacks
Repository 'rhel_dvd' is missing name in configuration, using id
============================= N/S matched: manual ==============================
groff-base.x86_64 : Parts of the groff formatting system required to display
: manual pages
httpd-manual.noarch : Documentation for the Apache HTTP server
man-pages.noarch : Man (manual) pages from the Linux Documentation Project
man-pages-es-extra.noarch : Extra manual pages - Spanish versions
man-pages-it.noarch : Italian man (manual) pages from the Linux Documentation
: Project
man-pages-ja.noarch : Japanese man (manual) pages from the Japanese Manual
: Project
man-pages-ko.noarch : Korean(Hangul) Man(manual) Pages from the Korean Manpage
: Project
man-pages-overrides.noarch : Complementary and updated manual pages
man-pages-overrides.x86_64 : Complementary and updated manual pages
Name and summary matches only, use "search all" for everything.
[root@localhost conf.d]# yum install httpd-manual.noarch -y
2、重启服务访问172.25.254.177/manual
3、点击CGI连接,查看相关信息
4、切换到默认发布目录,配置cgi ##[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# mkdir cgi
[root@localhost html]# vim cgi/index.cgi
[root@localhost html]# cat cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@localhost html]# chmod +x cgi/index.cgi
[root@localhost html]# cgi/index.cgi
Content-type: text/html
Sun May 27 11:14:33 EDT 2018
5、重启后访问172.25.254.177/cgi/index.cgi
6、修改http的配置文件
重启服务后再次访问 将执行date命令
注意:此时selinux为关闭状态
若selinux为强制状态需要修改安全上下文
semanage fcontext -a -t httpd_sys_content_t '/var/www/html/cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi
六、https配置
1.Https 访问时自动对数据加密,为了网络数据传输的安全
2.对Https访问,需要安装mod_ssl
`yum install mod_ssl -`y
此时配置目录自动生成 ssl.conf 配置文件
3、此时访问https,认证证书为默认
认证证书如下:
若自己配置,需安装 crypto-utils
[root@localhost ~]# yum install crypto-utils
注意:需要在本机进行域名解析 /etc/hosts
##172.25.254.177 www.westos.com##
##注意:不要修改默认目录,加密强度视情况而定(示例1024),不要选择付费、加密证书
4.修改配置文件 /etc/httpd/conf.d/ssl.conf,读取制作好的认证证书
5.此时访问https,可看到自己配置的认证证书
七、https地址转换
1.建立配置文件 /etc/httpd/conf.d/login.conf
2.域名解析 /etc/hosts
3、设置默认发布信息
4.重启后访问 login.westos.com 即可
5.地址转换
修改配置文件 login.conf
^(/.*)$ 客户在浏览器地址输入的所有字符
https:// 强制客户加密访问
%{HTTP_HOST} 客户访问主机
1 表 示 ( / . ∗ ) 1 表示^(/.*) 1表示(/.∗)的值
##301 临时重写,302表示永久转换