LNMP-架构环境配置-Nginx与PHP、PHP与MySQL相关集成
1、Fastcgi代理语法
1.1、设置fastcgi服务器的地址,该地址可以指定为域名或IP地址,以及端口
Syntax: fastcgi_pass address; Default: — Context: location, if in location fastcgi_pass localhost:9000;
1.2、设置fastcgi默认的首页文件,需要结合fastcgi_param一起设置
Syntax: fastcgi_index name;
Default: —
Context: http, server, location
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
1.3、通过fastcgi_param设置变量,并将设置的变量传递到后端的fastcgi服务
Syntax: fastcgi_param parameter value [if_not_empty]; Default: — Context: http, server, location fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string;
1.4、fastcgi_index & fastcgi_param 解析图
php解析的真实路径
SCRIPT_FILENAME =/opt/project/page.php
2、Nginx与PHP集成
2.1、配置nginx
cat >/etc/nginx/conf.d/php.conf <<'EOF' server { listen 80; server_name php.cyc.com; root /opt/wordpress; index index.php index.html; location ~ \.php$ { try_files $uri =404; root /opt/project; fastcgi_pass 192.168.10.5:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } EOF # 192.168.10.5:9000 是php-fpm的服务地址
2.2、nginx和php-fpm服务器创建/opt/project/info.php
mkdir /opt/project/ -p cat >/opt/project/info.php<<'EOF' <?php phpinfo(); ?> EOF chown www -R /opt/project
2.3、重新加载nginx服务
systemctl reload nginx
2.4、访问测试
3、PHP与MySQL集成
3.1、创建数据库、登陆用户、密码
3.1.1、创建数据库wordpress
create database wordpress CHARACTER SET utf8;
3.1.2、创建用户和密码并且授权
grant all on wordpress.* to 'wordpress'@'%' identified by 'wordpress';
3.2、PHP测试连接MySQL
3.2.1、php代码【nginx和php服务器上都要上传】
cat > /opt/project/mysqli.php<<'EOF' <?php $servername = "192.168.10.6"; $username = "wordpress"; $password = "wordpress"; $conn = mysqli_connect($servername, $username, $password); if (!$conn) { die("连接失败:" . mysqli_connect_error()); } echo "php connection mysql successfuly"; ?> EOF chown www /opt/project/mysqli.php
3.2.2、访问测试
]# php /opt/project/mysqli.php
php connection mysql successfuly