linux命令行如何安装php
2022-12-02 09:01:16
218
{{single.collect_count}}

linux命令行安装php的方法:首先通过“php -version”命令查看PHP的版本;然后使用命令“sudo apt-get install php5-cli php5-cgi”安装php依赖库即可。

Linux系统运维及项目正式上线:进入学习

推荐:《PHP视频教程

PHP在Linux Ubuntu中安装

相比Windows中略显繁琐的配置,在Ubuntu中几行命令就可以完成。

我们将同样构建PHP与Nginx结合的Web服务器环境。

2.1 下载并安装PHP

默认情况,Ubuntu中会自带PHP。

# 查看PHP的版本~ php -versionPHP 5.3.10-1ubuntu3.10 with Suhosin-Patch (cli) (built: Feb 28 2014 23:14:25)Copyright (c) 1997-2012 The PHP GroupZend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies# 安装PHP依赖库~ sudo apt-get install php5-cli php5-cgi
登录后复制

2.2 下载并安装Nginx

下载并安装nginx

~ sudo apt-get install nginx# 启动nginx~ sudo /etc/init.d/nginx start# 查看Nginx运行状态~ sudo /etc/init.d/nginx status * nginx is running# 查看Nginx进程~ ps -aux|grep nginxroot23060.00.0628601344 ?Ss 15:31 0:00 nginx: master process /usr/sbin/nginxwww-data23070.00.0632161916 ?S15:31 0:00 nginx: worker processwww-data23080.00.0632161656 ?S15:31 0:00 nginx: worker processwww-data23090.00.0632161916 ?S15:31 0:00 nginx: worker processwww-data23100.00.0632161656 ?S15:31 0:00 nginx: worker process
登录后复制

2.3 下载并安装spawn

spawn是一个FastCGI的应用,可伸缩地、高速地在HTTP server和动态脚本语言间通信的接口。

安装spawn-fcgi

~ sudo apt-get install spawn-fcgi
登录后复制

启动spawn-fcgi

~ sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -C 5 -p 9000 -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pidspawn-fcgi: child spawned successfully: PID: 2940# 查看进程~ ps -axu|grep cgiroot29400.00.0551966292 ?Ss 15:40 0:00 /usr/bin/php-cgiroot29410.00.0551962840 ?S15:40 0:00 /usr/bin/php-cgiroot29420.00.0551962840 ?S15:40 0:00 /usr/bin/php-cgiroot29430.00.0551962840 ?S15:40 0:00 /usr/bin/php-cgiroot29440.00.0551962840 ?S15:40 0:00 /usr/bin/php-cgiroot29450.00.0551962840 ?S15:40 0:00 /usr/bin/php-cgi
登录后复制

2.4 修改Nginx配置文件

PHP文件运行目录,/home/conan/php

设置访问域名,ubuntu.php.me

设置对.php文件,通过fastcgi转向127.0.0.1:9000解析

编辑文件:nginx.conf

~ sudo vi /etc/nginx/nginx.confhttp { # 忽略部分代码 server { set $htdocs /home/conan/php; listen 80; server_name ubuntu.php.me; location / { root $htdocs; autoindex on; index index.php index.html; } location ~ \.php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $htdocs$fastcgi_script_name; } }}
登录后复制

重启nginx服务器

~ sudo /etc/init.d/nginx restartRestarting nginx: nginx.
登录后复制

2.5 设置host

在host中把域名ubuntu.php.me映射为本机IP 127.0.0.1

~ sudo vi /etc/hosts127.0.0.1 ubuntu.php.me
登录后复制

用ping测试ubuntu.php.me

~ ping ubuntu.php.mePING ubuntu.php.me (127.0.0.1) 56(84) bytes of data.64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.040 ms64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.031 ms64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.067 ms
登录后复制

2.6 PHP测试文件

在目录 /home/conan/php 中,新建一个PHP的文件env.php

~ mkdir /home/conan/php~ vi /home/conan/php/env.php<?php phpinfo(); ?>
登录后复制

2.7 在浏览器中,查看PHP运行情况

在浏览器中打开HTTP地址:http://ubuntu.php.me/env.php

企业微信截图_15984062424716.png

注:在浏览器端的host文件中,设置ubuntu.php.me域名对应到IP的映射。

这样我们完成了PHP在Ubuntu中的安装和配置了!

以上就是linux命令行如何安装php的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}