Home>
I want to use Non Thread-PHP with Nginx.

OS: win10
DB: MariaDB
Server: Nginx/PHP
/ users/server/php
/ users/server/nginx

PHP = system environment variables
Nginx = user environment variables

What I tried

(Last time:NonThread-not PHPThread-PHPI was able to connect with)

https://www.tutorialfor.com/go.php?id=303745
At this time, Nginx is not used. (I was running PHP alone)

But to work with Nginx,Thread-PHPnot
Non Thread-PHPI learned from the following site that I should use
https://ti-tomo-knowledge.hatenablog.com/entry/2018/06/14/125116
I tried to move it in the same way as last time.



I couldn't connect for some reason and it was messed upTextificationI'm sorry.

Next time
If i dare to add a semicolon with PHP extension

Extension = pdo_mysql


↓ The result is as follows.

Not garbled!
However, the error in the execution result is the same as before. ↓

Apparently it is necessary to set nginx.conf on the Nginx side.
https://qiita.com/suke/items/4936809289ffe0dc782b
For the time being, copy and paste. Execute ↓

#Specify the user who executes the worker pro theory
#user nobody;
# Number of worker processes (child processes of nginx that receive and process requests from clients)
worker_processes 1;
#File location to save process ID
#pid logs/nginx.pid;
events {
    # I have the maximum number of connections per worker process
    worker_connections 1024;
}
http {
    #### MIME type settings
    include mime.types;# Load files with extensions and MIME typesdefault_type application/octet-stream;#MIME type when the MIME type cannot be determined from the extension
    #### Access log settings
    #Set access log format, specify main format in the following format
    log_format main'$remote_addr-$remote_user [$time_local] "$request"'
                      '$status $body_bytes_sent "$http_referer"'
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #Specify the output location and format of access log, output to logs/access.log in main format
    #access_log logs/access.log main;
    #### Settings related to sending (response) to the client
    # Whether to use the sendfile system call to send to the client
    sendfile on;
    # Whether to use the Linux TCP_CORK socket option
    #tcp_nopush on;
    #keepalive timeout time setting, 65 seconds
    keepalive_timeout 65;
    #Setting whether to compress and transfer content when sending it to the client
    #gzip on;
    #### Virtual server settings
    server {
        #### Server preferences
        # Set the IP address and port number that accepts requests
        listen 80;
        # Set the host name of the virtual server
        server_name localhost;
        #Specify root document, specify directory hierarchy with "/" instead of "\" in windows
        root C:/server/htdocs/testProject/public;
        #index Filename set,
        #Specify a file name to be used as an index for URLs ending in "/"
        index index.php index.html index.htm;#Applies in order from the front
        #### Log settings by server
        # Access log output destination and output format setting, output in main format
        access_log logs/access.log main;
        # Error log output destination and logging level setting, output up to notice level
        error_log logs/error.log notice;
        #### Error page settings
        #error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;#Set the path of the error page
        location = /50x.html {# The path exactly matches "/50x.html", rounded up if matched
            root html;
        }
        #### PHP settings
        #Regular expression, path matches "xxxx.php", "aaa/xxxx.php", "abc/xxxx.php/bbbb", etc.
        location ~ [^ /] \ .php (/ | $) {
            Define a regular expression that captures the value of # $fastcgi_path_info
            # The first () is the value of $fastcgi_script_name and the second is the value of $fastcgi_path_info
            fastcgi_split_path_info ^ (. +? \. Php) (/. *) $;
            # $document_root Path set in the root directory# $fastcgi_script_name Filename path
            # -f determines if the file exists
            # Returns a 404 error if there is no file
            if (! -f $document_root $fastcgi_script_name) {
                return 404;
            }
            # IP address and port settings to access php
            fastcgi_pass 127.0.0.1:9000;
            #Set index file name in php,
            #Specify a file name to be used as an index for URLs ending in "/"
            fastcgi_index index.php;
            #### Settings related to fastcgi_param
            # Read the fastcgi_params file with many fastcgi_params settings
            include fastcgi_params;
            # Empty the "HTTP_PROXY" parameter to mitigate HTTPoxy vulnerabilities
            fastcgi_param HTTP_PROXY "";
            # "SCRIPT_FILENAME" parameter is a setting for php-fpm to determine the script name
            fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;
        }
    }
}


There was progress ↓It has changed to "Error: could not find driver"

Try removing the semicolon with;extension = pdo_mysql in php.ini.

The error response is the same as "Error: could not find driver"
The garbled characters on the command side are terrible,

