PHP page generation speed

To measure the speed of page generation in PHP, insert the following code at the beginning of the script:

// Breathing start time of page generation
$start_array = explode(" ",microtime());$start_time = $start_array[1] + $start_array[0];

And at the end of the script we write this:

// Calculating the speed of page generation
$end_array = explode(" ",microtime());$time = $end_array[1] + $end_array[0] - $start_time;
printf("Generated in %f sec.",$time);

Why in the script?
To minimize the time for calculation of page generation in PHP, it is better to insert the code in the main script to minimize the accesses to the hard disk, and thus reduce the loss of time.

Install Nginx, PHP, MySQL (Mariadb) on Ubuntu

-Would you be kind to me in which direction to go?
-In the famous you, – replied the cat.
-I don’t know it.
-So, in the unknown. In any case, it is known that in a known time you will find yourself therrre or herrre,-purring cat.

Nginx Installation:

apt update
apt install nginx

Installing PHP and the MariaDB driver

apt install php-fpm php-mysqlnd php-xml

Creating Virtual Domains

mkdir /var/www/domain.com
chmod -R 755 /var/www/domain.com

In the /etc/nginx/sites-available directory, we create a domain.com.conf file

The contents of the domain.com.conf file

server {
     listen 80;
     server_name domain.com;

     root /var/www/domain.com;

     # Add index.php to the list if you are using PHP
     index index.php index.html index.htm index.nginx-debian.html;
 
     server_name domain.com;
 
    location / {
               try_files $uri $uri/ =404;
               if (!-e $request_filename) {
                  rewrite ^.+/?(/wp-.*) $1 last;
            rewrite ^.+/?(/.*\.php)$ $1 last;
                  rewrite ^(.+)$ /index.php?q=$1 last;
      }
}

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

Now you need to reboot the Webserver

service nginx restart

Checking PHP work

In /var/www/domain.com , we will create the index.php file and write in it:

<?php phpinfo(); ?>

Next, open your website in the browser, in the example we use domain.com and if everything is done correctly, we see this picture:

Install MARIADB

sudo apt install mariadb-server mariadb-client

Security settings

mysql_secure_installation

Answer the questions:

Create a database and a user.

Entering the MARIADB:

mysql -u root

If you set the password to root:

mysql -u root -p
CREATE DATABASE domaindb;
CREATE USER 'nameus'@'localhost' IDENTIFIED BY 'user_password';

For the user_password place, enter the user password.

GRANT ALL ON domaindb. * to nameus @ localhost;
Quit

Deploying WordPress

cd /var/www/domain.com
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
mv wordpress/* .
rmdir wordpress
chown -R www-data:www-data /var/www/domain.com
chmod -R 755 /var/www/domain.com

Intercept require output in PHP or execute PHP in file_get_contents

… So we’re introducing a plan-intercept. Only immediately prepare a report that the plan-interception no results.
The Lord of the Rings: the Two Towers

In order to load the contents of a file and display it in a certain place of a Web page, you can use file_get_contents, but if the file loaded in this way will contain PHP code, it is not processed.
For the PHP code to execute in a dynamically loaded file, you can use the command require, but it outputs the result instantly, and if we pre-generate the output into a variable, this method is also not suitable… Or does it fit?

In PHP there is a set of commands to buffer output to enable the output buffer by using the ob_start() command. After that, we call require and collect the result in the variable with the command ob_get_clean();

function GetPHP($file_name){
    ob_start();
    require( $file_name );
    return ob_get_clean();
}

Here is a function that returns the result of executing a PHP script in a variable.

echo GetPHP('some-file.php');

Deploying Nginx + PHP on the Ubuntu server

NGinx-is a quick and easy Web-server, to install it you need to enter several commands in the terminal:

sudo apt update
sudo apt install nginx php-fpm

Check the health of the Web server:

systemctl status nginx

In this case, change the file /etc/nginx/sites-available/default

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html;

	index index.html index.htm index.php;

	server_name _;

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

	location ~ \.php$ {
		try_files $uri = 404;
		include /etc/nginx/fastcgi.conf;
		fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}
}

Next, reboot Nginx

nginx -s reload

 

AJAX PHP JQUERY Transfer POST Request

In order to send a POST request to PHP script via JQUERY AJAX, create 2 files:

jqtest.htm – in which will be the request form and JavaScript code to generate and send the request;

jqtest.php – for receiving and sending the answer.

Listing of the file jqtest.htm:

<div>
<label for="request">Request:  </label>
<input id="request">
<input type="submit" value="Send" onclick="send();">
</div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
  function send()
  {
    jQuery.ajax({
      dataType: "json",
      method: "POST",
      url: "jqtest.php",
      data: { msg: jQuery('#request').val()}
    }).done(function( msg ) {
      alert(msg['answer']);
    });
  }
</script>

Listing of the file jqtest.php:

<?php
$q = ($_POST['msg']);
$ans= array(
  'answer' => 'You send: '.$q,
);
echo json_encode($ans);
?>

JQUERY query passes the MSG key in JSON format, and PHP code generates a JSON response and returns it in the answer key.

Cross-Origin Read Blocking (CORB)-Resolved

Each... You can listen, but not everyone is worth talking to.
Arthur Schendhauer

When you try to send a jQuery JSON request from another domain, you receive an error:

Access to XMLHttpRequest at ‘ http://* ‘ from Origin ‘ http://* ‘ has been blocked by CORS policy: No ‘ Access-Control-Allow-Origin ‘ header is present on the requested resource.

jquery. js? ver = *: 4 Cross-Origin Read Blocking (CORB) blocked cross-origin response * with MIME type application/JSON. See https://www.chromestatus.com/feature/5629709824032768 for more details. 

This means that sharing resources between different sources (CORS) is not allowed.

In order to allow CORS, it is necessary at the beginning of the PHP script, which processes the request, to add the generation of server response headers indicating that CORS is allowed.

<?php
switch ($ _server['HTTP_ORIGIN']) {
  Case 'http://remote.domain': Case 'https://remote.domain':
  Header ('Access-Control-Allow-Origin: '. $_server['HTTP_ORIGIN']);
  Header ('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  Header ('Access-Control-Max-Age: 1000');
  Header ('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  break;
}
?>
  • http://remote.domain-the URL of the site from which the request may occur.

On the client side, you need to specify crossDomain: true:

<script>
jQuery.ajax({
  crossDomain: true,
  dataType: "json",
  method: "POST",
  url: "http://main.domain/script.php",
  data: { key: "some text" }
  }).done(function( msg ) {
  // answer
});
</script>

So, to pass a request from one site to another, you need to specify the server headers that allow requests from a certain remote domain in the receiving site. And on the remote site, write a key that indicates that this query will work between different domains.