CentOS 安装httpd服务(Apache)

1、从ISO镜像安装,Apache 服务的软件包名称为 httpd

#检查源配置
[root@localhost media]# cat /etc/yum.repos.d/CentOS-Media.repo # CentOS-Media.repo # # This repo can be used with mounted DVD media, verify the mount point for # CentOS-7. You can use this repo and yum to install items directly off the # DVD ISO that we release. # # To use this repo, put in your DVD and use it with the other repos too: # yum --enablerepo=c7-media [command] # # or for ONLY the media repo, do this: # # yum --disablerepo=\* --enablerepo=c7-media [command] [c7-media] name=CentOS-$releasever - Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#挂载cdrom,挂载点和repo配置相同
mount /dev/cdrom /media/cdrom
#安装httpd
[root@localhost media]# yum install httpd

#启动服务
[root@localhost media]# systemctl start httpd
[root@localhost media]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2019-01-21 16:11:38 CST; 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 6702 (httpd)
。。。 。。。

#设置自动启动
systemctl enable httpd
#永久打开80端口
[root@localhost media]# firewall-cmd --zone=public --add-port=80/tcp --permanent

至此,从浏览器可以访问缺省页面。

2、配置

缺省配置目录:

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

2.1、配置文件主要参数/etc/httpd/conf/httpd.conf

ServerRoot  服务目录
ServerAdmin  管理员邮箱
User  运行服务的用户
Group  运行服务的用户组
ServerName  网站服务器的域名
DocumentRoot  网站数据目录
Directory 网站数据目录的权限
Listen  监听的 IP 地址与端口号
DirectoryIndex  默认的索引页页面
ErrorLog  错误日志文件
CustomLog  访问日志文件
Timeout  网页超时时间,默认为 300

2.2、替换网站缺省的页面

#静态网站一般以index.html为启动页面,在网络目录里放入一个index.html页面替换apache的缺省页面
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# echo "welcome visit my homepage..." > index.html
[root@localhost html]# ls
index.html

生产环境网站的数据文件整体放入/var/www/html即可

2.3、重新设定网站的数据目录

[root@localhost html]# mkdir /home/wwwroot
[root@localhost html]# cd /home/wwwroot/
[root@localhost wwwroot]# echo "welcome my new page..." > index.html
[root@localhost wwwroot]# ls
index.html

#修改DocumentRoot和<Directory ""> [root@localhost conf]#
vi /etc/httpd/conf/httpd.conf # # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # # DocumentRoot "/var/www/html" DocumentRoot "/home/wwwroot" # # Relax access to content within /var/www. # <Directory "/home/wwwroot"> AllowOverride None # Allow open access: Require all granted </Directory>


。。。。。。
#重启httpd服务
[root@localhost conf]# systemctl restart httpd


重新访问:

页面已经变化。

如果出现“Forbidden,You don't have permission to access /index.html on this server.”,则可能是SELinux的权限导致的。

这要重新配置SELinux权限,或者直接关闭SELinux权限。

#权限disabled
[root@localhost conf]# cat /etc/selinux/config

# 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 - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

猜你喜欢

转载自www.cnblogs.com/asker009/p/10301262.html
今日推荐