Home  >  Article  >  Backend Development  >  How to use Nginx with PHP programming?

How to use Nginx with PHP programming?

WBOY
WBOYOriginal
2023-06-12 11:07:401756browse

Using Nginx in PHP programming is a very common requirement, because Nginx is a high-performance web server and reverse proxy server software that can help us handle requests, responses and load balancing more efficiently. This article will introduce how to use Nginx in PHP programming.

Step 1: Install and configure Nginx server

First, we need to install and configure Nginx server. On Linux systems, you can install Nginx through the package manager. For example, use the apt-get command on Ubuntu:

sudo apt-get update
sudo apt-get install nginx

Once the installation is complete, you can start the Nginx server:

sudo service nginx start

Now, we need to configure the Nginx server to handle PHP scripts. In Nginx, use FastCGI to process PHP scripts. You need to install FastCGI Process Manager (FPM) to handle PHP scripts.

In Ubuntu, you can use the following command to install:

sudo apt-get install php-fpm

After the installation is complete, you need to edit the Nginx configuration file to connect FastCGI with PHP-FPM.

In Ubuntu, you can edit the /etc/nginx/sites-available/default file and add the following configuration items in the server block:

location ~ .php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Next, restart the Nginx server to apply these changes:

sudo service nginx restart

Now, your Nginx server is configured and ready to handle PHP scripts.

Step 2: Use PHP-FPM and Nginx to process dynamic content

In order to process dynamic content, we can use PHP-FPM and Nginx to work together. PHP-FPM is a FastCGI process manager that can be set up as a backend service in Nginx. In this setup, Nginx sends requests to PHP-FPM and receives responses.

Add the following configuration in the Nginx configuration file:

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

In this configuration, the fastcgi_pass directive specifies the location of the backend PHP processor. It points to a Unix socket file, which is the address that the PHP-FPM server listens to.

Step 3: Process static content using PHP and Nginx

To process static content, we can simply serve the file directly to Nginx. For example, we can create the following configuration for static HTML files, CSS stylesheets, and JavaScript files:

location / {
   root /var/www/;
   index index.html index.htm;
}

location /css/ {
   root /var/www/;
   expires 1h;
}

location /js/ {
   root /var/www/;
   expires 1h;
}

In this configuration, we define the root directory (root), which specifies where all files can be found. The expiration mark for static files (HTML, CSS, and JavaScript) is set to 1 hour to ensure efficient use of browser cache.

Step 4: Run PHP applications using reverse proxy

If you want to run PHP applications, you can configure Nginx using reverse proxy mode. In reverse proxy mode, Nginx sends requests to PHP applications on behalf of the client. This code can demonstrate the reverse proxy mode very well:

server {
    listen       80;
    server_name  www.example.com;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

In this code, we set up the virtual host on Nginx and then configure the reverse proxy in the location block. The proxy_pass directive specifies the backend location of the PHP application. This backend location can use the local HTTP port (8000).

Conclusion

When using Nginx to handle dynamic and static content, you can not only improve performance, but also improve scalability and security. Additionally, using Nginx’s reverse proxy allows for easier configuration and deployment, as well as the ability to scale applications securely.

In this article, we cover how to install and configure an Nginx server, use PHP-FPM and Nginx to handle dynamic and static content, and use a reverse proxy to run PHP applications. With this, you can start using Nginx with PHP programming.

The above is the detailed content of How to use Nginx with PHP programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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