系统环境

Centos 7.4

Apache

安装

yum install httpd

查看版本

httpd -v  或者 cd /etc/httpd  httpd -v

启动下apache

systemctl start httpd.service

访问下服务器ip就可以看到页面信息了。

PHP 7.2

首先,添加源

yum install epel-release

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装

yum install mod_php72w php72w-opcache  # 有需要其它扩展也是按这种方式安装

其它扩展可以使用下面命令查看

yum search php72w

重启下apache

systemctl restart httpd.service

/var/www/html下新建个index.php文件,输出phpinfo()就可以查看了。

apache 的一些使用

systemctl help httpd.service # 查看帮助

httpd -f /etc/httpd/conf/httpd.conf  #测试配置是否正确

虚拟主机配置

在apache2.4中跟以前版本比变化挺大的,和nginx一样,可以自定义.conf文件

我们建立一个虚拟主机目录

mkdir /etc/httpd/vhost.d

编辑httpd.conf配置文件

vi /etc/httpd/conf/httpd.conf

在尾部加入

Include vhost.d/*.conf

新建虚拟主机文件

vi /etc/httpd/vhost.d/c.bdelay.com.conf  # 为了便于管理文件名命名为应用域名

文件内容

<VirtualHost *:80>
		ServerAdmin admin@amsilence.com  
		DocumentRoot "/var/www/html"  #应用目录
		ServerName c.bdelay.com  #域名
		ErrorLog "/etc/httpd/logs/c_bdelay_com-error_log"  #错误日志记录
		CustomLog "/etc/httpd/logs/c_bdelayyy_com-access_log" common  #访问日志记录
</VirtualHost>

测试文件是否正常

httpd -f /etc/httpd/conf/httpd.conf 

报错

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.31.131.202. Set the 'ServerName' directive globally to suppress this message

修改httpd.conf

#ServerName www.example.com:80
ServerName localhost:80  # 新增这句

重新测试,返回结果

httpd (pid 1413) already running

ok了。重启apache服务器,就可以使用域名访问了。

下次需要新增域名,在vhost.d下新增一个配置文件就可以了。

禁止通过IP访问服务器

新增个虚拟主机配置文件

vi /etc/httpd/vhost.d/deny_ip.conf

内容

<VirtualHost *:80>
    ServerName xx.xx.xx.xx  #你的服务器IP
    <Directory />
            Order Allow,Deny
            Deny from all
    </Directory>
</VirtualHost>

重启apache