Nginx.ini I just copied and pasted the whole thing
Of course, it may not be possible due to a mistake in specifying the file.
So

  #### Virtual server settings
    server {
        #### Server preferences
        # Set the IP address and port number that accepts requests
        listen 80;
        # Set the host name of the virtual server
        server_name localhost;
        #Specify root document, specify directory hierarchy with "/" instead of "\" in windows
        root C:/server/nginx/html;
# Copying =>C:/server/htdocs/testProject/public;
#Before copy and paste =>root html;

The execution result did not change.

It has nothing to do with the question, but I'm still wondering where the characters are garbled.
If anyone knows, please let me know.

  • Answer # 1

    Run PHP using Nginx in Win10 environment + connect to DB.

    "Non Thread Safe version" is recommended when running PHP using Nginx.
    However, PHP worked even if I used the following method,Could not connect to DB.

    So"Thread Safe version"However, I tried it the same way and it was done.
    ↓ Details after ↓

    OS: win10
    DB: MariaDB
    server: Nginx
    PHP-https://www.php.net/downloads

    Hierarchical construction format
    C: \ server \ php \ php-cgi.exe
    C: \ server \ nginx \ html
    MariaDB and PHP-cgi environment variables have been edited.

    [Nginx file]
    nginx \ conf \ nginx.conf

    [PHP php.ini file]
    php \ php.ini

    Edit each file.

    1) Edit the Nginx file

    First, in C: \ server \ nginx \ htmlphpinfo.phpTo create
    Use a text editor to read the contents of phpinfo.php

    <? php
    phpinfo ();
    ?>


    And then writeEdit Nginx fileI will continue to do it.

    Set with reference to the following site

    #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 localhost;
            # charset koi8-r;
            #access_log logs/host.access.log main;
            location/{
                root html;
                index index.php index.html index.htm;
            }
            #error_page 404 /404.html;
            #redirect server error pages to the static page /50x.html
            #
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                root 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 ${
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME C:/server/nginx/html $fastcgi_script_name;
                include fastcgi_params;
            }
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            # deny all;
            #}
        }
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        # listen 8000;
        # listen somename: 8080;
        # server_name somename alias another.alias;
        # location/{
        #root html;
        # index index.html index.htm;
        #}
        #}
        # HTTPS server
        #
        #server {
        # listen 443 ssl;
        # server_name localhost;
        # ssl_certificate cert.pem;
        # ssl_certificate_key cert.key;
        # ssl_session_cache shared: SSL: 1m;
        # ssl_session_timeout 5m;
        # ssl_ciphers HIGH:! a NULL:! MD5;
        # ssl_prefer_server_ciphers on;
        # location/{
        #root html;
        # index index.html index.htm;
        #}
        #}
    }


    ↓ Editing contents ↓

    (9th line and after)

    Since the item of "pid" is commented out with "#", uncomment it, modify the path as follows and save it.

    pid C: /servers/nginx/logs/nginx.pid;

    (↓ I just uncommented without touching anything, but it worked.)

    pid logs/nginx.pid;

    ---------------
    (From the 43rd line)
    Add index.php to the index specification.
    Now, by default nginx goes to read index.php ↓

    location/{
    root html;
    indexindex.phpindex.html index.htm;
    }
    ---------------
    (From line 65 onwards)
    I made it earlierphpinfo.phpWhere is placedC: \ server \ nginx \ htmlSo
    In Nginx conffile

    location ~ \ .php ${
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAMEC:/server/nginx/html$fastcgi_script_name;
    include fastcgi_params;
    }
    ---------------

    I will rewrite it.

    At this point, run and start Nginx from the command prompt,
    PHP-CGI (with another command promptphp-cgi.exe -b 127.0.0.1:9000)
    In your browser127.0.0.1/phpinfo.php] And search


    The PHP version and details are displayed.
    If the display is successful,That PHP worked fineIt is almost as if I was half successful.

    nextConnection to DBis,


    2) Edit the php.ini file
    To connect PHP running on Nginx to a database, first

    In C: \ server \ php \php.iniCheck if there is or not, if not
    php.ini-production
    php.ini-development
    There should be files such as, so either of the two!
    This time, copy and duplicate php.ini-development and name itphp.iniI will change it to.

    After renaming
    Text editorEtcOpen the contents of php.ini.

    Approximately after the 900th line
    ";extension = pdo_mysqlI searched for the syntax and commented it out with a semicolon.
    semicolonI will remove it.

    extension = pdo_mysql

    * By the way, on some old reference sites
    Search for "php_mysql.dll or extension = php_mysqli.dll".
    It says, but now it seems that it has changed to;extension = pdo_mysql.

    laterC: \ server \ nginx \ htmlInCreate a test.php file
    Configure the executable statement to connect to the database and save it by overwriting.

    <html><head><title>PHP TEST</title></head><body><? php
    $dsn ='mysql: dbname = test;host = localhost';
    $user ='myname';
    $password ='mypass';
    try {$dbh = new PDO ($dsn, $user, $password);
        print ('<br>');
        if ($dbh == null) {
            print ('Connection failed.<br>');
        } else {
            print ('Connect successfully.<br>');
        }
    } catch (PDOException $e) {
        print ('Error:'. $E->getMessage ());
        die ();
    }
    $dbh = null;
    ?></body></html>

    Then again
    Run Nginx from the command prompt
    Run PHP-CGI with another command prompt

    [127.0.0.1/test.php] To display the browser

    The connection was successful.....