It is quite common to use PHP when developing web applications. Installing the php environment in a Unix environment allows us to develop and test more conveniently. From source code installation to system package managers, this article will take you through the different methods of installing php on Unix systems.
Most Unix distributions provide a package manager that makes it easy to install and upgrade software packages. Installing php using a package manager is also a very simple method.
Debian/Ubuntu systems:
Using the apt-get package manager, you can install PHP using the following command:
sudo apt-get update
sudo apt-get install php
CentOS / Fedora system:
Using the yum package manager, you can use the following command to install PHP:
sudo yum install php
Note that this method installs the preconfigured PHP version of the system, which may not be the latest PHP version, so if you need to run the latest PHP application, you need to use other installation methods.
If you use source code to install the PHP environment, you need to download the latest PHP source code package and compile and install it. The following are the steps to install using source code:
Necessary dependencies
First you need to install dependent libraries, including the updated gcc compiler, as well as standard C libraries and development header files. Taking the Ubuntu system as an example, you can use the following command:
sudo apt-get install gcc libpcre3-dev
CentOS or Fedora systems may require different commands. Please note that compiling on a standalone system requires installation of dependencies.
Download source code
The new version of PHP code can be downloaded from the official website and can be downloaded through the following command:
cd /usr/local/src
sudo wget https://www.php.net/distributions/php-8.0.3.tar.gz
sudo tar -xzf php-8.0.3.tar.gz
cd php-8.0.3
Configuration, compilation and installation
Execute the following command in the source code directory to configure:
sudo ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-mysql --with-zlib
sudo make
sudo make install
here , the --prefix parameter will define the PHP installation path. The --with-apxs2 parameter compiles the Apache Portable Runtime (APR) and Apache XML/XSLT libraries into PHP binaries. --with-mysql and --with-zlib will compile with MySQL and zlib support.
Configure PHP
Installation configuration file:
cd /usr/local/src
sudo cp php-8.0.3/php.ini-production /usr/local/php/lib/php.ini
And configure the php.ini file, select the appropriate options for the configuration of the web server and PHP
Use Docker containers to quickly build a development environment or run php applications. System administrators or software developers simply install Docker on the host machine and pull the PHP container image from the public Docker Hub repository.
Pull container image
Execute the following command to pull the latest php container image from Docker Hub:
sudo docker pull php
Start container
Execute the following command to start the php container:
sudo docker run -p 80:80 -v $PWD:/var/www/html php
Among them, the -p parameter maps the host's port 80 to the container's port 80, and uses the -v parameter to associate the current directory with the container's /var/www/html directory, so that the PHP files in the container can be accessed on the host. .
Summary
There are several different ways to install the php environment on Unix systems. Use your system package manager to quickly install preconfigured versions of php. Installing from source provides more customization options, including the latest version of PHP code and the ability to compile more language modules. Docker containerization can quickly build a development environment or run php applications. Different methods have their own advantages and limitations, and you can choose according to your needs.
The above is the detailed content of unix installation php environment. For more information, please follow other related articles on the PHP Chinese website!