Connect SSH to PHP to ensure data transmission security

Source: Internet
Author: User
Tags php cli
SSH can transmit data by encrypting the online packets. SSH can be used to encrypt all transmitted data, and no useful information can be obtained even if someone intercepts the data. At the same time, data is compressed, which greatly speeds up transmission. In short, the use of SSH ensures that SSH can transmit data through the online packet encryption technology. SSH can be used to encrypt all transmitted data, even if someone intercepts the data, they cannot obtain useful information. At the same time, data is compressed, which greatly speeds up transmission. In short, the use of SSH ensures that data transmission is secure and the transmission efficiency is high.

However, not everyone knows the features that PHP can connect to SSH and the ability to execute remote commands, but this is very useful. Since PHP can be used in many different ways, it has many configuration options to control its behavior. A large set of optional parameters can ensure that you can use PHP for many different purposes, but it also means that the combination of these parameters and server configuration will bring some security issues. I have been using SSH in php cli applications. I used it from cronjobs, but it was not very simple at the beginning. it can be said that it was a great deal of money. The manual on secure use of the Shell2 function is not very practical either. I have conducted many tests before having published this article. I hope you can save some time for configuring PHP after reading this article.

In this article, I need to assume that:

The operating system you are running is Debian/Ubuntu. If you are not running Debian/Ubuntu, you may need to replace the corresponding content in this article with the data package manager provided by your Linux release.

You are running PHP5. if you are not running PHP5, use PHP4 instead.

You have a basic understanding of PHP and server management.

You have installed PHP.

Prerequisites

Installation package

First, let's install the following package:

  

sudo aptitude update
  sudo aptitude install php5-dev php5-cli php-pear buid-essential \
  openssl-dev zlib1g-dev

After the installation is complete, go to the next step.

Compile libssh2

After downloading Libssh2 from the sourceforge website, we need to compile it, but don't worry, you just need to follow the steps below:

  

cd /usr/src
  wget surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  tar -zxvf libssh2-0.14.tar.gz
  cd libssh2-0.14/
  ./configure
  make all install

If you want to check whether a new version is available, you can check SF. NET. However, 0.14 is enough.

Install

Install ssh2.so

Next, we need to link libssh and PHPr. There is a PECL module to complete this function. We can use PEAR to install it.

Pear install-f ssh2

The-f parameter ensures that SSH2 is installed, even if there is no stable selection object. You can also use the following package name: ssh2-beta to forcibly run.

Now you need to make sure that our new SSH2.SO module is loaded by PHP. Edit your php. ini file (for CLI utility:/etc/php5/cli/php. ini, for Apache utility:/etc/php5/apache2/php. ini)

Extension = ssh2.so

This should be placed under "Dynamic Extensions", which is about 515th rows.

PHP supports coding through SSH

You have just enabled SSH2 in PHP. So how should we use it now? There are two options. SSH support:

1. execution method:

This tells the operating system of your server to execute something and transmits it back to your script through the pipeline.

2. Shell method:

This method opens an actual shell in the operating system, just as it is operated when logging on through the terminal application. Some routers do not have a fully POSIX consistency implementation process. Instead, they run their own applications immediately upon logon. In this case, you need this method.

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.