php web server部署(PHP+Nginx+Redis+MySQL)

4 篇文章 0 订阅
2 篇文章 0 订阅

1. 运行环境和软件准备

操作系统:Windows10_x64
数据库:mysql-5.7.17-winx64.msi
Redis:Redis-x64-3.2.100.msi
PHP:php-7.3.9-Win32-VC15-x64.zip
PHP插件模块:php_igbinary-2.0.8-7.3-ts-vc15-x64.zip
PHP插件模块:php_redis-4.2.0-7.3-ts-vc15-x64.zip
服务器:nginx-1.18.0.zip
例子工程:https://e.coding.net/ersu/supply_chain/supply_chain.git

2. MySQL部署

安装略。
针对例子工程进行用户、数据库、表的创建

先用root用户登录MySQL

--创建用户
CREATE USER 'admin'@localhost IDENTIFIED BY 'admin';
--创建数据库
CREATE DATABASE IF NOT EXISTS supply_chain CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
--用户授权
grant all privileges on supply_chain.* to admin@localhost identified by 'admin';
--刷新授权
flush privileges;

用刚创建的admin用户登录MySQL

--切换数据库
use supply_chain
--建表
CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `ip` varchar(50) DEFAULT NULL,
  `profit` int(11) DEFAULT '0',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

3. Redis部署

Redis-x64-3.2.100.msi文件双击打开一路Next。安装完成后,进入到安装目录下,带参数(配置文件名)运行redis-server.exe就可以了。却省的绑定端口是6379。

D:\Redis>redis-server.exe redis.windows.conf
[18320] 12 Mar 10:47:33.307 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error

D:\Redis>

4. 安装Nginx

可以参考《Windows上部署Ngnix
先直接解压,然后运行ngnix.exe反向代理服务程序:
在这里插入图片描述
此时在浏览器地址栏中输入http://localhost可以看到ngnix的欢迎页面。
在这里插入图片描述
然后修改ngnix.conf配置文件
在这里插入图片描述
这一步可以只改标注红色1的内容,用于等步骤5完成后查看PHP信息(在nginx-1.18.0/html/目录下创建文件index.php,内容如下);标注红色2的内容可以等到部署完应用后再改。

<?php
phpinfo();
?>

5. PHP部署

先附上PHP官网,注意PHP分为“线程安全(thread safe,简称ts)”版和“非线程安全”(none thread safe,简称nts)版,本文用的是ts版。
下载php-7.3.9-Win32-VC15-x64.zip后,将php安装文件解压,然后进入安装目录,将php.ini-development复制一份更名为php.ini
在这里插入图片描述
修改配置文件php.ini:

;extension_dir = “ext” 改为 extension_dir = “ext” (去掉注释)
enable_dl = Off 改为 enable_dl = On (Off改成On)
;cgi.force_redirect = 1 改为 cgi.force_redirect = 0 (去掉注释并将1改成0)

在这里插入图片描述

;cgi.fix_pathinfo=1 改为 cgi.fix_pathinfo=1 (去掉注释)
;fastcgi.impersonate = 1 改为 fastcgi.impersonate = 1 (去掉注释)

在这里插入图片描述

;extension=mysqli 改为 extension=mysqli (去掉注释)
;extension=openssl 改为 extension=openssl (去掉注释)
;extension=pdo_mysql 改为 extension=pdo_mysql (去掉注释)

在这里插入图片描述
然后带参数启动php-cgi.exe

php-cgi.exe -b 127.0.0.1:9000 -c php.ini

没有输出就是正确
在这里插入图片描述
然后修改Nginx的配置文件ngnix.conf,将Nginx和PHP进行关联。
在这里插入图片描述
修改完,保存,重启Nginx。

如果Ngnix和php-cgi程序都正常启动的话,此时在浏览器地址栏输入http://localhost可以看到php的信息页面。
在这里插入图片描述

6. 配置PHP的Redis支持插件

PHP分为“线程安全(thread safe,简称ts)”版和“非线程安全”(none thread safe,简称nts)版,相关插件也需要搭配对应版本。本文开头部分的链接配套的都是ts版。
这里也放上PHP官方扩展网站:
https://windows.php.net/downloads/pecl/releases/redis/
https://windows.php.net/downloads/pecl/releases/igbinary/
php_igbinary-2.0.8-7.3-ts-vc15-x64.zipphp_redis-4.2.0-7.3-ts-vc15-x64.zip下载后解压。然后分别将里面的 php_igbinary.dll 和 php_redis.dll,复制到PHP的ext目录下。然后修改php.ini文件,增加:

extension=igbinary
extension=redis

或者

extension=php_igbinary.dll
extension=php_redis.dll

效果是一样的。
在这里插入图片描述
然后重新启动php-cgi:

php-cgi.exe -b 127.0.0.1:9000 -c php.ini

这时再看php的信息页面,能看到页面中有redis相关信息。
在这里插入图片描述

7. 部署应用

将步骤4中的nginx.conf配置文件改为最终的样子,然后重启ngnix.exe即可。
全部修改好以后最终的nginx.conf配置文件如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  ws-manage-dev.jammyfm.com;
        root         E:/test/SupplyChain/public;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /api {
            index  index.php index.html;
            try_files $uri $uri/ /index.php?$query_string;
        }

        location / {
            root  E:/test/SupplyChain/public/dist;
            index  index.html index.htm index.php;
        }


        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        
        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;
        }
    }
}

例子工程中有一处需要修改。是位于SupplyChain/public/dist/目录下的config.js文件(改文件未纳入版本管理,从创建和修改时间上看,应该是应用初次运行时生成的)。其中的baseURL的值应改为实际服务器的地址。【*版本更新】后来经过开发团队的调整,写为空字符串(仅一对双引号)也行。

window.g = {
    baseURL: "http://www.chenth.net/",
    real: false
}

如果没有域名,则写成http://localhost/
此处若没改对,则“Next Day”按钮点击不响应。
工程项目最终效果如下。
在这里插入图片描述
手机访问效果也不错,为开发团队点个赞哈!
在这里插入图片描述

8.总结
  1. 因为本文是验证性部署,所以php、ngnix、redis都没有配置成系统服务,需要手动启动,特此说明。
  2. 虚拟服务器是Windows Server 2019操作系统,只有IE浏览器(连edge也没有)。导致index.php始终无法解析,怪的是我在自己的机器上用chrome输入 “http://域名/index.php” 远程访问也始终无法解析,反复查Nginx配置都没有找到问题。最后在服务器上安装了chrome,服务器就可以解析了,随后远程访问也能解析了。没想通,有人知道原因的,欢迎留言。

【参考资料】《Windows上搭建PHP开发环境

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值