懒人专用: https://lnmp.org
自己手动折腾实现,至于为什么呢?
学习,积累,折腾自己。
本人的电脑是win7系统,
1、安装一个vmware,里面装一个centos 7
2、安装nginx
3、安装PHP
4、安装MySQL
5、结合起来
nginx配置虚拟主机,支持php-fpm, php连接数据库。
mkdir /usr/local/nginx/conf/vhost
vim /usr/local/nginx/conf/nginx.conf
在http配置中加入虚拟主机配置:
http{
#其他的忽略没写,主要是加入下面这句
include vhost/*.conf;
}
vim /usr/local/nginx/conf/vhost/test.com.conf
server{
listen 80;
server_name test.com;
root /data/www/test.com;
index index.html index.htm index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
mkdir -p /data/www/test.com
cd /data/www/test.com
vim index.php
<?php
header('Content-Type:text/html;charset=utf-8');
$db = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8','root','123456');
$sql="SELECT * FROM test";
$sth = $db->prepare($sql);
$sth->execute();
$list = $sth->fetchAll();
echo "<pre>";
print_r($list);
连接数据库,创建表和测试数据
CREATE TABLE `test`.`test` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT ,
`title` varchar(255) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `test`.`test` (`id`, `title`) VALUES ('1', '测试');
INSERT INTO `test`.`test` (`id`, `title`) VALUES ('2', '测试21');
重新加载配置:
nginx -s reload
6、在win7系统上的host上,配置一个域名解析到虚拟机的centos上,
文件路径: C:\Windows\System32\drivers\etc\hosts
192.168.50.21 test.com
7、打开浏览器访问 test.com
完毕!