Home > Backend Development > PHP Tutorial > Nginx and PHP compilation and installation guide

Nginx and PHP compilation and installation guide

WBOY
Release: 2024-02-27 13:58:01
Original
1032 people have browsed it

Nginx and PHP compilation and installation guide

Nginx and PHP Compilation and Installation Guide

In the process of building the website server, Nginx serves as a high-performance web server and reverse proxy server, while PHP is a A popular server-side scripting language, the combination of the two can run dynamic websites quickly and stably. This article will introduce the compilation and installation process of Nginx and PHP in detail, and provide specific code examples so that readers can easily complete related operations when building their own website servers.

1. Install Nginx

1. Download Nginx

First, you need to download the latest version of the Nginx source code package from the Nginx official website. You can visit https://nginx.org/ to download the latest version.

2. Install dependencies

Before compiling and installing Nginx, you need to install some dependencies to ensure that Nginx can run normally. Enter the following command in the terminal:

sudo apt-get update
sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
Copy after login

3. Compile and install Nginx

Decompress the downloaded Nginx source code package and enter the decompressed folder. Then execute the following command to compile and install:

./configure
make
sudo make install
Copy after login

4. Start Nginx

After the installation is completed, enter the following command in the terminal to start the Nginx server:

sudo nginx
Copy after login

2. Install PHP

1. Download PHP

Similarly, you need to download the latest version of the PHP source code package from the official PHP website. You can visit https://www.php.net/ to download the latest version.

2. Install dependencies

Before compiling and installing PHP, you need to install some dependencies to ensure that PHP can run normally. Enter the following command in the terminal:

sudo apt-get install build-essential libxml2-dev libssl-dev libcurl4-openssl-dev
Copy after login

3. Compile and install PHP

Decompress the downloaded PHP source code package and enter the decompressed folder. Then execute the following command to compile and install:

./configure --with-fpm --enable-mbstring --with-openssl --enable-sockets
make
sudo make install
Copy after login

4. Configure PHP

After completing the installation of PHP, you need to make some configurations to ensure that Nginx can correctly parse PHP files. Edit the Nginx configuration file and add the following content:

server {
    listen 80;
    server_name your_domain.com;
    root /var/www/html;
    
    location ~ .php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Copy after login

5. Restart Nginx and PHP-FPM

After the configuration is completed, execute the following command to restart Nginx and PHP-FPM:

sudo systemctl restart nginx
sudo systemctl restart php-fpm
Copy after login

At this point, you have successfully set up a server environment for Nginx and PHP. You can enter your domain name into the browser to access the website, and if everything goes well, you should be able to see the effect of the PHP web page running normally. I hope this Nginx and PHP compilation and installation guide will be helpful to you!

The above is the detailed content of Nginx and PHP compilation and installation guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template