简体   繁体   中英

How do I configure nginx and CodeIgniter?

I'm running nginx on my home computer for development. I also have it linked to DynDNS so I can show progress to my co-worker a bit easier. I can't seem to get nginx to rewrite to CodeIgniter properly. I have CodeIgniters uri_protocol set to REQUEST_URI.

All pages that should be showing up wtih content show up completely blank. If I phpinfo(); die(); in the index.php file of Codeigniter, it works as expected and I get the phpinfo.

Also, pages that should give a 404 give a proper CodeIgniter 404 error.

Here's what I have so far.

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    #gzip  on;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    root /home/zack/Development/public_html;

    include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;


    server {
        listen 80;
        server_name zackhovatter.dyndns.info;

        index index.php;
        root /home/zack/Development/public_html;

        location /codeigniter/ {
            if (!-e $request_filename) {
                rewrite ^/codeigniter/(.*)$ /codeigniter/index.php/$1 last;
            }
        }
            #this is for the index.php in the root folder
        location /index.php {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /home/zack/Development/public_html/index.php;
            include         fastcgi_params;
        }

        location /codeigniter/index.php {
            fastcgi_index   index.php;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME  /home/zack/Development/public_html/codeigniter/index.php;
            fastcgi_param   REQUEST_URI      $request_uri;
            fastcgi_param   QUERY_STRING     $query_string;
            fastcgi_param   PATH_INFO        $document_uri;
            fastcgi_param   REQUEST_METHOD   $request_method;
            fastcgi_param   CONTENT_TYPE     $content_type;
            fastcgi_param   CONTENT_LENGTH   $content_length;
            fastcgi_pass    127.0.0.1:9000;
        }
    }
}

Actually I got this working. Not sure how, but I think it was file permissions being set incorrectly or something.

Edit: For those who are wondering, I had an older version of PHP installed it seems. Reverting back to old class constructors fixed the issue. Darnit.

I have exactly the same problem (Codeigniter gives blank pages and the error pages and php are working) and i found that the problem was from the mysql db driver which should be changed to mysqli driver .

So just we need to change the db driver in config/database.php

$db['default']['dbdriver'] = 'mysqli';

And this is due that the mysql driver is deprecated in the new versions of php.

CodeIgniter4 How To Setup NGINX Server Blocks (Virtual Hosts) on RasperyPi Debian 10.4

I am using a RasperyPi-4 as web developing server with different projects running on it. Visual Studio Code enables editing files via Remote-SSH extension very easily. Setting up CI4 on NGINX gives you the opportunity to run different projects on the same server. Because it took me some days to get this configuration running I will give you a quick reference guide to setup quick and easy. If you have not installed NGINX and composer yet please have a look here: http://nginx.org/en/docs/beginners_guide.html

https://codeigniter4.github.io/userguide/installation/installing_composer.html

https://getcomposer.org/download/

  1. CodeIgniter4 installation via composer

server ip: 10.0.0.10 port: 8085 project name is 'test' Please modify to your needs!

cd /var/www/html
composer create-project codeigniter4/appstarter test

the command above will create a 'test' folder /var/www/html/test

  1. Modify the 'env' file

sudo nano /var/www/html/test/env


app.baseURL = 'http://10.0.0.10:8085'

Important: Do NOT use 'localhost' for the server URL!!!!!

please uncomment: # CI_ENVIRONMENT = production and modify: CI_ENVIRONMENT = development

Save the file as '.env' to hide the file

  1. NGINX Server Blocks

cd /etc/nginx/sites-available
touch test
sudo nano test

Paste this into the file and save after modifying to your requirements:

server {
    listen 8085;
    listen [::]:8085;

    server_name test;

    root  /var/www/html/test/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm:
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        # With php-cgi:
        # fastcgi_pass 127.0.0.1:9000;
    }

    error_page 404 /index.php;

    # deny access to hidden files such as .htaccess
    location ~ /\. {
        deny all;
    }
}

After saving the file create a symbolic link to sites-enabled:

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/test  /etc/nginx/sites-enabled/test
  1. Modify File and Group Permissions

chown -v -R pi:www-data /var/www/html/test
sudo chmod -v -R 775 /var/www/html/test
  1. Start NGINX

sudo service nginx start
  1. Open CI4 Welcome Page

 http://10.0.0.10:8085

Here is a solution.

https://web.archive.org/web/20120504010725/http://hiteshjoshi.com/linux/secure-nginx-and-codeigniter-configuration.html

Also, I had to change $config['uri_protocol'] = “DOCUMENT_URI”; to make it work.

UPDATE: Fixed the 404 url from web archive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM