Cách cài đặt PHP 8.3 và thiết lập môi trường trên Ubuntu 24.04
Bài viết này giải thích cách cài đặt PHP 8.3 trên hệ thống Debian và Ubuntu hiện đại. Một số tiện ích mở rộng PECL phổ biến nhất cũng có sẵn để cài đặt theo cách tương tự, được liệt kê trong phần Cài đặt tiện ích mở rộng PECL.
Giới thiệu
PHP là ngôn ngữ lập trình máy chủ phổ biến được biết đến với khả năng tạo ra các trang web động và tương tác. Bắt đầu và chạy với ngôn ngữ bạn chọn là bước đầu tiên trong việc học lập trình.
Hướng dẫn này sẽ hướng dẫn bạn cài đặt PHP 8.1 trên Ubuntu và thiết lập môi trường lập trình cục bộ thông qua dòng lệnh. Bạn cũng sẽ cài đặt trình quản lý phụ thuộc, Composer và kiểm tra cài đặt của mình bằng cách chạy một tập lệnh.
Điều kiện tiên quyết
Để hoàn thành hướng dẫn này, bạn sẽ cần một máy thật hoặc máy ảo cài đặt Ubuntu và có quyền root và có kết nối internet. Bạn có thể tải xuống hệ điều hành này qua trang chủ Ubuntu.
Các bước cài đặt
Bước 1 — Thiết lập PHP 8.3
Trên Ubuntu, bạn có thể tìm ứng dụng Terminal bằng cách nhấp vào biểu tượng Ubuntu ở góc trên bên trái màn hình và nhập terminal vào thanh tìm kiếm. Nhấp vào biểu tượng ứng dụng Terminal để mở. Ngoài ra, bạn có thể nhấn đồng thời các phím CTRL, ALT, và T trên bàn phím để tự động mở ứng dụng Terminal.
Cập nhật hệ thống và cài đặt PHP 8.3
# Cập nhật hệ thống
sudo apt update && sudo apt upgrade -y
# Cài đặt PHP 8.3
sudo apt install --no-install-recommends php8.3
Cờ --no-install-recommends sẽ đảm bảo rằng các gói khác như máy chủ web Apache không được cài đặt.
Sau khi cài xong hãy kiểm tra PHP bằng lệnh sau:
php -v
Kết quả sẽ như thế này:
PHP 8.3.9 (cli) (built: Jul 2 2024 18:18:06) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.9, Copyright (c) Zend Technologies
Cài đặt thêm các module cơ bản
Bạn cũng có thể cài đặt nhiều hơn một gói cùng một lúc. Sau đây là một số gợi ý về các mô-đun phổ biến nhất mà bạn có thể muốn cài đặt:
sudo apt-get install -y php8.3-cli php8.3-common php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath
Cấu hình PHP liên quan đến Apache được lưu trữ trong /etc/php/8.3/apache2/php.ini. Bạn có thể liệt kê tất cả các mô-đun PHP đã tải bằng lệnh sau:
php -m
Bước 2 — Thiết lập Composer (Tùy chọn)
Bạn có thể cài đặt Composer chỉ với 1 lệnh sau:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Sau khi cài xong hãy dùng lệnh sau để kiểm tra Composer:
composer -v
Kết quả sẽ như sau:
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.7.7 2024-06-10 22:11:12
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer's internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [dumpautoload] Dumps the autoloader
exec Executes a vendored binary/script
fund Discover how to help fund the maintenance of your dependencies
global Allows running commands in the global composer dir ($COMPOSER_HOME)
help Display help for a command
init Creates a basic composer.json file in current directory
install [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licenses Shows information about licenses of dependencies
list List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove [rm|uninstall] Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock
Kết luận
Lúc này, bạn đã thiết lập môi trường lập trình PHP 8.3 trên hệ thống Ubuntu 24.04 và có thể bắt đầu một dự án.