Centos7 安装Apache

Apache简介

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器软件,可以在大多数电脑操作系统中运行,由于其跨平台和安全性(尽管不断有新的漏洞被发现,但由于其开放源代码的特点,漏洞总能被很快修补。因此总合来说,其安全性还是相当高的。)。被广泛使用,是最流行的Web服务器软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。

Apache安装

[1]查看是否安装有httpd

rpm -qa httpd

[2]安装httpd

yum install httpd* -y

Installed:
  httpd.x86_64 0:2.4.6-80.el7.centos.1                                                
  httpd-devel.x86_64 0:2.4.6-80.el7.centos.1                                          
  httpd-itk.x86_64 0:2.4.7.04-2.el7                                                   
  httpd-manual.noarch 0:2.4.6-80.el7.centos.1                                         
  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1                                          

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7_4.1            apr-devel.x86_64 0:1.4.8-3.el7_4.1         
  apr-util.x86_64 0:1.5.2-6.el7           apr-util-devel.x86_64 0:1.5.2-6.el7        
  cyrus-sasl.x86_64 0:2.1.26-23.el7       cyrus-sasl-devel.x86_64 0:2.1.26-23.el7    
  expat-devel.x86_64 0:2.1.0-10.el7_3     libdb-devel.x86_64 0:5.3.21-24.el7         
  mailcap.noarch 0:2.1.41-2.el7           openldap-devel.x86_64 0:2.4.44-15.el7_5    

Dependency Updated:
  openldap.x86_64 0:2.4.44-15.el7_5                                                   

Complete!

[3]此时可启动httpd服务,查看启动后的状态;

systemctl start httpd.service
systemctl status httpd.service

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2018-09-15 15:50:29 CST; 34s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1427 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1427 /usr/sbin/httpd -DFOREGROUND
           ├─1428 /usr/sbin/httpd -DFOREGROUND
           ├─1429 /usr/sbin/httpd -DFOREGROUND
           ├─1430 /usr/sbin/httpd -DFOREGROUND
           ├─1431 /usr/sbin/httpd -DFOREGROUND
           └─1432 /usr/sbin/httpd -DFOREGROUND

Sep 15 15:50:28 adobe.com systemd[1]: Starting The Apache HTTP Server...
Sep 15 15:50:29 adobe.com httpd[1427]: AH00558: httpd: Could not reliably determi...ge
Sep 15 15:50:29 adobe.com systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

[4]此时打开浏览器,输入虚拟机IP地址:192.168.245.162,即可看到apache的默认界面;

[5]停止httpd服务命令:

systemctl stop httpd.service

Apache的配置

apach 的应用服务

服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
网站数据目录 /var/www/html
访问日志 /var/log/httpd/access_log
错误日志 /var/log/httpd/error_log

在httpd服务程序主配置文件中最为常用的参数如下:

ServerRoot 服务目录
ServerAdmin 管理员邮箱
User 运行服务的用户
Group 运行服务的用户组
ServerName 网站服务器的域名
DocumentRoot 网站数据目录
Listen 监听的IP地址与端口号
DirectoryIndex 默认的索引页页面
ErrorLog 错误日志文件
CustomLog 访问日志文件
Timeout 网页超时时间,默认为300秒.
Include 需要加载的其他文件
新建一个index.html文件测试一下;
# vim /var/www/html/index.html
<h1>测试主页</h1>
此时刷新一下浏览器即可显示“测试主页”;

修改httpd服务目录:

新建一个httpd服务目录文件夹;
# mkdir /data/web/html -pv
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/web’
mkdir: created directory ‘/data/web/html’
~]# vim /etc/httpd/conf/httpd.conf
119行"DocumentRoot "/var/www/html"修改为
"DocumentRoot "/data/web/html"
还有131行"# Further relax access to the default document root:
 <Directory "/var/www/html">"需要修改为
 <Directory "/data/web/html">
 修改完后,使用httpd -t检查语法是否有误;

 ~]# httpd -t
Syntax OK
 新建一个测试主页看一下效果;
 # vim /data/web/html/index.html 
<h1>测试主页更换目录</h1>
<h2>效果展示</h2>
修改完毕需要重启httpd服务,

systemctl restart httpd.service

打开浏览器刷新再看效果;

问题来了:无法显示index.html,而是显示默认apache欢迎界面;

httpd服务程序的功能就是让用户能够访问到网站内容,因此让SELinux对网页访问功能肯定是默认允许的,但刚刚把保存网站数据的默认路径修改为了/home/wwwroot目录,这似乎就产生问题了,也就是说现在httpd提供的网站服务却要去获取普通用户家目录中的数据了,这个行为触犯SELinux服务的监管项目。

解决问题

查看SELinux状态(如果SELinux status参数为enabled即为开启状态)
~]# /usr/sbin/sestatus -v 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

Process contexts:
Current context:                unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Init context:                   system_u:system_r:init_t:s0
/usr/sbin/sshd                  system_u:system_r:sshd_t:s0-s0:c0.c1023

File contexts:
Controlling terminal:           unconfined_u:object_r:user_devpts_t:s0
/etc/passwd                     system_u:object_r:passwd_file_t:s0
/etc/shadow                     system_u:object_r:shadow_t:s0
/bin/bash                       system_u:object_r:shell_exec_t:s0
/bin/login                      system_u:object_r:login_exec_t:s0
/bin/sh                         system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0
/sbin/agetty                    system_u:object_r:getty_exec_t:s0
/sbin/init                      system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0
/usr/sbin/sshd                  system_u:object_r:sshd_exec_t:s0

关闭SELinux:

1、临时关闭(不用重启机器):

设置SELinux 成为permissive模式
setenforce 0 设置SELinux 成为permissive模式
setenforce 1 设置SELinux 成为enforcing模式

setenforce 0

2、修改配置文件需要重启机器:

修改/etc/selinux/config 文件

# vim /etc/selinux/config
第7行代码:SELINUX=enforcing
修改成:SELINUX=disabled

然后重启机器;
reboot

posted @ 2021-08-08 09:26  老头还我葵花宝典  阅读(270)  评论(0编辑  收藏  举报