本教程将指导您如何在Ubuntu 20.04上安装PHP 8,包括下载、编译和配置等步骤。
在Ubuntu 20.04上安装PHP 8的教程如下:
1、更新系统软件包
我们需要确保系统软件包是最新的,打开终端,输入以下命令:
sudo apt update sudo apt upgrade
2、安装依赖软件包
为了安装PHP 8,我们需要安装一些依赖软件包,在终端中输入以下命令:
sudo apt install software-properties-common libonig-dev libxml2-dev libssl-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libmcrypt-dev libreadline-dev libxslt1-dev zlib1g-dev libzip-dev unzip git
3、添加PHP 8存储库
接下来,我们需要添加PHP 8的存储库,在终端中输入以下命令:
wget -O https://packages.sury.org/php/apt.gpg | sudo apt-key add - echo "deb https://packages.sury.org/php/ focal main" | sudo tee /etc/apt/sources.list.d/php-focal.list
4、更新存储库列表并安装PHP 8
现在,我们可以更新存储库列表并安装PHP 8,在终端中输入以下命令:
sudo apt update sudo apt install php8.0 php8.0-cli php8.0-fpm php8.0-mysql php8.0-mbstring php8.0-zip php8.0-xml
5、配置FPM(可选)
如果您需要使用PHP FPM作为Web服务器,可以按照以下步骤配置它,创建一个名为php-fpm
的systemd服务文件:
sudo nano /etc/systemd/system/php-fpm.service
将以下内容粘贴到文件中:
[Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple User=www-data Group=www-data WorkingDirectory=/var/www/html/public_html/your_project_directory/public_html/vendor/bin ExecStart=/usr/sbin/php-fpm8.0 -F -O /run/php-fpm8.sock -c /etc/php8/fpm/php-fpm.conf Restart=always RestartSec=10s KillSignal=SIGINT SendSIGKILL=no ProcessType=forking PIDFile=/var/run/php-fpm8.pid TimeoutStopSec=10s SendMessageWhenStopped=yes User=www-data Group=www-data UMask=000777 Environment="PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/usr/local/sbin" "PHP_HOME=/usr" "PHP_INI_DIR=/etc/php8" "FPM_CONF=/etc/php8/fpm/php-fpm.conf" "OPCACHE_VALIDATE_TIMESTAMPS=0" "OPCACHE_MAX_ACCELERATED_FILES=2000" "OPCACHE_REVALIDATE_AGE=600" "OPCACHE_MEM_SIZE=128" "OPCACHE_DISABLE_COMPRESSION=1" "OPCACHE_HASH_FUNCTION=" "OPCACHE_BLOCK_SIZE=64" "OPCACHE_MAX_OBJECT_SIZE=2500000" "OPCACHE_PROCESS_COMMAND=''" "OPCACHE_PREFIX=$(pwd)/opcache" "PHP_ERRORS=1" "LOGGING=1" "LOGLEVEL=error" "ERRORLOGFILE=-" "VARNISH_PORT=81" "VARNISH_LISTEN_PORT=6081" "VARNISH_BACKEND_HOSTS=localhost,localhost:6082" "VARNISH_BACKEND_PORT=9292" "VARNISH_BACKEND_HTTPS=off" "VARNISH_BACKEND_SSLKEYFILE=${APACHE_RUN_DIR}/ssl.key" "VARNISH_BACKEND_SSLCERTFILE=${APACHE_RUN_DIR}/ssl.crt" "VARNISH_BACKEND_SSLCHAINFILE=${APACHE_RUN_DIR}/ssl.cacert" "VARNISHD_SECURITY_POLICY='none'" "VARNISHD_SECURE_LISTEN='off'" "VARNISHD_DEBUG='all'" "VARNISHD_THREADS=4" "VARNISHD_DAEMONIZE='off'" "VARNISHD_OPTS='-a :6081 \u0026\u0023+feature\u0026-\u0026autoresp\u0026-\u0026benchmark\u0026-\u0026child\u0026-\u0026default\u0026-\u0026dump\u0026-\u0026helpu0026-\u0026log\u0026-\u0026lua\u0026-\u0026lockfile\u0026-\u0026monitor\u0026-\u0026nolisten\u0026-\u0026novcl\u0026-\u0026nullu0026-\u0026randomize\u0026-\u0026report\u0026-\u0026rttests\u0026-\u0026showconfig\u0026-\u0026status\u0026-\u0026testcase\u0026-u0026unlockfile\u0026-\u0026vclreload'" "VARNISHD=''" "VARNISHDEXITCODE=''" "VARNISHCONFIG=''" "VARNISHSTATSFILE=''" "VARNISHPIDFILE=''" "VARNISHLOGFILE=''" "VARNISHSECURITYPOLICY='none'" "VARNISHSECURELISTEN='off'" "VARNISHDEBUG='all'" "VARNISHDAEMONIZE='off'" "VARNISHDAEMONOPTS=''" > /dev/null 2>&1 &
保存并关闭文件,运行以下命令以启动FPM服务:
sudo systemctl start php8.1-fpm.service sudo systemctl enable php8.1-fpm.service
现在,PHP 8已经在您的Ubuntu 20.04系统上安装完成,您可以创建一个新的PHP文件来测试它,创建一个名为info.php
的文件,并在其中输入以下内容:
<?php phpinfo(); ?>
将此文件放置在您的Web服务器的根目录中,并通过浏览器访问它,您应该能看到一个包含有关您的PHP安装的详细信息的页面。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/360174.html