配置PHP远程执行命令

博客介绍了CentOS安装PHP环境的步骤,包括更新升级、安装支持库、安装ssh2扩展、关闭防火墙等,还给出了相关命令。同时提到了PHP语言中文参考和CentOS下载地址,最后涉及PHP中使用ssh2远程执行Linux命令代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、官方主站:

https://www.php.net(PHP官方网站)

参考:

https://www.centos.org/download/(CentOS下载地址)

二、PHP语言中文参考:

https://www.php.net/manual/zh/

参考:

https://www.runoob.com/ (HTML参考手册)

PHP入门
http://www.w3school.com.cn/index.html

三、CentOS安装PHP环境步骤:

0.有时候需要删除:yum.pid才能更新升级:
rm -f /var/run/yum.pid
1.更新升级:
yum -y update
2.安装必要的支持库(Apache会自动安装):
yum install gcc libcap libpcap libpcap-devel screen php dstat cmake gmp gmp-devel gengetopt byacc flex git json-c zmap iptraf nano
3.验证安装:
yum groupinstall "Development Tools"
4.安装ssh2扩展(安装支持的库文件):------------------------------------------------------------------------
yum install gcc php-devel php-pear libssh2 libssh2-devel

pecl install -f ssh2 //(建立ssh2扩展:之后会显示安装的日志,需要选择时直接按回车键就好)

touch /etc/php.d/ssh2.ini //(修改ssh2.ini)

echo extension=ssh2.so > /etc/php.d/ssh2.ini //(可以用vim打开/etc/php.d/ssh2.ini 确认是否已经包含extension=ssh2.so)

php -m | grep ssh2 //(验证是否安装成功:显示ssh2则表示安装成功)

5.关闭防火墙:
systemctl stop firewalld.service

参考命令:

systemctl status firewalld.service //(查看状态)

6.设置允许httpd访问网络:
setsebool -P httpd_can_network_connect 1 //(允许反向代理)

setsebool -P nis_enabled 1 //(关闭selinux)

getsebool -a | grep httpd_can_network_connect //(检查网络访问的配置:)

//(结果如下:httpd_can_network_connect --> on)

7.安装ssh(一般默认CentOS已经安装了OpenSSH,即使你是最小化安装也是如此。)

参考命令:

yum install openssh-server //(安装ssh)

systemctl start sshd.service //(启动ssh的服务)

systemctl enable sshd.service //(设置开机自动启动ssh服务)

rpm -qa | grep ssh //(检查是否安装ssh)

service sshd restart //重启SSH服务

service sshd start //启动服务

service sshd stop //停止服务

netstat -antp | grep sshd //查看是否启动22端口

putty下载地址

8.重新启动web服务:
service httpd restart #重启

参考命令:

systemctl start httpd.service #启动

systemctl stop httpd.service #停止

systemctl restart httpd.service #重启

第二、设置开机启动/关闭

systemctl enable httpd.service #开机启动

systemctl disable httpd.service #开机不启动

第三、检查httpd状态

systemctl status httpd.service

网站文件夹:

/var/www/html

然后把你的PHP文件上传到html文件夹里去浏览器中输入主机IP就可以了!

四、PHP中使用ssh2远程执行Linux命令代码:

<?php
	ini_set("display_errors","On");

	error_reporting(E_ALL);

	if (!function_exists('ssh2_connect'))
	{	
        die("Install ssh2 module.\n");
	}
	$conn = ssh2_connect('192.168.16.118',22);
	if (!$conn) die('Connection failed');

	if (!ssh2_auth_password($conn,'root','root'))
		 throw new Exception("Error with login credentials");

	$stream=ssh2_exec($conn, "ls /");

	stream_set_blocking($stream, true);

	$output = stream_get_contents($stream);

	fclose($stream);

	echo $output ;

 	die('end.');
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值