Difference between revisions of "PHP"

From ElectroDragon
Jump to navigation Jump to search
(php7.3)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Install ==
 
== Install ==
 
* '''sudo apt-get update''', '''sudo apt-get upgrade'''
 
* '''sudo apt-get update''', '''sudo apt-get upgrade'''
 +
=== php7.0 ===
 +
* echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.0.list
 +
* apt-get install php7.0-fpm nginx
 +
* sudo apt-get install php7.0-{bcmath,bz2,intl,gd,mbstring,mysql,zip}
 
=== php7.3 ===
 
=== php7.3 ===
 
correct method
 
correct method
Line 68: Line 72:
 
  request_slowlog_timeout = 10s
 
  request_slowlog_timeout = 10s
  
== PHP Configure ==
+
== PHP Configure (New) ==
 
====  use this latest config ====
 
====  use this latest config ====
 
* File at /etc/nginx/sites-available/default
 
* File at /etc/nginx/sites-available/default
 +
* For PHP7.3 change unix:/var/run/php5-fpm.sock --> unix:/var/run/php/php7.3-fpm.sock
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
server {
 
server {
Line 96: Line 101:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Old ====
+
== PHP Configure (Old) ==
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
server {
 
server {

Latest revision as of 01:45, 2 December 2019

Install

  • sudo apt-get update, sudo apt-get upgrade

php7.0

  • echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.0.list
  • apt-get install php7.0-fpm nginx
  • sudo apt-get install php7.0-{bcmath,bz2,intl,gd,mbstring,mysql,zip}

php7.3

correct method

  • sudo apt install lsb-release apt-transport-https ca-certificates
  • sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
  • echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.3.list
  • sudo apt update
  • sudo apt-get install php7.3 php-pear
  • Install addtions - sudo apt-get install php7.3-{bcmath,bz2,intl,gd,mbstring,mysql,zip}

Install Nginx + php7.3 together

  • apt-get install nginx php7.3-fpm

or

  • sudo apt-get install php7.3 php-pear php7.3-fpm php7.3-mysql


other wrong method

  • apt-get install php7.0 php7.0-fpm php-fpm

Restart service

  • service php7.0-fpm restart

php5

  • apt-get install php5-common libapache2-mod-php5 php5-cli
  • sudo apt-get install php5-fpm php5-mysql
  • sudo nano /etc/php5/fpm/php.ini -> cgi.fix_pathinfo=0

or

  • apt-get install php5-cgi php5-cli php5-mysql
  • apt-get install php5-fpm php5-mysql

Restart service

  • service php5-fpm restart
  • sudo systemctl restart php5-fpm

Test Installation

Confirm installation

  • php -v
  • Create PHP test file
sudo nano /usr/share/nginx/html/info.php
nano /var/www/html/info.php
<?php
phpinfo();
?>

PHP

  • Edit sources.list due to licensing issues
  • /etc/apt/sources.list -> add contrib non-free at end of each line
...
deb http://mirrors.digitalocean.com/debian jessie main contrib non-free
deb-src http://mirrors.digitalocean.com/debian jessie main contrib non-free

deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

# jessie-updates, previously known as 'volatile'
deb http://mirrors.digitalocean.com/debian jessie-updates main contrib non-free
deb-src http://mirrors.digitalocean.com/debian jessie-updates main contrib non-free


PHP Debug

  • Debug -> vim /etc/php5/fpm/pool.d/www.confMake necessary changes to match following values:
slowlog = /var/log/php5/slow.log
request_slowlog_timeout = 10s

PHP Configure (New)

use this latest config

  • File at /etc/nginx/sites-available/default
  • For PHP7.3 change unix:/var/run/php5-fpm.sock --> unix:/var/run/php/php7.3-fpm.sock
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name your_server_ip;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

PHP Configure (Old)

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Let's copy the follow code into the default to enable php, compare to the code at Nginx on pcduino, it's php-enabled, you can read it on this part "location ~ \.php$".

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

And now when we navigate to 127.0.0.1/index.php (or info.php), we will see the info like below:
Phpinfo.png

Configuration

We're going to start moving about in Linux now. Remember that Linux still has a folder structure - just like any graphical operating system. If you want to learn more about the folder structure of a Linux system - there are some great tutorials and images that can be found through a quick Google search.

Let's move to where nginx stores the sites which it hosts, by using the command:

cd /etc/nginx/sites-enabled 

or try at /etc/nginx/sites-available

And let's edit the file "default", delete everything in it, and copy the following code into it, you can do this with editor like nano or GUI editor:

server { 
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /usr/share/www/example;
index index.php index.html index.htm;

location / {
error_page 404 = 404.html;
}

# Make site accessible from http://localhost/
server_name example.com *.example.com;

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
}

Build the website

Now, we're actually going to make a little website in the /usr/share/nginx/www/example folder we made earlier.

Let's move to the folder by using the command:

cd /usr/share/nginx/www/example 

We're going to make two files - an index page (homepage), and a 404 page (a page to display if the page a user is trying to access doesn't exist).

Let's first make the index page by using the command:

pico index.html 

If you're familiar with html, at this point type in whatever you want. If you're not, then paste in the following, save and exit (as you did earlier with the nginx configuration file):

<html>
Hello world, this is my first website!
</html> 

Next, do the same for the 404 page by using the command:

pico 404.html 

Paste in the following if unsure:

<html>
Sorry, the page you are looking for doesn't exist!
</html> 

The final step is to make sure that our pages are readable for others by changing the permissions on the files. If we didn't change the permissions, someone accessing your website may be given the 'You are not allowed to view this page' (or equivalent) error generated by nginx.

Use the command:

chmod -R 775 /usr/share/nginx/www/example

Detailed Configuring nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

vi /etc/nginx/nginx.conf

The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here: http://wiki.nginx.org/NginxFullExample2)

First (this is optional) adjust the number of worker processes and set the keepalive_timeout to a reasonable value:

[...]
worker_processes  4;
[...]
    keepalive_timeout   2;
[...]

Advanced

  • basedir
#防跨站设置
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/data/tmp/php/upload/:/proc/:/";
  • rewrite root to /k, add line in location / { }
#return 301 /k;

Docker