PHPStudy2018环境部署全攻略

PHPStudy2018环境部署全攻略 PHPStudy网站部署攻略一、部署环境准备1.1 软件清单软件版本用途下载/安装方式PHPStudy 20182018版集成环境推荐官网下载https://www.xp.cn/download.htmlNginx1.15.11Web服务器PHPStudy自带PHPStudy集成PHP5.6.27nts脚本解析器PHPStudy自带PHPStudy集成MySQL5.6数据库需单独切换PHPStudy软件管理中安装5.6版本HeidiSQL / phpMyAdmin-数据库管理工具PHPStudy自带phpMyAdmin1.2 兼容性要点PHP版本必须为5.6网站基于ThinkPHP3.2.3开发MySQL必须用5.65.7以上可能存在兼容性问题Web服务器可选最终采用Nginx支持伪静态规则二、部署步骤2.1 安装PHPStudy 2018下载PHPStudy 2018版不是8.1版本安装路径D:\phpStudy\PHPTutorial\避免中文和空格启动后在软件管理中安装MySQL 5.6切换PHP版本为5.6.27nts2.2 放置网站文件网站根目录D:\phpStudy\PHPTutorial\WWW\hn56at\必须包含以下文件和目录index.php- 前台入口admin.php- 后台入口ThinkPHP/- 框架核心Common/- 配置文件目录Admin/- 后台模块Index/- 前台模块Uploads/- 附件目录关键Public/- 静态资源目录2.3 数据库配置通过phpMyAdminhttp://localhost/phpMyAdmin/创建数据库导入网站数据文件xxx.sql修改D:\phpStudy\PHPTutorial\WWW\hn56at\Common\Conf\config.phpDB_HOSTlocalhost,DB_NAME数据库名,DB_USERroot,DB_PWDroot,2.4 PHP配置修改编辑D:\phpStudy\PHPTutorial\php\php-5.6.27-nts\php.ini; 关键1禁用open_basedir限制否则会报权限错误 ;open_basedir /www/wwwroot/hn56at.com/:/tmp/:/proc/ ; 关键2设置时区消除警告 date.timezone Asia/Shanghai ; 关键3开启必要扩展 extensionphp_mysql.dll extensionphp_mysqli.dll extensionphp_gd2.dll extensionphp_curl.dll extensionphp_mbstring.dll重要修改php.ini后必须重启PHP-CGI才能生效2.5 Nginx核心配置文件位置D:\phpStudy\PHPTutorial\nginx\conf\nginx.confworker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root D:/phpStudy/PHPTutorial/WWW; # 1. 静态文件处理优先级最高 location /Uploads/ { alias D:/phpStudy/PHPTutorial/WWW/hn56at/Uploads/; expires 30d; access_log off; # 关键alias替换路径不加break可能导致后续匹配 break; } location /Public/ { alias D:/phpStudy/PHPTutorial/WWW/hn56at/Public/; expires 30d; access_log off; break; } location /uploadfile/ { alias D:/phpStudy/PHPTutorial/WWW/hn56at/uploadfile/; expires 30d; access_log off; break; } # 2. 根目录下的伪静态请求 location / { # 捕获伪静态URL重定向到子目录 if ($request_uri ~ ^/(list|show|article)-\d-\d\.html) { rewrite ^/(.*)$ /hn56at/$1 permanent; } try_files $uri $uri/ rewrite; } location rewrite { rewrite ^/(.*)$ /hn56at/index.php?s$1 last; } # 3. 子目录处理核心伪静态规则 location /hn56at/ { index index.php index.html; # 关键文件不存在才重写到index.php if (!-e $request_filename) { rewrite ^/hn56at/(.*)$ /hn56at/index.php?s$1 last; } } # 4. 根目录跳转 location / { return 302 /hn56at/; } # 5. PHP解析 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }三、关键问题及解决方案3.1 open_basedir限制最坑现象访问PHP文件报错open_basedir restriction in effect解决php.ini中注释掉open_basedir并重启PHP-CGI注phpStudy内置PHP-CGI重启phpStudy即可达到重启PHP-CGI的效果3.2 图片404问题现象HTML中图片路径为/Uploads/xxx.jpg但文件实际在/hn56at/Uploads/解决用alias做路径映射而不是rootlocation /Uploads/ { alias D:/phpStudy/PHPTutorial/WWW/hn56at/Uploads/; }3.3 伪静态404问题现象访问/list-28-1.html报404解决在子目录中配置rewrite规则并用!-e判断文件是否存在3.4 子目录访问问题现象直接访问/hn56at/显示403解决添加index index.php index.html;指定默认首页3.5 PHP-CGI不生效问题现象修改php.ini后配置不生效解决必须重启PHP-CGI进程不能只重启Nginxtaskkill /F /IM php-cgi.exe cd /d D:\phpStudy\PHPTutorial\php\php-5.6.27-nts php-cgi.exe -b 127.0.0.1:9000 -c php.ini四、测试与验证4.1 访问测试首页跳转http://localhost/→/hn56at/图片访问http://localhost/Uploads/2024-09-03/xxx.jpg伪静态http://localhost/hn56at/list-28-1.html后台http://localhost/hn56at/admin.php4.2 常用命令# 测试Nginx配置 cd /d D:\phpStudy\PHPTutorial\nginx nginx -t # 重启Nginx nginx -s reload # 完全重启Nginx nginx -s stop nginx # 查看错误日志 type D:\phpStudy\PHPTutorial\nginx\logs\error.log五、注意事项总结PHP版本必须5.6MySQL必须5.6open_basedir是最大的坑修改后必须重启PHP-CGI静态文件用alias而不是root伪静态规则要放在子目录内处理并判断文件是否存在location匹配顺序静态文件 伪静态 PHP解析修改hosts文件做域名测试后记得删除或注释掉避免影响线上访问六、线上迁移提示部署到线上服务器时需注意线上环境通常也是Nginx PHP5.6 MySQL5.6将本地的Nginx配置适配到线上修改root路径数据库连接配置改为线上数据库信息上传目录Uploads需要设置写入权限