Home  >  Article  >  Backend Development  >  Teach you step by step how to compile and install Nginx and PHP

Teach you step by step how to compile and install Nginx and PHP

WBOY
WBOYOriginal
2024-02-27 12:18:04865browse

Teach you step by step how to compile and install Nginx and PHP

Compiling and installing Nginx and PHP is a common way, which can achieve more flexible and customized configurations. The following will explain step by step how to compile and install Nginx and PHP. At the same time Provide specific code examples.

First, we need to prepare a server with a Linux system installed. This article uses CentOS 7 as an example for demonstration.

Step 1: Prepare the environment

Before starting the installation, we need to ensure that the server has installed some necessary software and tools, such as gcc, make, openssl-devel , pcre-devel, zlib-devel, etc. You can install it through the following command:

sudo yum install gcc make openssl-devel pcre-devel zlib-devel

Step 2: Compile and install Nginx

  1. Download the Nginx source package and decompress it:
wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar -zxvf nginx-1.20.0.tar.gz
cd nginx-1.20.0
  1. Configure compilation parameters:
./configure --prefix=/usr/local/nginx --with-http_ssl_module
  1. Compile and install Nginx:
make
make install
  1. Start Nginx service:
/usr/local/nginx/sbin/nginx

Now, you can access the server's IP address through the browser. If you see the Nginx welcome page, the installation is successful.

Step 3: Compile and install PHP

  1. Download the PHP source code package and unzip it:
wget https://www.php.net/distributions/php-7.4.21.tar.gz
tar -zxvf php-7.4.21.tar.gz
cd php-7.4.21
  1. Configure and compile Parameters:
./configure --prefix=/usr/local/php --with-curl --with-fpm --with-mysqli --with-mbstring
  1. Compile and install PHP:
make
make install
  1. Configure PHP-FPM:
cp sapi/fpm/php-fpm.service /etc/systemd/system/
systemctl enable php-fpm
systemctl start php-fpm

Step 4: Configure Nginx to support PHP

  1. Edit Nginx configuration file:
vim /usr/local/nginx/conf/nginx.conf

Add the following content in the server block to Support PHP:

location ~ .php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
  1. Restart Nginx service:
/usr/local/nginx/sbin/nginx -s reload

Now, you can create a php file in the Nginx Web directory and access the file, if it can be displayed normally PHP page, indicating that the installation is successful.

Through the above steps, we successfully completed the compilation and installation of Nginx and PHP, and configured them to work normally. I hope this article can help readers in need, so that everyone can have a deeper understanding and mastery of compilation and installation methods.

The above is the detailed content of Teach you step by step how to compile and install Nginx and PHP. 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