Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Trong kho phần mềm chính thức của Ubuntu 18.04 thì PHP 5.4 đã cũ và không còn được các nhà phát triển hỗ trợ, duy trì và cập nhật bản vá lỗi.

Để hỗ trợ cho những tính năng mới cũng như tăng cường khả năng bảo mật, bạn cần phiên bản PHP mới hơn trên hệ thống Ubuntu 18.04.

Hiện tại, PHP 7.3 là phiên bản được phát hành mới nhất của PHP. Trong nội dung bài viết này, mình sẽ hướng dẫn bạn cài đặt các phiên bản PHP 7.x (bao gồm 7.0, 7.1, 7.2 và 7.3) lên hệ thống Ubuntu 18.04.

Cài đặt PHP 7 trên Ubuntu

Để cài đặt PHP 7.x, bạn cần kích hoạt PPA cho PHP trên hệ thống Ubuntu của bạn bằng những tập lệnh sau:PPA cho PHP trên hệ thống Ubuntu của bạn bằng những tập lệnh sau:

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php

Server của bạn đã được thiết lập để cài đặt PHP từ PPA repository. Sử dụng một trong những lệnh bên dưới để cài đặt PHP 7.0, PHP 7.1, PHP 7.2 hoặc PHP 7.3:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

Sau khi tiến trình cài đặt hoàn tất, để kiểm tra lại thông tin phiên bản PHP đang được kích hoạt lên server, bạn sử dụng lệnh:

$ sudo php -v 

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Cài đặt PHP Modules

Sau khi cài đặt một phiên bản PHP 7.x trên server, bạn có thể sử dụng lệnh bên dưới để tìm những PHP modules có sẵn trong gói PPA repository:

$ sudo apt-cache search php7*

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Cuối cùng, để cài đặt thêm một số PHP modules cần thiết cho các ứng dụng, bạn sử dụng lệnh bên dưới:

### For PHP 7.3
$ sudo apt-get install php7.3-mysql php7.3-curl php7.3-json php7.3-cgi php7.3-xsl

### For PHP 7.2
$ sudo apt-get install php7.2-mysql php7.2-curl php7.2-json php7.2-cgi php7.2-xsl

### For PHP 7.1
$ sudo apt-get install php7.1-mysql php7.1-curl php7.1-json php7.1-cgi php7.1-xsl

### For PHP 7.0
$ sudo apt-get install php7.0-mysql php7.0-curl php7.0-json php7.0-cgi php7.0-xsl

Chúc bạn thành công!

BÀi VIếT NÀY CHẤN TUYSI Sẽ HướNG DẫN Tại thời Điểm Viết Bài Chún tôm sẽ cài đặt php từ kho lưu trữ ppa ondrej / php. Trước khi n là

Bật kho lưu trữ php

Để Kích Hoạt Kho lưu Trữ, HÃy Chạy:

# sudo apt install software-properties-common

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

# sudo add-apt-repository ppa:ondrej/php

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Sau khi bạn ppa Được Kích Hoạt, bạn Có Thể Cài Đặt Php 8.

CÀi ĐặT Php 8.0 VớI Apache

Nếu Đang Sử dụng Apache làoer Web Server, bạn Có Thể Chạy Php Dưới Dạng Mô

CÀi ĐặT PHP dưới dạng Mô

CÀi ĐặT PHP lÀm Mô-Đun Apache là Một Công Việc Đơn Giản:

# sudo apt update

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

# sudo apt install php8.0 libapache2-mod-php8.0

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Sau Khim Các Các

# sudo systemctl restart apache2

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Đêu

Php-fpm là một trình quản lý QUy trình fastcgi Cho PHP. Chạy lệnh Sau đó

# sudo apt update

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

1

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Theo mặc Đêm, php-fpm không được bật trong apache. Để Kích Hoạt Nó, HÃy Chạy:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

2

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

3

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Để Kích Hoạt Các họ Đổi, Hãy Khởi Động Lại Apache:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

4

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

CÀi ĐặT PHP 8.0 VớI nginx

Nginx Không Có Hỗ Trợ Tích HợP Để Xử Lý Các TệP Php. Chún tôm sẽ sử dụng php-fpm Để xử Lý Các tệp Php. Chạy Các lệNH Sau Đêm Cài ĐặT Php Và PHP FPM:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

5

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

6

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Sau khi cài đặt xong, dịch vụ FPM sẽ tự động bắt đầu. Để kiểm tra trạng thái của dịch vụ, hãy chạy:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

7

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

8

Bây giờ bạn có thể chỉnh sửa khối Nginx server và thêm các dòng sau để Nginx có thể xử lý các tệp PHP:

$ sudo apt-get update
## Install PHP 7.3 $ sudo apt-get install -y php7.3

## Install PHP 7.2
$ sudo apt-get install -y php7.2

