Home  >  Article  >  Backend Development  >  How to set up nginx php environment

How to set up nginx php environment

PHPz
PHPzOriginal
2023-03-31 09:05:362867browse

In modern web development, it is very important to build a fast and efficient server environment. Currently, Nginx and PHP are two software widely used in web development. Moreover, when these two software are used together, they can increase the processing speed and stability of the server without losing performance, allowing users to access faster and smoother websites.

This article will lead you to understand how to set up an Nginx PHP environment.

1. Install Nginx

Nginx is a high-performance web server and reverse proxy server. Use it to increase the efficiency of your web server, thereby speeding up web access. There are two main ways to install Nginx: download the source code, compile and install it from the official website, and install it through the system package manager. The following is how to install using the package manager in Linux systems:

1.1 Install Nginx in Ubuntu/Debian

sudo apt-get update
sudo apt-get install nginx

1.2 Install Nginx in CentOS/Fedora

sudo yum install nginx

2. Install PHP

PHP is a scripting language that is widely used on the Web In the process of development. PHP can quickly and easily generate dynamic web content and is an important part of web development. The method of installing PHP is similar to installing Nginx, and can also be achieved through the system package manager. The following is how to install using the package manager in Linux systems:

2.1 Install PHP in Ubuntu/Debian

sudo apt-get install php-fpm php-mysql php-xml php- mbstring

2.2 Install PHP in CentOS/Fedora

sudo yum install php-fpm php-mysql php-xml php-mbstring

3. Configure Nginx and PHP

After Nginx and PHP are installed, they need to be configured to maximize their performance. The following are the specific steps:

3.1 Configure Nginx

In Ubuntu/Debian, edit the /etc/nginx/sites-available/default file through the following steps:

sudo nano /etc/nginx/sites-available/default

In the editor, find the location part in the server paragraph and set the root parameter to the PHP file location (for example/var/www/html/):

location / {

root /var/www/html/;
index index.php;
try_files $uri $uri/ /index.php?$args;

}

In CentOS / Fedora, edit the /etc/nginx/conf.d/default.conf file as follows:

sudo nano /etc/nginx/conf.d/default.conf

Same as in Ubuntu/Debian, find the location/{} section in the editor and set the root parameter to the PHP file location (for example /var /www/html/):

location / {

root /var/www/html/;
index index.php;
try_files $uri $uri/ /index.php?$args;

}

3.2 Configure PHP

In Ubuntu/Debian, edit / etc/php/7.0/fpm/pool.d/www.conf file:

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

in editor , find the following two lines and modify them as follows:

listen = /var/run/php/php7.0-fpm.sock
listen.owner = www-data
listen.group = www -data
listen.mode = 0666

In CentOS/Fedora, edit the /etc/php-fpm.d/www.conf file as follows:

sudo nano /etc /php-fpm.d/www.conf

The same as in Ubuntu/Debian, find the following two lines in the editor and modify them as follows:

listen = /var/run/php/ php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

4. Start Nginx and PHP

through the above configuration , the combination of Nginx and PHP is ready, now you need to start them. If it is Ubuntu/Debian, execute the following command:

sudo systemctl start nginx
sudo systemctl start php7.0-fpm

If it is CentOS/Fedora, execute the following command:

sudo systemctl start nginx
sudo systemctl start php-fpm

If no error is reported in the interface, congratulations, you have successfully set up the Nginx PHP server environment.

5. Test your environment

Enter "http://server ip" in the browser. If you see the "Welcome to nginx!" page, it means that Nginx has been successfully installed. ; Then, place a correctly formatted PHP file in the root directory, and enter "http://server ip/yourfile.php" in the browser. If the PHP output is displayed correctly, it means that the PHP module is installed successfully and the server The environment is set up.

Conclusion

Through this article, I believe you have understood how to install and configure Nginx and PHP environments on Linux systems, and complete testing of related structures. If you need more detailed information, please refer to the official documentation. At the same time, this is also an important step for you to maximize the effectiveness of your server in the future. keep learning!

The above is the detailed content of How to set up nginx php environment. 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