Chạy nhiều version PHP trên một Ubuntu 18.04 – server

30/12/2020

Như chúng ta đã biết thì PHP thường được sử dụng để làm ngôn ngữ trong việc tạo ra một trang web. Mỗi một phiên bản của PHP thì sẽ lại thích ứng với một phiên bản của một ứng dụng khác nhau. Ví dụ như wordpress.

Ở bài này tôi sẽ hướng dẫn làm thế nào để cài được nhiều version của PHP trên một server.

Mục lục

  1. Kịch bản
  2. Thực hiện

Kịch bản

  1. Cài đặt gói phần mềm Apache
  2. Cài đặt 2 site của server
  3. Trỏ domain tới 2 site đó
  4. Cài đặt hai version PHP
  5. Cho mỗi site sử dụng một version PHP khác nhau
  6. kiểm tra

Thực hiện

1) Cài đặt apache

Download gói cài đặt

sudo apt install apache2

Khởi động và bật chạy cùng hệ thống

sudo systemctl start apache2 sudo systemctl enable apache2

2) Cài đặt 2 site

2.1) Cài đặt site thứ 1

Tạo thư mục chứa nội dung site 1. và di chuyển vào thư mục

sudo mkdir /var/www/anhduc.com cd /var/www/anhduc.com

Cấp quyền truy cập cho thư mục

sudo chmod -R 755 /var/www/anhduc.com

Tạo ra một nội dung cho site đó trong file index.html

<html>     <head>         <title>Welcome to Your_domain!</title>     </head>     <body>         <h1>Success!  The your_domain virtual host is working!</h1>     </body> </html>

Tạo ra virtual host

vi /etc/apache2/sites-available/anhduc.com.conf 

nội dung file virtual host

<VirtualHost *:80>     ServerName anhduc.com     ServerAlias www.anhduc.com     DocumentRoot /var/www/anhduc.com     DirectoryIndex info.php      <Directory /var/www/anhduc.com>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>     ErrorLog ${APACHE_LOG_DIR}/anhduc_error.log     CustomLog ${APACHE_LOG_DIR}/anhduc.com_access.log combined  </VirtualHost>

Cho phép bật nội dung với công cụ a2ensite

sudo a2ensite anhduc.com.conf   sudo a2dissite 000-default.conf

Kiểm tra cấu hình apache

sudo apache2ctl configtest

Khởi động lại apache

sudo systemctl restart apache2

2.2) Cài đặt site thứ 2

Tạo thư mục chứa nội dung site 2. và di chuyển vào thư mục

sudo mkdir /var/www/anhduc.xyz cd /var/www/anhduc.xyz

Cấp quyền truy cập cho thư mục

sudo chmod -R 755 /var/www/anhduc.xyz

Tạo ra một nội dung cho site đó trong file index.html

<html>     <head>         <title>Welcome to Your_domain!</title>     </head>     <body>         <h1> Đây là site 2</h1>     </body> </html>

Tạo ra virtual host

vi /etc/apache2/sites-available/anhduc.xyz.conf 

nội dung file virtual host

<VirtualHost *:80>     ServerName anhduc.xyz     ServerAlias www.anhduc.xyz     DocumentRoot /var/www/anhduc.xyz     DirectoryIndex info.php      <Directory /var/www/anhduc.xyz>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>     ErrorLog ${APACHE_LOG_DIR}/anhduc_error.log     CustomLog ${APACHE_LOG_DIR}/anhduc.xyz_access.log combined  </VirtualHost>

Cho phép bật nội dung với công cụ a2ensite

sudo a2ensite anhduc.xyz.conf   sudo a2dissite 000-default.conf

Kiểm tra cấu hình apache

sudo apache2ctl configtest

Khởi động lại apache

sudo systemctl restart apache2

3) Trỏ domain cho 2 site

Để có thể trỏ được domain tới hai site này mà không cần đăng ký tên miền thì ta cần sửa file hosts của máy truy cập đến để có thể sử dụng tên đó.

vi /etc/hosts 

Nội dung file hosts

27.0.0.1       localhost 127.0.1.1       anhduc 10.10.34.133    anhduc.com 10.10.34.133    anhduc.xyz # The following lines are desirable for IPv6 capable hosts ::1     ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters

Truy cập bằng trình duyệt để kiểm tra 2 site này.

4) Cài đặt hai version PHP

Cài đặt phần mềm software-properties-common

sudo apt-get install software-properties-common -y

Add repository ondrej/php. Nó có những phiên bản cập nhật của PHP mới nhất

sudo add-apt-repository ppa:ondrej/php

Cập nhật repository

sudo apt-get update -y

Cài đặt PHP 7.0

sudo apt-get install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y

Start PHP 7.0

sudo systemctl start php7.0-fpm 

Kiểm tra trạng thái PHP 7.0

sudo systemctl status php7.0-fpm 

Cài đặt PHP 7.2

sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y

Start PHP 7.2

sudo systemctl start php7.2-fpm

Kiểm tra trạng thái PHP7.2

sudo systemctl status php7.2-fpm

Kích hoạt module để có thể làm việc được với 2 phiên bản PHP

sudo a2enmod actions fcgid alias proxy_fcgi

Khởi động lại apache

sudo systemctl restart apache2

5) Cài đặt nội dung cho 2 site

Tạo file info PHP cho site 1

echo '<?php phpinfo(); ?>' > vi /var/www/anhduc.com/info.php

Copy cho site thứ 2

cp /var/www/anhduc.com/info.php /var/www/anhduc.xyz/info.php

Cấu hình cho 2 site sử dụng 2 version PHP khác nhau

vi /etc/apache2/sites-available/anhduc.com.conf
<VirtualHost *:80>     ServerAdmin webmaster@localhost     ServerName anhduc.com     ServerAlias www.anhduc.com     DocumentRoot /var/www/anhduc.com     DirectoryIndex info.php      <Directory /var/www/anhduc.com>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>     <FilesMatch .php$>       # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server       SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"     </FilesMatch>      ErrorLog ${APACHE_LOG_DIR}/anhduc_error.log      CustomLog ${APACHE_LOG_DIR}/anhduc.com_access.log combined </VirtualHost>
vi /etc/apache2/sites-available/anhduc.xyz.conf
<VirtualHost *:80>     ServerAdmin webmaster@localhost     ServerName anhduc.xyz     ServerAlias www.anhduc.xyz     DocumentRoot /var/www/anhduc.xyz     DirectoryIndex info.php      <Directory /var/www/anhduc.xyz>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>         <FilesMatch .php$>       # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server       SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"     </FilesMatch>      ErrorLog ${APACHE_LOG_DIR}/anhduc.xyz_error.log      CustomLog ${APACHE_LOG_DIR}/anhduc.xyz_access.log combined </VirtualHost>

Kiểm tra cấu hình apache

sudo apachectl configtest

Khởi động lại apache

sudo systemctl restart apache2

6) Kiểm tra

Để kiểm tra được version của 2 site thì ta cần đăng nhập với url

http://anhduc.com/info.php

Vậy là ta có thể sử dụng được 2 version PHP trên một server. Các bạn có thể sử dụng nó theo cách mà mình muốn nhé!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Python 3 on CentOS 7

In this article, I will show you how to install Python 3.x, mainly Python 3.4, Python 3.5, Python 3.6 along with PIP for...
28/12/2020

How to Install and Configure iSCSI Storage Server on CentOS 7

Internet Small Computer Systems Interface or iSCSI in short is used to share block devices on the network. It provides...
28/12/2020

CentOS Install Htop

It doesn’t matter whatever system you’re using – Windows, Linux or macOS or whatever, having a handy task manager...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024