A detailed method of installing PHP under Linux and configuring it in an Nginx server

Source: Internet
Author: User
Tags fpm install php php script nginx server

First introduce the environment used: CentOS 7.4, PHP 7.0, Nginx 1.12

The Linux system version can be viewed via the command: Lsb_release-a.

Now it's time to get to the chase!

1. First check to see if Nginx has been installed successfully.

Linux command: Find/|grep nginx.conf If the installation is complete, it will show the directory where your nginx configuration file is located, and my directory here is under the/etc/nginx directory.

Do not install the small partners please Baidu, there is time I will also write a blog specifically about the installation and configuration of Nginx, and so on, not listed here.

2. Install PHP

I use the quick installation method here, using Yum to install directly.

Execute the command separately:

# RPM-UVH https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# RPM-UVH https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# yum Install Php70w-common php70w-fpm php70w-opcache php70w-gd php70w-mysqlnd php70w-mbstring Php70w-pecl-redis Php70w-pecl-memcached Php70w-devel

# systemctl Restart httpd

Small partners can also be installed using the method of compiling the installation.

In this time, you can create a new PHP file in the Nginx HTML directory to detect whether it can be used. Here I create a new file named: index.php, which reads as follows:

<?php

Phpinfo ();

?>

Input command restart Nginx nginx-s Reload

This time you can enter your PHP test file through the browser to test, I entered here is: location/index.php

When the problem is found, the browser does not load the PHP test file, but directly download the file to local. This is because Nginx has no way to parse the PHP suffix file, and then send it directly to you.

Clear the problem, so you need to configure the Nginx server.

Open the configuration file for your Nginx server: nginx.conf. Its path can be viewed through the Find/|grep nginx.conf.

In http{},

Location to find/{

root HTML;
Index index.html index.htm;
Add a sentence after index index.php

The changed content is:

Location/{

root HTML;
Index index.php index.html index.htm;
}

Then add the content after location/{}:

# PHP script request all forwarded to fastcgi processing. Use the FASTCGI protocol default configuration.
# FASTCGI Server and program (Php,python) communication protocol.
Location ~ \.php$ {
# Set the Listening port
Fastcgi_pass 127.0.0.1:9000;
# Set Nginx default home page file (above already set, can delete)
Fastcgi_index index.php;
# Set the path of the script file request
Fastcgi_param script_filename $document _root$fastcgi_script_name;
# Introduction of fastcgi configuration files
Include Fastcgi_params;
}

Save after configuration is complete, restart Nginx,nginx-s reload

Then this is to enter your test PHP file path (Web Access path) in the browser and you will find it done! The PHP file is ready to run properly.

If your server is still unable to load PHP properly, prompt for page errors or download directly, then check whether your Linux open Port 9000 or is occupied.

View Port: NETSTAT-ANTP

When the reality does not have 9000 ports, it is now 9000 port is not occupied, using Php-cgi-b 127.0.0.1:9000 & open PHP 9000 port, where & is running in the background, do not forget to add!

When there are 9000 ports appearing, you need to see which program is occupied, confirm that the seizure program can stop after the kill process, kill PID

For example here is: Kill 5611

Then execute php-cgi-b 127.0.0.1:9000 & open PHP 9000 port.

To add, it is possible that the use of php-cgi may not be accessible after a certain period of time, so it is recommended to use PHP-FPM to start PHP.

One more thing to add here is that perhaps some of the small partners will encounter this error when restarting the Nginx server:

Nginx: [ERROR] Open () "/usr/local/nginx/logs/nginx.pid" failed (2:no such file or directory)

Workaround:

[Email protected] nginx]#/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf

This is because, Nginx.pid is lost, so use nginx-c parameters to specify the location of the nginx.conf file, and then restart Nginx!

A detailed method of installing PHP under Linux and configuring it in an Nginx server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.