安装 Apache
安装启动
# 安装
yum -y install httpd
# 启动
systemctl start httpd
systemctl enable httpd
简单粗暴方法:关闭防火墙
# 关闭 firewalld
systemctl stop firewalld
# 取消开机启动 firewalld
systemctl disable firewalld
# 关闭 selinux
setenforce 0
# 修改 selinux 配置文件,取消开机启动 selinux
vi /etc/selinux/config
# SELINUX=enforcing
SELINUX=disabled
安全有效方法:配置防火墙
# firewall 防火墙开放 80 端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 重新加载 firewall 防火墙策略,立即生效。
firewall-cmd --reload
# 设置网站根目录的安全上下文
semanage fcontext -a -t httpd_sys_content_t /data/web
semanage fcontext -a -t httpd_sys_content_t /data/web/*
# 使得目录新的安全上下文生效
restorecon -Rv /data/web
httpd
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
# 重新加载 apache 服务器配置文件,不需要重启服务
systemctl reload httpd
systemctl status httpd
ps -ef | grep httpd
systemctl enable httpd
systemctl disable httpd
systemctl is-enabled httpd
systemctl is-active httpd
firewalld
systemctl start firewalld
systemctl stop filrewalld
# 查看 filewalld 防火墙开放了哪些端口
firewall-cmd --list-ports
# 查看 80 端口是否开放
firewall-cmd --zone=public --query-port=80/tcp
# 开放 80 端口,重启之后生效
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 不开放 80 端口,重启之后生效
firewall-cmd --zone=public --remove-port=80/tcp --permanent
# 不用重启,重新加载防火墙策略,立即生效
firewall-cmd --reload
selinux
firewall 相当于防盗门,对进出服务器的 ip 和端口进行限制
selinux 相当于保险柜,对在服务器运行的程序能够访问的资源进行限制


# 查看 selinux 防火墙状态
sestatus
# 查看 selinux 防火墙配置模式
getenforce
# 设置配置模式为 permissive
setenforce 0
# 设置配置模式为 enforcing
setenforce 1
通过 setenforce
命令修改 selinux 状态,立即生效,但是重启之后就失效。
通过配置文件 /etc/selinux/config
修改 selinux 状态,可以永久生效。但是不会立即生效,需要重启服务器。
查看 /var/www/html
目录的上下文类型
# -d 显示目录本身信息
# -Z 显示目录安全上下文信息
$ ls -Zd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
semanage 命令可能没有安装
# 查看哪个包提供了 semanage 命令
yum provides semanage
# 安装 policycoresulitls-python 包,这个包提供了 semanage 命令
yum -y install policycoresulitls-python
使用 semanage 命令设置目录的安全上下文
semanage fcontext -a -t httpd_sys_content_t /data/web
semanage fcontext -a -t httpd_sys_content_t /data/web/*
使用 restorecon 命令使得对应目录的新设置生效
restorecon -Rv /data/web
配置 Apache


修改网站根目录
修改 /etc/httpd/conf/httpd.conf
配置文件
# DocumentRoot "/var/www/html"
DocumentRoot "/data/web"
# <Directory "/var/www/html">
<Directory "/data/web">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
重新加载 apache 配置文件
systemctl reload httpd
设置虚拟主机
修改 /etc/httpd/conf/httpd.conf
配置文件
<VirtualHost 192.168.100.104>
DocumentRoot /data/web/lishiqing
ServerName www.lishiqing.com
<Directory /data/web/lishiqing>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.100.104>
DocumentRoot /data/web/zhaojinmai
ServerName www.zhaojinmai.com
<Directory /data/web/zhaojinmai>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
重新加载 apache 配置文件
systemctl reload httpd
动态 IP 与静态 IP
/etc/hosts
本地客户端的域名解析文件,优先级高于 dns
# ip 地址解析到主机名,多用于局域网主机
192.168.100.103 zeronic
# ip 地址解析到域名,多用于互联网主机
192.168.100.104 www.lishiqing.com
/etc/sysconfig/network-scripts/ifcfg-enp0s3
服务器上的网络配置文件
#BOOTPROTO=dhcp
BOOTPROTO=static
IPADDR=192.168.100.104
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=192.168.1.1
修改配置之后,重启网络服务
systemctl restart network
除了使用配置文件修改 IP 地址,还可以使用 NetworkManage 工具修改 IP 地址
nmtui
修改配置之后,重启网络服务
systemctl restart network
网友评论