linux命令行如何安装php

互联网 20-8-26

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

推荐:《PHP视频教程》

PHP在Linux Ubuntu中安装

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

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

2.1 下载并安装PHP

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

# 查看PHP的版本 ~ php -version PHP 5.3.10-1ubuntu3.10 with Suhosin-Patch (cli) (built: Feb 28 2014 23:14:25) Copyright (c) 1997-2012 The PHP Group Zend 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 nginx root      2306  0.0  0.0  62860  1344 ?        Ss   15:31   0:00 nginx: master process /usr/sbin/nginx www-data  2307  0.0  0.0  63216  1916 ?        S    15:31   0:00 nginx: worker process www-data  2308  0.0  0.0  63216  1656 ?        S    15:31   0:00 nginx: worker process www-data  2309  0.0  0.0  63216  1916 ?        S    15:31   0:00 nginx: worker process www-data  2310  0.0  0.0  63216  1656 ?        S    15: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.pid spawn-fcgi: child spawned successfully: PID: 2940 # 查看进程 ~ ps -axu|grep cgi root      2940  0.0  0.0  55196  6292 ?        Ss   15:40   0:00 /usr/bin/php-cgi root      2941  0.0  0.0  55196  2840 ?        S    15:40   0:00 /usr/bin/php-cgi root      2942  0.0  0.0  55196  2840 ?        S    15:40   0:00 /usr/bin/php-cgi root      2943  0.0  0.0  55196  2840 ?        S    15:40   0:00 /usr/bin/php-cgi root      2944  0.0  0.0  55196  2840 ?        S    15:40   0:00 /usr/bin/php-cgi root      2945  0.0  0.0  55196  2840 ?        S    15: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.conf http {    # 忽略部分代码    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 restart Restarting nginx: nginx.

2.5 设置host

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

~ sudo vi /etc/hosts 127.0.0.1       ubuntu.php.me

用ping测试ubuntu.php.me

~ ping ubuntu.php.me PING 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 ms 64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.031 ms 64 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

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

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

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

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: linux
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:php.exe 无法启动怎么办

相关资讯