Install php centos 8 stream


PHP 7.4 : Install

2021/03/15

 

Install PHP 7.4.

[1] Confirm the current enabled version of PHP and Install it.

[root@dlp ~]#

dnf module list php


CentOS Stream 8 - AppStream
Name      Stream       Profiles                       Summary
php       7.2 [d]      common [d], devel, minimal     PHP scripting language
php       7.3 [e]      common [d], devel, minimal     PHP scripting language
php       7.4          common [d], devel, minimal     PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

# if other versions are enabled, reset once and switch to the version

[root@dlp ~]#

dnf module reset php

[root@dlp ~]#

dnf module enable php:7.4

# specify PHP 7.4 and install


[root@dlp ~]#

dnf module -y install php:7.4/common


Dependencies resolved.
================================================================================
 Package       Arch    Version                                 Repository  Size
================================================================================
Upgrading:
 php-cli       x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream  3.1 M
 php-common    x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream  696 k
 php-fpm       x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream  1.6 M
 php-json      x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream   74 k
 php-mbstring  x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream  484 k
 php-xml       x86_64  7.4.6-4.module_el8.3.0+434+2ab5050a     appstream  174 k
Installing module profiles:
 php/common

Transaction Summary
================================================================================
Upgrade  6 Packages
.....
.....

[root@dlp ~]#

php -v


PHP 7.4.6 [cli] [built: May 12 2020 08:09:15] [ NTS ]
Copyright [c] The PHP Group
Zend Engine v3.4.0, Copyright [c] Zend Technologies

# verify to create test script


[root@dlp ~]#

echo "" > php_test.php


[root@dlp ~]#

php php_test.php


PHP 7.4 Test Page

Matched Content


PHP 8.0 : Install

2022/02/08

 

Install PHP 8.0.

[1] Confirm the current enabled version of PHP and Install it.

[root@dlp ~]#

dnf module list php


CentOS Stream 8 - AppStream
Name      Stream       Profiles                       Summary
php       7.2 [d]      common [d], devel, minimal     PHP scripting language
php       7.3          common [d], devel, minimal     PHP scripting language
php       7.4          common [d], devel, minimal     PHP scripting language
php       8.0          common [d], devel, minimal     PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

# if other versions are enabled, reset once and switch to the version

[root@dlp ~]#

dnf module reset php

[root@dlp ~]#

dnf module enable php:8.0

# specify PHP 8.0 and install


[root@dlp ~]#

dnf module -y install php:8.0/common


Dependencies resolved.
================================================================================
 Package          Arch   Version                                Repo       Size
================================================================================
Installing group/module packages:
 php-cli          x86_64 8.0.13-2.module_el8.6.0+1066+63503082  appstream 3.1 M
 php-common       x86_64 8.0.13-2.module_el8.6.0+1066+63503082  appstream 720 k
 php-fpm          x86_64 8.0.13-2.module_el8.6.0+1066+63503082  appstream 1.6 M
 php-mbstring     x86_64 8.0.13-2.module_el8.6.0+1066+63503082  appstream 482 k
 php-xml          x86_64 8.0.13-2.module_el8.6.0+1066+63503082  appstream 175 k
Installing dependencies:
 httpd-filesystem noarch 2.4.37-43.module_el8.5.0+1022+b541f3b1 appstream  39 k
 nginx-filesystem noarch 1:1.14.1-9.module_el8.0.0+1060+3ab382d3
                                                                appstream  24 k
Installing module profiles:
 php/common

Transaction Summary
================================================================================
Install  7 Packages
.....
.....

[root@dlp ~]#

php -v


PHP 8.0.13 [cli] [built: Nov 16 2021 18:07:21] [ NTS gcc x86_64 ]
Copyright [c] The PHP Group
Zend Engine v4.0.13, Copyright [c] Zend Technologies

# verify to create test script


[root@dlp ~]#

echo "" > php_test.php


[root@dlp ~]#

php php_test.php


PHP 8.0 Test Page

Matched Content

Introduction

A “LAMP” stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps written in PHP. This term is an acronym which represents the Linux operating system, with the Apache web server. The backend data is stored in a MariaDB database and the dynamic processing is handled by PHP.

The database layer in a LAMP stack is typically a MySQL database server, but prior to the release of CentOS 8, MySQL wasn’t available from the default CentOS repositories. Because of this, MariaDB, a community fork of MySQL, became a widely accepted alternative to MySQL as the default database system for LAMP stacks on CentOS machines. MariaDB works as a drop-in replacement for the original MySQL server, which in practice means you can switch to MariaDB without having to make any configuration or code changes in your application.

In this guide, you’ll install a LAMP stack on a CentOS 8 server, using MariaDB as the database management system.

Prerequisites

To follow this guide, you’ll need access to a CentOS 8 server as a non-root user with sudo privileges, and an active firewall installed on your server. To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

Step 1 — Installing the Apache Web Server

In order to display web pages to our site visitors, we are going to employ Apache, a popular open source web server that can be configured to serve PHP pages. We’ll use the dnf package manager, which is the new default package manager on CentOS 8, to install this software.

Install the httpd package with:

  1. sudo dnf install httpd

When prompted, enter y to confirm that you want to install Apache.

After the installation is finished, run the following command to enable and start the server:

  1. sudo systemctl start httpd

In case you have enabled the firewalld firewall as per our initial server setup guide, you will need to allow connections to Apache. The following command will permanently enable HTTP connections, which run on port 80 by default:

  1. sudo firewall-cmd --permanent --add-service=http

