Tomcat 下运行 php 文件(Windows):https://blog.csdn.net/qq_28033719/article/details/80361823
因为有了解过 Linux 也需要尝试去部署 php 项目,所以现在再写一个关于 Linux 的部署。
我个人进行一些准备:
腾讯学生服务器 : ¥10 / 月
https://cloud.tencent.com/act/campus?fromSource=gwzcw.3775866.3775866.3775866&utm_medium=cpc&utm_id=gwzcw.3775866.3775866.3775866
操作系统是:CentOS 7.6 64位
因为省事,所以自己去买了个服务器玩,里面需要用到 yum wget tar 都有了,linux 基本功能完善,如果自己处理的话可以自行安装模块。
jdk 环境会配置到 /usr/lib/jvm 下面,可以 ll 查看这个文件夹命名,然后
vim /etc/profile进行环境变量配置
done unset i unset -f pathmunge # 放到最后面环境变量 JAVA_HOME=/usr/lib/jvm/jre-1.8.0 PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar # 需要导出的变量名 export JAVA_HOME CLASSPATH PATH然后覆盖
ESC :wq java ## 查看 java 环境变量1、到 网址 https://tomcat.apache.org/download-80.cgi 获取 tomcat,我使用 wget ,选择 tar.gz 并安装,如果连接失效可以去官网重新复制一条需要下载的php连接即可,wget 也有可能下载出现中断,自己下载然后手动上传也可以
wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz tar -zxvf apache-tomcat-8.0.23.tar.gz mkdir /usr/local/tomcat mv apache-tomcat-8.0.23 /usr/local/tomcat cd /usr/local/tomcat/apache-tomcat-8.0.23/bin ./startup.sh2、然后访问:
你的IPV4公网主机IP:8080,腾讯服务器实例信息里面可以找到,tomcat 如果不能开箱即用就查看我的 windows 的处理,是一样的处理方法,查看日志。
3、安装之后需要四个 JAR 包:
JavaBridge.jar、php-servlet.jar、php-script.jar、script-api.jar:
https://pan.baidu.com/s/1Htrn1SzWgxjdFcGMtS2lBg 密码:9st9
把 JAR 复制到,tomcat的lib目录下,放了jar之后需要查看tomcat能否启动。
cp -R /root/phpBRIDGEjava/* /usr/local/tomcat/你的tomcat路径/lib/ cd /usr/local/tomcat/你的tomcat路径/bin ./startup.sh访问 你的IPV4公网主机IP:8080,正常就可以继续下面流程了。
1、到网址 https://www.php.net/downloads.php 选择 tar.gz 压缩包,然后如果使用 wget 命令的话,可以直接复制下载链接,然后输入命令 (我下载的是7.3.22,如果连接失效可以去 php 网址复制一个新的下载链接):
wget https://www.php.net/distributions/php-7.4.10.tar.gz下载或者上传之后进行解压
tar -zxvf php-*.tar.gz (你的 php 压缩包)2、然后进行配置,要注意 configure 的编译配置
cd php-*/ //你的解压 php 目录 // 注意配置里面的安装目录, enable 选项 ./configure --prefix=/root/php-7.4.10 // php 目录前缀 --with-config-file-path=/usr/local/php/etc // php 的 etc mulu --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi // 使用 cgi --enable-fpm --enable-force-cgi-redirect // 这里开启重定向了,会保护直接访问 php 文件的处理 --enable-mbstring // 正则处理,配置需要 --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap // 我的最终配置是这样,必须要压成一行 ./configure --prefix=/root/php-7.4.10 --with-config-file-path=/root/php-7.4.10/etc --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fastcgi--enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap如果出现缺省就需要一直解决,比如
No package 'libxml-2.0' found (某个配置没有) No package 'sqlite3' found可以马上处理掉,比如我的这个 libxml,有时候 yum 安装还是找不到就 wget 获取并且安装 然后环境变量里面配
// 安装 libxml2 // centos yum yum install libxml // apt apt install libxml2 // wget wget ftp://xmlsoft.org/libxml2/libxml2-*.tar.gz(通过 ftp 获取版本) tar -zxvf libxml2-*.tar.gz cd libxml2-*/ ./configure --prefix=/usr/local //我的安装目录,不指定前缀可以的,直接装到 /usr/local下,会有目录分配,而且 etc/profile指定也好处理 make make install 然后查看 /usr/local/lib/pkgconfig 目录下有没有 libxml-2.0.pc vim /etc/profile export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH一直处理到编译ok
一些帮助指令
yum search libcurl // 查找 libcurl 库 rpm -ql libcurl // 找到安装包的安装路径 yum list installed | grep libcurl // 列出信息乱七八糟的搞定之后就可以
make make install然后将 php 根目录的 php.ini-development 复制修改为 php.ini,再进行 ini 修改这三个基本地方
要注意,本文直接使用 *.php 的调用方式访问 php 文件,所以如果安装时候开启了 --enable-force-cgi-redirect
https://www.php.cn/manual/view/298.html
https://www.php.net/manual/zh/security.cgi-bin.force-redirect.php
你就需要修改一下 ini 配置
(1)cgi.force_redirect = 0 cgi.redirect_status_env ="yes"; //这个是强制跳转,如果没有的话,页面会读取不到 *.php 的文件 --enable-force-cgi-redirect安装时候就进行了处理 。。。。。。 (2)extension_dir="/root/php-7.4.10/ext" ;//这里是你的 php/ext 位置 。。。。。。 (3)extension=mbstring ;//这个是正则处理,没有的话,访问页面可能抛出 The mbstring extension is missing.通常都在php的目录下 //通常 window 编译时候可以完全编译, linux 需要 enable 选项进行编译, 不然会缺失 记住,然后把 */php*/bin/php-cgi 拷贝到 php 按照根目录,因为执行文件读取的 php.ini 是读取根目录下的配置,如果不拿到和 php.ini 一起的目录,那么配置文件不生效,除非将配置文件 .ini 放到全局配置中。
cp /root/php/bin/php-cgi /root/php/php-cgiOK , php 的处理算是完成了
1、修改 TOMCAT / conf / context.xml ;在 Context 节点下添加 privileged="true"
<Context privileged="true"> ...... </Context>2、在 Tomcat / conf / 找 web.xml,添加
<!-- 尝试成功 --> <servlet> <servlet-name>php</servlet-name> <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class> <init-param> <param-name>clientInputTimeout</param-name> <param-value>200</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>executable</param-name> <!-- 这里是放置你的 php/php-cgi.exe --> <param-value>/root/php-7.4.10/php-cgi</param-value> </init-param> <init-param> <param-name>passShellEnvironment</param-name> <param-value>true</param-value> </init-param> <!-- 指定拦截前缀,可用可不用,使用之后URL需要带上 前缀 + 原访问路径 --> <!-- <init-param> <param-name>cgiPathPrefix</param-name> <param-value>WEB-INF/mibew</param-value> </init-param> --> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>php</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping>再次启动 tomcat,并且再次访问 8080 看是否成功
3、用mantisbt做测试项目:https://www.mantisbt.org/
然后把PHP项目放到上传到 linux tomcat 的 webapp 目录下
4、然后在浏览器点出你的php文件路径(以webapps为根目录),运行 php 文件
(你的ip网址):8080/mantisbt/admin/install.php
一样最后也搞定了