## Install PHP 7.1
$ sudo apt-get install -y php7.1

## Install PHP 7.0
$ sudo apt-get install -y php7.0

9

Đừng quên khởi động lại dịch vụ Nginx để cấu hình mới có hiệu lực:

$ sudo php -v 
0

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu

Cài đặt phần mở rộng PHP

Phần mở rộng PHP là các thư viện được biên dịch để mở rộng chức năng cốt lõi của PHP. Các tiện ích mở rộng có sẵn dưới dạng gói và có thể dễ dàng cài đặt với apt:

$ sudo php -v 
1

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Ví dụ: để cài đặt phần mở rộng MySQL và GD, bạn sẽ chạy lệnh sau:

$ sudo php -v 
2

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Sau khi cài đặt tiện ích mở rộng PHP mới, đừng quên khởi động lại dịch vụ Apache hoặc PHP FPM, tùy thuộc vào thiết lập của bạn.

Thử nghiệm xử lý PHP

Để kiểm tra xem máy chủ web có được định cấu hình đúng cách để xử lý PHP hay không, hãy tạo một tệp mới có tên info.php bên trong thư mục /var/www/html với mã sau: info.php bên trong thư mục /var/www/html với mã sau: http://your_server_ip/info.php Bạn sẽ thấy thông tin về cấu hình PHP của mình tương tự như sau:

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
Dẫn nguồn: linuxize.com

English: How to Install PHP 8 on Ubuntu 20.04

PHP is one of the most widely used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP. PHP 8.0 is the latest major release of the PHP language. It introduces several breaking changes, performance improvements, and lots of new features such as named arguments, JIT compiler, union types, match expression, and more. This article will show you how to install PHP 8 on Ubuntu 20.04 and integrate it with Nginx and Apache. At the time of writing, the default Ubuntu 20.04 repositories include PHP 7.4 version. We’ll install PHP from the ondrej/php PPA repository. Before upgrading to or installing PHP 8, make sure that your applications support it. The same steps apply for Ubuntu 18.04 and all Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS. This article will show you how to install PHP 8 on Ubuntu 20.04 and integrate it with Nginx and Apache. At the time of writing, the default Ubuntu 20.04 repositories include PHP 7.4 version. We’ll install PHP from the ondrej/php PPA repository. Before upgrading to or installing PHP 8, make sure that your applications support it. The same steps apply for Ubuntu 18.04 and all Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS.

Enabling PHP Repository

a Debian developer, maintains a repository that includes multiple PHP versions. To enable the repository , run: # sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php Once the PPA is enabled, you can install PHP 8. # sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php Once the PPA is enabled, you can install PHP 8.

Installing PHP 8.0 with Apache

If you’re using Apache as a web server, you can run PHP as an Apache module or PHP-FPM.

Install PHP as Apache Module

Installing PHP as an Apache module is a straightforward task:

$ sudo php -v 
3

Once the packages are installed, restart Apache for the PHP module to get loaded:

# sudo systemctl restart apache2

Configure Apache with PHP-FPM

Php-FPM is a FastCGI process manager for PHP. Run the following command to install the necessary packages:

$ sudo php -v 
5

By default PHP-FPM is not enabled in Apache. To enable it, run:

$ sudo php -v 
6

To activate the changes, restart Apache:

$ sudo php -v 
7

Installing PHP 8.0 with Nginx

Nginx doesn’t have built-in support for processing PHP files. We’ll use PHP-FPM (“fastCGI process manager”) to handle the PHP files. Run the following commands to install PHP and PHP FPM packages:

$ sudo php -v 
8

Once the installation is completed, the FPM service will start automatically. To check the status of the service, run

$ sudo php -v 
9
$ sudo apt-cache search php7*
0

You can now edit the Nginx server block and add the following lines so that Nginx can process PHP files:

$ sudo apt-cache search php7*
1

Do not forget to restart the Nginx service so that the new configuration takes effect:

$ sudo apt-cache search php7*
2

Installing PHP extensions

PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are available as packages and can be easily installed with apt :

$ sudo apt-cache search php7*
3

For example, to install MySQL and GD extensions, you would run the following command:

$ sudo php -v 
2

After installing a new PHP extension, do not forget to restart Apache or PHP FPM service, depending on your setup.

Testing PHP Processing

To test whether the web server is configured properly for PHP processing, create a new file named info.php inside the /var/www/html directory with the following code: /var/www/html/info.php

Hướng dẫn install php on ubuntu - cài đặt php trên ubuntu
You’ll see information about your PHP configuration similar to the following:

Conclusion

Installing PHP 8 on Ubuntu 20.04 server is a simple task. All you need to do is to enable the “ondrej/php” repository and install PHP 8 with apt. If you have any questions or feedback, do not hesitate to leave a comment. If you have any questions or feedback, do not hesitate to leave a comment.