php&nginx&php-fpm&mysql&redis&php simple server code

本文介绍如何配置Nginx以支持PHP处理,并涉及mysql和redis的基本安装步骤。此外,还提供了一个简单的PHP API请求处理示例。

overview

nginx 本身不具备解析 php 的能力,通过,识别 *.php 的请求,将请求交给 php fpm 进行处理

首先启动 php fpm 服务

这里写图片描述

      location ~ \.php$ {
            root           /Users/liyang/programmer/project/vpn-project/vpn_server/OpenVpn/web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

1 php 解析 Post 请求

原始 php 通过 $_POST[{key}] 获取 请求中的参数,获取php 请求中的数据 需要更改 php 的配置文件

获取 php 配置文件的位置

php --ini 

2 mysql 配置

下载mysql 并安装 myql -u -root -p 进行登录即可
mysql 配置文件, my.cnf 通常位于 /etc/ 或 /etc/mysq/ 目录下

3 redis

下载redis 并安装

4 php 请求处理

新建php文件,请求至 api.php 文件,前端将方法作为参数,传于其中,Php 获取请求中的 方法后,执行即可,代码如下

if (empty($function) || empty($_POST) || !function_exists($function())) {
         header('HTTP/1.1 404 Not Found');
         return;
}
// header('Content-type: application/json');
echo $function();

5 后言

使用服务时,通常都伴随着配置文件,在进行环境搭建,以及代码编写过程中,切记万变不离其宗。

6 Issue

nginx sudo 启动文件权限问题,什么时候出现权限问题,怎么解决权限问题(明晰什么时候出现权限问题,解决之道自生)

docker 文件权限问题

git 代码提交后,对文件权限的更改,是否依旧会一并提交。

[root@yfw ~]# cd /www/wwwroot/yfw.szrengjing.com [root@yfw yfw.szrengjing.com]# ./deploy.sh 🔧 开始自动化部署... fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git No composer.json in current directory, do you want to use the one at /www/wwwroot? [Y,n]? y Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default. Installing dependencies from lock file Verifying lock file contents can be installed on current platform. Nothing to install, update or remove Generating optimized autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! Could not open input file: think Could not open input file: think chown: changing ownership of 'public/.user.ini': Operation not permitted chmod: changing permissions of 'public/.user.ini': Operation not permitted PHP Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 PHP Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Failed to reload php7.4-fpm.service: Unit php7.4-fpm.service not found. 🎉 部署完成 [root@yfw yfw.szrengjing.com]# lsattr /www/wwwroot/yfw.szrengjing.com/public/.user.ini ----i---------e----- /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# chattr -i /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# cd /www/wwwroot [root@yfw wwwroot]# [root@yfw wwwroot]# # 备份旧站点 [root@yfw wwwroot]# mv yfw.szrengjing.com yfw.szrengjing.com.bak [root@yfw wwwroot]# [root@yfw wwwroot]# # 使用 Composer 创建新 TP6 项目 [root@yfw wwwroot]# composer create-project topthink/think yfw.szrengjing.com Creating a "topthink/think" project at "./yfw.szrengjing.com" Cannot use topthink/think's latest version v8.1.3 as it requires php >=8.0.0 which is not satisfied by your platform. Installing topthink/think (v6.1.0) - Downloading topthink/think (v6.1.0) - Installing topthink/think (v6.1.0): Extracting archive Created project in /www/wwwroot/yfw.szrengjing.com Loading composer repositories with package information Updating dependencies Lock file operations: 17 installs, 0 updates, 0 removals - Locking league/flysystem (1.1.10) - Locking league/flysystem-cached-adapter (1.1.0) - Locking league/mime-type-detection (1.16.0) - Locking psr/cache (1.0.1) - Locking psr/container (1.1.2) - Locking psr/http-message (1.1) - Locking psr/log (1.1.4) - Locking psr/simple-cache (1.0.1) - Locking symfony/polyfill-mbstring (v1.33.0) - Locking symfony/polyfill-php72 (v1.31.0) - Locking symfony/polyfill-php80 (v1.33.0) - Locking symfony/var-dumper (v4.4.47) - Locking topthink/framework (v6.1.5) - Locking topthink/think-filesystem (v1.0.3) - Locking topthink/think-helper (v3.1.11) - Locking topthink/think-orm (v2.0.62) - Locking topthink/think-trace (v1.6) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 17 installs, 0 updates, 0 removals - Downloading psr/cache (1.0.1) - Downloading psr/container (1.1.2) - Downloading psr/http-message (1.1) - Downloading symfony/var-dumper (v4.4.47) - Downloading topthink/think-helper (v3.1.11) - Downloading psr/simple-cache (1.0.1) - Downloading psr/log (1.1.4) - Downloading topthink/think-orm (v2.0.62) - Downloading topthink/framework (v6.1.5) - Downloading league/flysystem (1.1.10) - Downloading league/flysystem-cached-adapter (1.1.0) - Downloading topthink/think-filesystem (v1.0.3) - Downloading topthink/think-trace (v1.6) - Installing league/mime-type-detection (1.16.0): Extracting archive - Installing psr/cache (1.0.1): Extracting archive - Installing psr/container (1.1.2): Extracting archive - Installing psr/http-message (1.1): Extracting archive - Installing symfony/polyfill-php80 (v1.33.0): Extracting archive - Installing symfony/polyfill-php72 (v1.31.0) - Installing symfony/polyfill-mbstring (v1.33.0): Extracting archive - Installing symfony/var-dumper (v4.4.47): Extracting archive - Installing topthink/think-helper (v3.1.11): Extracting archive - Installing psr/simple-cache (1.0.1): Extracting archive - Installing psr/log (1.1.4): Extracting archive - Installing topthink/think-orm (v2.0.62): Extracting archive - Installing topthink/framework (v6.1.5): Extracting archive - Installing league/flysystem (1.1.10): Extracting archive - Installing league/flysystem-cached-adapter (1.1.0): Extracting archive - Installing topthink/think-filesystem (v1.0.3): Extracting archive - Installing topthink/think-trace (v1.6): Extracting archive 12 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files > @php think service:discover Succeed! > @php think vendor:publish Succeed! 6 packages you are using are looking for funding. Use the `composer fund` command to find out more! Found 1 security vulnerability advisory affecting 1 package. Run "composer audit" for a full list of advisories. [root@yfw wwwroot]# ls -la /www/wwwroot/yfw.szrengjing.com total 108 drwxr-xr-x 10 root root 4096 Nov 14 17:05 . drwxr-xr-x 13 www www 4096 Nov 14 17:05 .. drwxr-xr-x 3 root root 4096 Oct 24 2022 app -rw-r--r-- 1 root root 1138 Oct 24 2022 composer.json -rw-r--r-- 1 root root 37667 Nov 14 17:05 composer.lock drwxr-xr-x 2 root root 4096 Nov 14 17:05 config -rw-r--r-- 1 root root 231 Oct 24 2022 .example.env drwxr-xr-x 2 root root 4096 Oct 24 2022 extend -rw-r--r-- 1 root root 34 Oct 24 2022 .gitignore -rw-r--r-- 1 root root 1822 Oct 24 2022 LICENSE.txt drwxr-xr-x 3 root root 4096 Oct 24 2022 public -rw-r--r-- 1 root root 1459 Oct 24 2022 README.md drwxr-xr-x 2 root root 4096 Oct 24 2022 route drwxr-xr-x 2 root root 4096 Oct 24 2022 runtime -rw-r--r-- 1 root root 180 Oct 24 2022 think -rw-r--r-- 1 root root 2038 Oct 24 2022 .travis.yml drwxr-xr-x 8 root root 4096 Nov 14 17:05 vendor drwxr-xr-x 2 root root 4096 Oct 24 2022 view [root@yfw wwwroot]#
11-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值