Install PHP + Nginx on a Unix System

Source: Internet
Author: User
Tags sapi
What is Nginx1.4.x in Unix? This document includes instructions and tips for installing and configuring PHP for the Nginx1.4.xHTTP server using a PHP-FPM. This Guide assumes that you have successfully built Nginx from the source code, and its binary files and configuration files are both located in usrlocalnginx. If you want to obtain Nginx in other ways, refer?

Nginx 1.4.x in Unix? This document includes instructions and tips for installing and configuring PHP for the Nginx 1.4.x HTTP server with a PHP-FPM. This Guide assumes that you have successfully built Nginx from the source code, and its binary files and configuration files are located in/usr/local/nginx. If you want to obtain Nginx in other ways, refer?

Nginx 1.4.x in Unix?

This document includes instructions and tips for installing and configuring PHP for the Nginx 1.4.x HTTP server with a PHP-FPM.

This Guide assumes that you have successfully built Nginx from the source code, and its binary files and configuration files are both located in/Usr/local/nginx. If you want to obtain Nginx in other ways, refer? Nginx Wiki is installed in accordance with this document.

This document only contains the basic configuration of the Nginx server. It provides the processing capability of the PHP application through port 80. If you require installation configuration guidance beyond the scope of this document, we recommend that you read the Nginx and PHP-FPM documentation.

Note that this document uses 'x' to indicate the version number. Replace 'X' with the corresponding version number based on the actual situation.

  1. How can I access Nginx Wiki? Install Nginx on your system.

  2. Obtain and decompress the PHP source code:

    tar zxf php-x.x.x

  3. Configure and build PHP. In this step, you can use many options to customize PHP, such as enabling some extensions. Run the./configure -- help Command to obtain the complete list of available options. In this example, we only perform simple configuration that contains the PHP-FPM and MySQL support.

    cd ../php-x.x.x./configure --enable-fpm --with-mysqlmakesudo make install

  4. Create a configuration file and copy it to the correct location.

    cp php.ini-development /usr/local/php/php.inicp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.confcp sapi/fpm/php-fpm /usr/local/bin

  5. Note that, if the file does not exist, Nginx is prevented from sending requests to the backend PHP-FPM module to avoid malicious script injection attacks.

    Set the configuration item cgi. fix_pathinfo in the php. ini file0.

    Open php. ini:

    vim /usr/local/php/php.ini

    GoCgi. fix_pathinfo =And modify it as follows:

    cgi.fix_pathinfo=0

  6. Before starting the service, you need to modify the php-fpm.conf configuration file to ensure that the php-fpm module runs as the www-data user and www-data user group.

    vim /usr/local/etc/php-fpm.conf

    Find and modify the following content:

    ; Unix user/group of processes; Note: The user is mandatory. If the group is not set, the default user's group;       will be used.user = www-datagroup = www-data

    Then start the php-fpm service:

    /usr/local/bin/php-fpm

    This document does not cover further configuration of php-fpm. For more information, see the relevant documentation.

  7. Configure Nginx to support PHP applications:

    vim /usr/local/nginx/conf/nginx.conf

    Modify the default location block to support the. php file:

    location / {    root   html;    index  index.php index.html index.htm;}

    Next configure to ensure that requests to the. php file will be sent to the back-end PHP-FPM module, uncomment the default PHP configuration block, and modify it to the following:

    location ~* \.php$ {    fastcgi_index   index.php;    fastcgi_pass    127.0.0.1:9000;    include         fastcgi_params;    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;}

    Restart Nginx.

    sudo /usr/local/nginx/sbin/nginx -s stopsudo /usr/local/nginx/sbin/nginx

  8. Create a test file.

    rm /usr/local/nginx/html/index.htmlecho "
       " >> /usr/local/nginx/html/index.php

    Open the browser and access http: // localhost. phpinfo () is displayed ().

Through the configuration above, the Nginx server can now useSAPIThe module supports PHP applications. Run the command in the corresponding source code directory../Configure -- helpFor more configuration options.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.