美文网首页我爱编程
CentOS安装启动Apache

CentOS安装启动Apache

作者: PPMac | 来源:发表于2017-09-25 19:10 被阅读153次

本次安装采用 yum 安装的方式.

yum安装

sudo yum install httpd -y

此时,默认安装的配置文件为 : /etc/httpd/conf/httpd.conf

配置防火墙

sudo vim /etc/sysconfig/iptables

添加以下代码

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

保存后,80端口被开放.

重启防火墙才能生效.

sudo service iptables restart

启动服务

Apache配置完成后,则可启动服务

sudo service httpd start

查看启动状态

sudo service httpd status

若发现状态信息包含以下内容

httpd: Could not reliably determine the server's fully qualified domain name

则是Apache服务器的配置文件中未添加ServerName参数.

修改hostname

查看本机hostname

$ hostname #运行结果假设为localhost.localdomain

也可自行修改

$ sudo vim /etc/sysconfig/network

进入后,添加一行:

HOSTNAME=localhost.localdomain

修改/etc/hosts文件:

127.0.0.1    localhost.localdomain  #修改为localhost.localdomain

修改hostname后,需要重启才能生效
重启:

shutdown -r now

编辑Apache配置文件

sudo vim /etc/httpd/conf/httpd.conf

在其中加入一行:ServerName localhost.localdomain:80

重启服务器:

sudo service httpd restart

查看本机ip:

ifconfig

192.168.xx.xx为ip地址

产看运行结果

在浏览器输入 : http://192.168.xx.xx

你将看到Apache的测试页面.

自定义站点目录

测试页面上告诉了我们默认的站点目录:

Are you the Administrator?

You should add your website content to the directory /var/www/html/.
To prevent this page from ever being used, follow the instructions in the file /etc/httpd/conf.d/welcome.conf.

修改目录:

sudo vim /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"
以及<Directory "/var/www/html">
的目录修改为自己想要的目录即可.
修改后需要重启服务器:

sudo systemctl restart httpd.service

相关文章

网友评论

    本文标题:CentOS安装启动Apache

    本文链接:https://www.haomeiwen.com/subject/bnmdextx.html