Home  >  Article  >  Backend Development  >  How to install php from linux command line

How to install php from linux command line

藏色散人
藏色散人Original
2020-08-26 09:46:402178browse

How to install PHP from the Linux command line: First check the PHP version through the "php -version" command; then use the command "sudo apt-get install php5-cli php5-cgi" to install the PHP dependent library.

How to install php from linux command line

Recommended: "PHP Video Tutorial"

PHP Installation in Linux Ubuntu

Compared with the slightly cumbersome configuration in Windows, it can be completed in Ubuntu with just a few lines of commands.

We will also build a web server environment that combines PHP and Nginx.

2.1 Download and install PHP

By default, Ubuntu will come with 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 Download and install Nginx

Download and install 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 Download and install spawn

spawn is a FastCGI application, scalable , an interface for high-speed communication between HTTP servers and dynamic scripting languages.

Install spawn-fcgi

~ sudo apt-get install spawn-fcgi

Start 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 Modify Nginx configuration file

PHP file running directory, /home/conan/php

Set the access domain name, ubuntu.php.me

Set the .php file, and forward it to 127.0.0.1:9000 for analysis through fastcgi

Edit the file: 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;
       }
   }
}

Restart nginx server

~ sudo /etc/init.d/nginx restart
Restarting nginx: nginx.

2.5 Set host

Map the domain name ubuntu.php.me in the host to the local IP 127.0.0.1

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

Use Ping test 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 test file

In the directory /home/conan/php, create a new PHP file env.php

~ mkdir /home/conan/php
~ vi /home/conan/php/env.php
<?php phpinfo(); ?>

2.7 In the browser, check the running status of PHP

Open the HTTP address in the browser: http://ubuntu.php.me/env.php

How to install php from linux command line

Note: In the host file on the browser side, set the mapping of the ubuntu.php.me domain name to the IP.

In this way we have completed the installation and configuration of PHP in Ubuntu!

The above is the detailed content of How to install php from linux command line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn