Quick Installation Guide for PHP 8.2 Stack on Ubuntu 20.04 (Focal)

This step‑by‑step tutorial shows how to configure Alibaba mirrors, install PHP 8.2, Nginx, MySQL 8, Redis 6 and Composer on Ubuntu 20.04, adjust PHP‑FPM settings, create a test PHP file, start services, and verify the full LEMP stack works within about 20 minutes.

php Courses
php Courses
php Courses
Quick Installation Guide for PHP 8.2 Stack on Ubuntu 20.04 (Focal)

This guide explains how to quickly set up a complete PHP 8.2 development stack on Ubuntu 20.04 (Focal) using Alibaba Cloud mirrors for faster package downloads.

Software versions used : Ubuntu 20.04, PHP 8.2.1, Nginx 1.22.1, MySQL 8.0.31, Redis 7.0.7, Git 2.24.4.

1. Add Alibaba apt repository

apt update
vim /etc/apt/sources.list

– replace existing entries with the following lines (use https for speed):

deb https://mirrors.aliyun.com/ubuntu focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu focal main restricted universe multiverse

... (similar entries for security, updates, backports) ...

After editing, run apt update again.

2. Install PHP 8.2 and extensions

apt install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils systemd gnupg2 lsb-release ubuntu-keyring

Choose the Asia/Shanghai mirror when prompted.

Import Ondřej Surý’s key and add his PPA:

curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /usr/share/keyrings/ppa_ondrej_php.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list
apt-get update
apt-get install -y php8.2-cli php8.2-dev php8.2-pgsql php8.2-sqlite3 php8.2-gd php8.2-curl php8.2-imap php8.2-mysql php8.2-mbstring php8.2-xml php8.2-zip php8.2-bcmath php8.2-soap php8.2-intl php8.2-readline php8.2-ldap php8.2-msgpack php8.2-igbinary php8.2-redis php8.2-swoole php8.2-memcached php8.2-pcov php8.2-fpm php8.2-gmp php8.2-imagick php8.2-mcrypt php8.2-uuid php8.2-yaml

3. Install Composer from Alibaba mirror

curl -o /usr/local/bin/composer https://mirrors.aliyun.com/composer/composer.phar
chmod +x /usr/local/bin/composer
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

To avoid the “yes” prompt when running Composer as root, add: vim /etc/environment and append export COMPOSER_ALLOW_SUPERUSER=1, then source /etc/environment.

4. Install Nginx and configure PHP‑FPM

Import Nginx signing key and add its repository:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | tee /etc/apt/sources.list.d/nginx.list
apt update
apt install nginx

Modify PHP‑FPM pool config to listen on TCP:

sed -i 's/listen = \/run\/php\/php8.2-fpm.sock/listen = 127.0.0.1:9000/g' /etc/php/8.2/fpm/pool.d/www.conf

Adjust /etc/nginx/nginx.conf (set user www-data;) and replace the default site configuration with a custom /etc/nginx/conf.d/default.conf that routes .php files to 127.0.0.1:9000. Example server block is included in the original article.

Create a test PHP file: vi /usr/share/nginx/html/1.php Contents: <?php phpinfo(); ?> Start services:

/etc/init.d/nginx start
/etc/init.d/php8.2-fpm start

Verify with curl localhost/1.php – you should see the PHP info page.

5. Install MySQL 8 and configure root user apt install mysql-server-8.0 mysql-client-8.0 Find the temporary root password in /var/log/mysqld.log and log in with mysql -uroot -p. Then run:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'tb4Wn3BthR.';
flush privileges;

Create a remote root user and grant privileges:

create user 'root'@'%' identified by 'root1234';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root1234';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
drop user root@localhost;
flush privileges;

Now you can connect with mysql -uroot -proot1234.

6. Install Redis 6 (stack server)

Add Redis package repository:

curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
apt-get update
apt-get install redis-stack-server

If you prefer Redis 5, you can simply run apt install redis-server without adding a repository.

7. Summary

The entire LEMP (Linux, Nginx, MySQL, PHP) stack with Redis and Composer can be installed in roughly 20 minutes on Ubuntu 20.04 using Alibaba Cloud mirrors, though mirror speed may vary.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDevOpsNginx
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.