In this section we teach you how to install php and configure php
1. Install php
PHP actually contains two parts: php and php-fpm. If you only execute apt-get install php, apache will be installed by default. Therefore, our command needs to install only php. We only need to ensure that the version numbers of php and php-fpm are consistent. We use php7.2 version here, the command is as follows:
apt-getinstallphp7.2php7.2-fpm
Then enter y according to the prompt to confirm
After a while, the installation of php will be completed. After that, we execute the php -v command to view the installed version number information, as shown in the figure:
As shown in the picture above, it means that we have successfully installed php.
2. PHP configuration
1. First open the default configuration file under /etc/nginx/sites-available/
cd/etc/nginx/sites-available/videfault
Find the location ~.php module in the configuration file, as follows
First uncomment the first and last lines of location ~.php, and then modify
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
In this sentence, change 7.0 to 7.2 and uncomment it.
And add three sentences at the end:
fastcgi_split_path_info^(.+.php)(/.*)$;includefastcgi_params;fastcgi_indexindex.php;
The modified version is as follows:
2. Modify the /etc/php/7.2/fpm/pool.d/www.conf file, open it with the vi command, and find the sentence listen = /run/php/php7.2-fpm.sock
Then change it to listen=/var/run/php/php7.2-fpm.sock, as follows:
3. Modify the /etc/php/7.2/fpm/php.ini file
After finding and opening it, find the sentence cgi .fix_pathinfo=1, uncomment it, and change 1 to 0. The final result is as follows:
4. Modify the /etc/nginx/fastcgi_params file
After opening, append the following content at the end:
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
3. nginx and php integration test
Create the test.php file in the /var/www/html directory and write the following code:
<?phpechophpinfo();
After saving, we enter the browser and open 127.0.0.1/test.php to see the php page, as follows:
If it does not take effect, you can execute nginx -s reload to reload the configuration file or service nginx restart to restart nginx to take effect.