一、Apache
yum install -y httpd
systemctl enable --now httpd
#设置开机自启
pgrep httpd
ps aux | grep httpd
pgrep httpd | wc -l
#查看进程
http://10.1.1.3
##在浏览器中打开链接访问测试
ps -ao user,group,pid,nice,comm $(pgrep httpd)
#打印出我们的httpd所关联用户是否是主配置文件里面的定义的用户和组
编辑vhost配置文件
mkdir -p /etc/httpd/conf.d/vhost
mkdir -p /var/www/html/{test1,test2}
cd /etc/httpd/conf.d/vhost
touch {test1.conf,test2.conf}
vi test1.conf
<VirtualHost *:9090>
DocumentRoot "/var/www/html/test1"
DirectoryIndex index.html
</VirtualHost>
<Directory "/var/www/html/test1">
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
vi test2.conf
<VirtualHost *:9091>
DocumentRoot "/var/www/html/test2"
DirectoryIndex index.html
</VirtualHost>
<Directory "/var/www/html/test2">
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
编辑主要配置文件
cd ../../conf
echo n | cp -i httpd.conf httpd.conf.bak
vi httpd.conf
#在配置文件中添加以下内容
Listen 9090
Listen 9091
IncludeOptional conf.d/vhost/*.conf
建立静态页面
cd /var/www/html/test1
touch index.html
echo "test1" > index.html
cd /var/www/html/test2
touch index.html
echo "test2" > index.html
访问测试
systemctl restart httpd
http://10.1.1.3:9090
http://10.1.1.3:9091
#在浏览器中打开链接访问测试
二、PHP
安装php
yum install -y php配置用户信息cd /etc/httpd/conf.d/vhost
vi test1.conf
vi test2.conf
#将配置文件中的 index.html 改成 index.php
建立PHP页面
cd /var/www/html/test1
touch index.php
echo "hello world test1 php" > index.php
cd /var/www/html/test2
touch index.php
echo "hello world test2 php" > index.php
访问测试
systemctl restart httpd
http://10.1.1.3:9090
http://10.1.1.3:9091
#在浏览器中打开链接访问测试
三、使用php连接数据库
检查数据库
mysql -uroot -predhat
show databases;
use inventory;
show tables;
select * from product;
exit;
安装php插件
echo y | yum install php-mysql php-odbc
systemctl restart mariadb
systemctl restart httpd
cd /var/www/html/test1
编辑页面
vi index.php
';}
mysqli_query($conn , "set names utf8");
$sql = "select * from product";
#查询 product 表中所有数据
mysqli_select_db( $conn, 'inventory' );
#使用数据库名 inventory
$retval = mysqli_query( $conn, $sql );
if(! $retval ) { die('查询失败: ' . mysqli_error($conn)); } else{echo "数据查询成功\n";}
while($row=mysqli_fetch_row($retval)){
echo "";
echo "{id=$row[0],name=$row[1]}.";
}
mysqli_close($conn);
?>
访问测试
http://10.1.1.3:9090
#在浏览器中打开链接访问测试