PHP安装
1、安装php-fpm
apt install php5-fpm -y
2、配置php-fpm
切换到/etc/php5/fpm/pool.d目录下,并编辑www.conf
cd /etc/php5/fpm/pool.d
gedit www.conf
将listen = /var/run/php5-fpm.sock
改为listen = 127.0.0.1:9000
3、启动php-fpm
/etc/init.d/php5-pfm restart
启动后查看php监听的端口号
netstat -antp|grep php
可知端口号正是我们上一步设置的9000这个端口号
4、至此,php平台安装及配置完成
Nginx配置
1、创建一个server,并让该服务监听8899端口
配置完重启nginx:
/home/ubuntu/nginx/sbin/nginx -s reload
重启后,打开浏览器确认配置生效
2、在8899该服务下添加php的相关配置
然后重新加载nginx配置
location ~ .*\.php?$ {
try_files $uri = 404;
root html;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
3、访问网页
浏览器中打开http://127.0.0.1:8899/index.php
和 http://127.0.0.1:8899/info.php
4、至此,nginx已经和php平台打通
php访问mysql
1、安装三个依赖
apt install php5-mysql php5-pgsql php5-sqlite
将nginx php-fmt mysql全部启动
如果有apache2占用端口,用下面语句关闭
kill -9 $(ps -e | grep apache2 | awk '{print $1}')
2、编写访问代码,以下代码目的在于访问早上创建的
mysql这个database,访问后,打印出里面ruanjian这个table
的表字段
<?php
$host = "127.0.0.1";
$port = "3306";
$username = "root";
$password = "1";
$dbname = "mysql";
$charset = "utf8";
$dsn = "mysql:dbname=$dbname;host=$host";
try{
$pdo = new Pdo($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
echo “接入mysql成功";
echo "hello world";
echo '<hr>';
$state = $pdo->query("select * from ruanjian ");
$total_column = $state->columnCount();
var_dump($total_column);
echo '<hr>';
for ($counter = 0; $counter < $total_column; $counter ++) {
$meta = $state->getColumnMeta($counter);
$column[] = $meta['name'];
}
print_r($column);
set_exception_handler("cus_exception_handler");
} catch(PDOException $e){
echo “接入mysql失败";
die("连接失败: ".$e->getMessage());
}
?>