Today I want to build a blog website by myself. Since I have been using Java development server and nginx, I have not built a PHP operating environment. So I decided to write this blog post to share with you all.
Let's first introduce the environment used: centos 7.4, PHP 7.0, nginx 1.12
Different system versions may have slightly different configurations, please pay attention when configuring.
The Linux system version can be viewed through the command: lsb_release -a. What the little brother wants to say here is that this command supports all Linux systems, whether you are using Red Hat or Debian, you can use it.
Let's get down to business now!
1. First check whether nginx has been installed successfully.
Linux command: find /|grep nginx.conf If the installation is complete, it will display the directory where your nginx configuration file is located. My directory here is in the /etc/nginx directory.
If you don't know how to install it, please Baidu by yourself. When I have time, I will also write a blog to introduce the installation and configuration of nginx, so I won't list it here.
2. Install PHP
I use the quick installation method here, using yum to install directly.
Execute the commands 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
Friends can also use the method of compiling and installing to install.
At this time, you can create a new php file in the html directory of nginx to check whether it can be used. Here I create a new file named: index.php with the following contents:
<?php
phpinfo();
?>
Enter the command to restart nginx nginx -s reload
At this time, you can enter your PHP test file through the browser for testing. What I entered here is: location/index.php
At this time, the problem was found. The browser did not load the PHP test file normally, but directly downloaded the file to the local. This is because nginx has no way to parse the file with the php suffix, so it sends it directly to you.
The problem is clear, so you need to configure the nginx server.
Open the configuration file of your nginx server: nginx.conf. Its path can be viewed by find /|grep nginx.conf.
In http{ },
find location / {
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 something after location / { }:
# PHP script requests are all forwarded to FastCGI for processing. Use FastCGI protocol default configuration.
# Fastcgi server and program (PHP, Python) communication protocol.
location ~ \.php$ {
# Set listening port
fastcgi_pass 127.0.0.1:9000;
# Settings nginx's default home page file (it has been set above and can be deleted)
fastcgi_index index.php;
# Set the path of the script file request
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Introduce the configuration file of fastcgi
include fastcgi_params;
}
Save after the configuration is complete, restart nginx, nginx -s reload
Then enter your test PHP file path (web access path) in the browser, and you will find that you are done! The PHP file is already working fine.
If your server still can't load PHP normally, prompts a web page error or downloads directly, then check whether your Linux port 9000 is open or occupied.
View port: netstat -antp
When there is no 9000 port in reality, it means that the 9000 port is not occupied. Use php-cgi -b 127.0.0.1:9000 & to open the PHP 9000 port, where & is running in the background, don't forget to add it!
When port 9000 appears, you need to check which program is occupied, confirm that the occupied program can be stopped and then kill the process, kill PID
For example here: kill 5611
Then execute php-cgi -b 127.0.0.1:9000 & open PHP 9000 port.
One more thing to add here, maybe some small partners will encounter such an error when restarting the nginx server:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)