Centos7.6 http服務(wù)搭建全攻略
1、安裝apache服務(wù)
yum??clean?all
yum?install?-y?httpd
2、啟動服務(wù)并驗(yàn)證
systemctl??start?httpd
瀏覽器127.0.0.1
3、創(chuàng)建網(wǎng)站根目錄和首頁文件
mkdir?-p?/data/www
chmod?-R?o+rx?/data
echo?"welcome?to?my?website"?>/data/www/index.html
cat??/data/www/index.html
4、修改apache主配置文件
cd?/etc/httpd/conf
mv??httpd.conf?httpd.conf.bak
grep?-v?"#"?httpd.conf.bak>httpd.conf
vim?httpd.conf
ServerName?ns1.myj.edu.cn
DocumentRoot?"/data/www"
<Directory?"/data/www">
AllowOverride?None
Require?all?granted
DirectoryIndex?index.html
</Directory>
5、重啟httpd服務(wù),防火墻放行
systemctl?restart?httpd
firewall-cmd?--permanent?--add-service=http
firewall-cmd?--reload
firewall-cmd?--list-all
setenforce?0
拓展1:創(chuàng)建虛擬目錄
1、創(chuàng)建虛擬目錄和分配權(quán)限
mkdir?-?p?/ito/www
chmod?-R?o+rx?/ito
echo?"this?is?in/ito/www">/ito/www/index.html
2、修改配置文件/etc/httpd/conf/httpd.conf
Alias?/doc?"/ito/www"
<Directory?"/ito/www">
AllowOverride?none
Require?all?granted
</Directory>
3、重啟httpd服務(wù)
systemctl?restart?httpd
拓展2:創(chuàng)建用戶個人主頁
1、創(chuàng)建用戶名和目錄
useradd??myj
useradd?myj2
chmod?-R?o+rx?/home/myj
chmod?-R?o+rx?/home/myj2
mkdir?-p?/home/myj/www
mkdir?-p?/home/myj2/www
echo?"this?is?myjwww"?>?/home/myj/www/index.html
echo?"this?is?myj2?www"?>?/home/myj/www/index.html
2、修改用戶主頁配置文件/etc/httpd/conf.d/userdir.conf
vim?/etc/httpd/conf.d/userdir.conf
<IfModule?mod_userdir.c>
#UserDir?disabled
UserDir?www
</IfModule>
<Directory?"/home/*/public_html">
修改成
<Directory?"/home/*/www">
3、重啟httpd服務(wù)
systemctl?restart?httpd
4、驗(yàn)證http://192.168.128.100/~myj
?????????????http://192.168.128.100/~myj2
拓展3:創(chuàng)建虛擬主機(jī)
1、修改網(wǎng)卡配置文件增加ip地址
cat?/var/named/zone.myj.edu.cn
vim?/etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR0=192.168.128.100
PREFIX0=24
GATEWAY0=192.168.128.254
IPADDR1=192.168.128.120
PREFIX1=24
GATEWAY1=192.168.128.254
DNS1=192.168.128.100
systemctl?restart?network
2、創(chuàng)建虛擬主機(jī)的根目錄和首頁文件,分配權(quán)限
mkdir?-p?/data/www2
chmod?-R?o+rx?/data/www2
echo?"this?is?www.myj.edu.cn"?>?/data/www2/index.html
3、創(chuàng)建vhost.conf配置文件
vim?/etc/httpd/conf.d/vhost.conf
<Virtualhost?192.168.128.100>
????????????DocumentRoot?/data/www
????ServerName?ns1.myj.edu.cn
????????????<Directory?/>
????????????????AllowOverride?none
????????????????Require?all?granted
????????????</Directory>
</Virtualhost>
<Virtualhost?192.168.128.120>
????????????DocumentRoot?/data/www2
????????????ServerName?www.myj.edu.cn
????????????<Directory?/>
????????????????AllowOverride?none
????????????????Require?all?granted
????????????</Directory>
</Virtualhost>
4、重啟httpd服務(wù)
systemctl?restart?httpd