To verify that the change was applied, you can run:

  1. sudo firewall-cmd --permanent --list-all

You’ll see output like this:

Output

public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

You’ll need to reload the firewall configuration so the changes take effect:

  1. sudo firewall-cmd --reload

With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser.

Note: In case you are using DigitalOcean as DNS hosting provider, you can check our product docs for detailed instructions on how to set up a new domain name and point it to your server.

If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by running the following command:

  1. ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will print out a few IP addresses. You can try each of them in turn in your web browser.

As an alternative, you can check which IP address is accessible, as viewed from other locations on the internet:

  1. curl -4 icanhazip.com

Type the address that you receive in your web browser and it will take you to Apache’s default landing page:

If you see this page, then your web server is now correctly installed.

Step 2 — Installing MariaDB

Now that you have a web server up and running, you need to install a database system to be able to store and manage data for your site. We’ll install MariaDB, a community-developed fork of the original MySQL server by Oracle.

To install this software, run:

  1. sudo dnf install mariadb-server

When the installation is finished, you can enable and start the MariaDB server with:

  1. sudo systemctl start mariadb

To improve the security of your database server, it’s recommended that you run a security script that comes pre-installed with MariaDB. This script will remove some insecure default settings and lock down access to your database system. Start the interactive script by running:

  1. sudo mysql_secure_installation

This script will take you through a series of prompts where you can make some changes to your MariaDB setup. The first prompt will ask you to enter the current database root password. This is not to be confused with the system root user. The database root user is an administrative user with full privileges over the database system. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.

The next prompt asks you whether you’d like to set up a database root password. Because MariaDB uses a special authentication method for the root user that is typically safer than using a password, you don’t need to set this now. Type N and then press ENTER.

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove anonymous users and the test database, disable remote root login, and load these new rules so that the server immediately respects the changes you have made.

When you’re finished, log in to the MariaDB console by typing:

  1. sudo mysql

This will connect to the MariaDB server as the administrative database user root, which is inferred by the use of sudo when running this command. You should see output like this:

Output

Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright [c] 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [[none]]>

Notice that you didn’t need to provide a password to connect as the root user. That works because the default authentication method for the administrative MariaDB user is unix_socket instead of password. Even though this might look like a security concern at first, it makes the database server more secure because the only users allowed to log in as the root MariaDB user are the system users with sudo privileges connecting from the console or through an application running with the same privileges. In practical terms, that means you won’t be able to use the administrative database root user to connect from your PHP application.

For increased security, it’s best to have dedicated user accounts with less expansive privileges set up for every database, especially if you plan on having multiple databases hosted on your server. To demonstrate such a setup, we’ll create a database named example_database and a user named example_user, but you can replace these names with different values.

To create a new database, run the following command from your MariaDB console:

  1. CREATE DATABASE example_database;

Now you can create a new user and grant them full privileges on the custom database you’ve just created. The following command defines this user’s password as password, but you should replace this value with a secure password of your own choosing:

  1. GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

This will give the example_user user full privileges over the example\_database database, while preventing this user from creating or modifying other databases on your server.

Flush the privileges to ensure that they are saved and available in the current session:

  1. FLUSH PRIVILEGES;

Following this, exit the MariaDB shell:

  1. exit

You can test if the new user has the proper permissions by logging in to the MariaDB console again, this time using the custom user credentials:

  1. mysql -u example_user -p

Note the -p flag in this command, which will prompt you for the password you chose when creating the example_user user. After logging in to the MariaDB console, confirm that you have access to the example\_database database:

  1. SHOW DATABASES;

This will give you the following output:

Output

+--------------------+ | Database | +--------------------+ | example_database | | information_schema | +--------------------+ 2 rows in set [0.000 sec]

To exit the MariaDB shell, type:

  1. exit

At this point, your database system is set up and you can move on to installing PHP, the final component of the LAMP stack.

Step 3 — Installing PHP

You have Apache installed to serve your content and MariaDB installed to store and manage your data. PHP is the component of our setup that will process code to display dynamic content to the final user. In addition to the php package, you’ll need php-mysqlnd, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.

To install the php and php-mysqlnd packages using the dnf package manager, run:

sudo dnf install php php-mysqlnd

After the installation is finished, you’ll need to restart the Apache web server in order to enable the PHP module:

sudo systemctl restart httpd

Your web server is now fully set up. In the next step, we’ll create a PHP testing script to make sure everything works as expected.

Step 4 — Testing PHP with Apache

The default Apache installation on CentOS 8 will create a document root located at /var/www/html. You don’t need to make any changes to Apache’s default settings in order for PHP to work correctly within your web server.

The only adjustment we’ll make is to change the default permission settings on your Apache document root folder. This way, you’ll be able to create and modify files in that directory with your regular system user, without the need to prefix each command with sudo.

The following command will change the ownership of the default Apache document root to a user and group called sammy, so be sure to replace the highlighted username and group in this command to reflect your system’s username and group.

  1. sudo chown -R sammy.sammy /var/www/html/

We’ll now create a test PHP page to make sure the web server works as expected.

The default text editor that comes with CentOS 8 is vi. vi is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as nano to facilitate editing files on your CentOS 8 server:

  1. sudo dnf install nano

Type y when prompted to confirm the installation.

Now, create a new PHP file called info.php at the /var/www/html directory:

  1. nano /var/www/html/info.php

The following PHP code will display information about the current PHP environment running on the server:

/var/www/html/info.php

Bài mới nhất

Chủ Đề