Home php教程 php手册 在Nginx上部署ThinkPHP项目教程

在Nginx上部署ThinkPHP项目教程

Jun 06, 2016 pm 08:11 PM
nginx thinkphp deploy

这篇文章主要介绍了在Nginx上部署ThinkPHP项目的方法,十分详细,需要的朋友可以参考下

前段时间用Apache配合TP开发了一个小型网站,也算是我使用TP的第一次实战。我习惯使用pathinfo模式,本地运行一切正常,然而部署到服务器上时,由于对方使用的是Nginx,默认是不支持ThinkPHP的pathinfo模式,所以导致路径错误,出现了页面循环跳转的问题,于是就把nginx下载下来在本地亲自测试。

      其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件,使它支持pathinfo。为了使问题简单化,我选择了第一种方式,因为就第二种方式,我查了很多资料,发现大家的方法不尽相同,有的还差别很大,容易造成误导,所以我选择从简出发,选择普通模式,虽然有一定的风险。当把index.php对应的前台代码修改完毕之后,发现前台基本正常,可是后台仍然出现重定向的问题。折腾了半天之后,我才想到看一下日志文件,原来是编辑器的问题,看来日志文件真的很重要,以前一直不重视。在config.php文件的第一行出现了输出,

在Nginx上部署ThinkPHP项目教程

      在sublime下,,一般会为UTF-8文件添加BOM头,这个BOM头在window下通常是看不见的,可以通过其他的编辑器查看到,Linux下也可以直接看到,通常显示出来是一个乱码字符,把这个字符删除即可,或者简单一点,直接在第一行回车,再删除就可以了。到这里,后台基本可以访问了。

      有几个小问题需要说明一下。

      1.在登录的时候,我是通过外部js文件发送Ajax请求进行验证的,在js与ThinkPHP模块函数通信遇到了点问题,一直不知道正确的路径该怎么写,也没有查到相关资料,只能各种试,好在找到了解决办法,通过直接带上入口文件名的方式,代码如下

复制代码 代码如下:


 var url="system.php?m=Login&a=doLog";
 $.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){
     if(data=="codeerr"){
           alert("验证码错误!");
     }else if(data=="authempty"){
           alert("请输入用户名或密码!")
     }else if(data=="autherr"){
           alert("用户名或密码错误!");
     }else if(data=="success"){
           alert("登录成功!");
            location.href="system.php?m=Index&a=index";   //访问首页
     }

    当然,此为普通模式下的访问方式,如果是pathinfo的话,只需要把红色部分如下修改即可

复制代码 代码如下:


 var url="doLog";
 $.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){
       if(data=="codeerr"){
               alert("验证码错误!");
       }else if(data=="authempty"){
               alert("请输入用户名或密码!")
       }else if(data=="autherr"){
               alert("用户名或密码错误!");
       }else if(data=="success"){
               alert("登录成功!");
               location.href="../Index/index";   //跳转首页,访问其他模块的方法

      2.下载文件的时候,总是莫名多出许多html的东西,原因是缓冲区没有清空,可以通过以下代码进行修改,不过这种方式实际上是下载的仍然是html格式的文件,只不过改了一下后缀名为xls而已,因而用excel打开的时候会提示格式问题,忽略即可。同时需要注意使用 icov()函数转换编码,因为xls默认编码格式并非utf-8.

复制代码 代码如下:


ob_start();
ob_end_clean();
Header( "Content-type: application/octet-stream");
Header( "Accept-Ranges: bytes ");
Header( "Content-type:application/vnd.ms-excel;charset=gb2312");  
Header( "Content-Disposition:attachment;filename={$filename}.xls");

      3.在删除文件时会遇到路径问题,因为项目中使用的较多的是相对路径,即相对入口文件而言,但是删除文件则需要使用绝对路径,我并没有找到合适的解决方法,只好用了比较保守的方式,代码如下

复制代码 代码如下:


 $path="./Public/uploads/";   
 $path=str_replace("\\","http://www.jb51.net/",realpath($path)."/");  //获取绝对路径,并转换分隔符

       4.在配置nginx和php方面,我使用了fastCGI的方式,将如下代码保存为cmd文件,直接点击运行就可以了

"F:\php\php-cgi.exe" -b 127.0.0.1:9000 -c "F:\php\php.ini"     //后面是php文件的路径
      然后在nginx的配置文件里加上几句话

复制代码 代码如下:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles