一安装LAMP
[root@VM_0_11_centos ~]# yum -y install httpd mariadb-server mariadb php php-mysql php-gd gd php-cli php-curl php-fpm php-intl php-mcrypt php-mbstring php-xml php-dom 【共15个】
二设置数据库密码,添加数据库test1和test2
MariaDB [(none)]> create database test1;
MariaDB [(none)]> create database test1;
【注:删除数据库:MariaDB [(none)]> drop database test;】
三设置apache对用户访问网站主目录的权限
[root@VM_0_11_centos ~]# cd /etc/php-fpm.d
[root@VM_0_11_centos php-fpm.d]# ls
www.conf
[root@VM_0_11_centos php-fpm.d]# vim www.conf
在文件末尾添加:
listen.owner = apache
listen.group = apache
listen.mode=0666 【6=4读+2写】
注:
[root@VM_0_11_centos httpd]# cd /root
[root@VM_0_11_centos ~]# ll 【ll用于查看文件权限】
total 8
-rwxr-xr-x 1 root root 114 Jul 5 20:43 expectd 【r读值为4,w写值为2,x执行值为1】
-rw-r--r-- 1 root root 719 Jul 5 20:38 lamp.sh
四设置虚拟主机
[root@VM_0_11_centos conf]# cd /etc/httpd
[root@VM_0_11_centos httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@VM_0_11_centos httpd]# cd conf
[root@VM_0_11_centos conf]# ls
httpd.conf magic
[root@VM_0_11_centos conf]# vim httpd.conf
在里面查找IncludeOptional
找到这行:IncludeOptional conf.d/*.conf 【这是在规定httpd服务(因为IP变了)加载不到相应的服务(为什么找不到)就去conf.d中查找以.conf为后缀的文件】
所以我们在conf.d中[root@VM_0_11_centos conf.d]# vim test1.conf
写如下内容:
<VirtualHost *:80> 【VirtualHost为虚拟主机,设置默认端口为80】
ServerName 【这里是Tab】 www.test1.com 【定义域名】
DocumentRoot 【这里是Tab】 /var/www/test1 【设置网站主目录】
</VirtualHost>
<Directory "/var/www/test1"> 【Directory目录,这句话是虚拟机到网站主目录中找php程序】
Require all granted 【把所有的权限放开】
</Directory>
再在conf.d中写一个vim test2.conf
[root@VM_0_11_centos conf.d]# vim test3.conf
:r/test1.conf 【使用末行模式:r绝对路径/文件名,快速读取某个文件中的内容到当前文件】
:%s/1/2/g 【替换】
[root@VM_0_11_centos conf.d]# mkdir /var/www/test1 【创建目录】
[root@VM_0_11_centos conf.d]# mkdir /var/www/test2 【创建目录】
[root@VM_0_11_centos conf.d]# httpd -t 【检查在写httpd配置文件时是否存在语法错误】
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message 【这个要改httpd.conf这个配置文件,主机配置文件一律不该】
Syntax OK
重启httpd
#vim /var/www/test1/index.www
写: this is TEST1111
#vim /var/www/test2/index.www
写: this is TEST2222
加域名解析:打开我的电脑,找到路径
在文件尾部添加:
公网IP 【按Tab】www.test1.com 【注:在访问www.test1.com时,它会逐个遍历 conf.d中的.conf文件中的ServerName ,看是否匹配域名,而不是根据.conf为后缀的文件名进行匹配】
公网IP 【按Tab】www.test2.com
保存
【注:当访问一个网页,首先会在这个文件中找对应IP如果找不到采取访问服务器上的IP】