Configure a LAMP Server on CentOS 8 for PHP Web Development

29/12/2020
Chưa phân loại
In this article, I am going to show you how to configure CentOS 8 as a LAMP (Linux, Apache,  MariaDB/MySQL, PHP) server for PHP web development. So, let’s get started.

Updating CentOS 8 Package Repository Cache:

First, update the CentOS 8 package repository cache with the following command:

$ sudo dnf makecache

Installing and Configuring MySQL/MariaDB:

I am going to show you how to configure the database first.

To install MariaDB database client tools and server, run the following command:

$ sudo dnf install mariadb mariadb-server

To confirm the installation, press Y and then press <Enter>.

MariaDB database server and client programs should be installed.

Now, check the status of the mariadb service as follows:

$ sudo systemctl status mariadb

It may be inactive (not running) and disabled (won’t automatically start on system boot) as shown in the screenshot below.

Start mariadb service with the following command:

$ sudo systemctl start mariadb

mariadb service should be active.

$ sudo systemctl status mariadb

Now, add mariadb service to the system startup as follows:

$ sudo systemctl enable mariadb

Now, you should set up a MariaDB root password. To do that, run the following command:

$ sudo mysql_secure_installation

Press <Enter>.

Press <Enter>.

Now, type in a new root password and press <Enter>.

Type in the root password again and press <Enter>.

Press Y and then press <Enter>.

Press Y and then press <Enter>.

Press Y and then press <Enter>.

Press Y and then press <Enter>.

MariaDB root password should be set.

Now, login to the MariaDB shell as root user as follows:

$ sudo mysql -u root -p

Type in the root password and press <Enter>.

You should be logged in.

Now, create a new MariaDB user as follows:

> GRANT ALL ON *.* TO ‘<username>’@‘localhost’ IDENTIFIED BY ‘<password>’

Make sure to replace <username> and <password> with your own username and password.

Now, run the following SQL statement for the changes to take effect.

> FLUSH PRIVILEGES;

Now, exit out of the MariaDB database as follows:

> exit

Installing and Configuring Apache Web Server and PHP:

Now, run the following command to install Apache web server and PHP:

$ sudo dnf install httpd httpd-tools php php-cli php-json php-gd php-mbstring php-pdo
 php-xml php-mysqlnd

To confirm the installation, press Y and then press <Enter>.

Apache web server and PHP should be installed.

Now, check the status of the httpd server as follows:

$ sudo systemctl status httpd

It may be inactive (not running) and disabled (won’t auto start on system boot) by default.

Start httpd service as follows:

$ sudo systemctl start httpd

The httpd service should be active.

$ sudo systemctl status httpd

Now, add the httpd service to the system startup as follows:

$ sudo systemctl enable httpd

The main configuration file of Apache web server is /etc/httpd/conf/httpd.conf

Custom configuration files should be put in the /etc/httpd/conf.d/ directory.

The default webroot directory is /var/www/html

Now, to test whether Apache web server and PHP is working, create a new PHP script index.php in the default webroot directory /var/www/html as follows:

$ echo ‘<?php phpinfo(); ?>‘ | sudo tee /var/www/html/index.php

Now, open a web browser and try to access http://localhost

You should see the phpinfo page as shown in the screenshot below. So, Apache and PHP are working correctly.

Letting Apache Web Server to Write to Web Root:

By default, the Apache web server can only read from the default web root directory /var/www/html

If your application needs to write to the directory, it must be owned by the apache user and group.

To change the user and group of the Apache web root directory /var/www/html to apache, run the following command:

$ sudo chown -Rf apache:apache /var/www/html

On CentOS 8, you also have to configure SELinux to allow write to the webroot directory /var/www/html.

You can configure SELinux for the /var/www/html directory and its contents with the following command:

$ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html(/.*)?"

For the SELinux changes to take effect, run the following command:

$ sudo restorecon -Rv /var/www/html

Making Web Development Easier:

When you’re developing a website, you would want to make changes to the /var/www/html directory as your login user.

To make this easier, create a symbolic link of the /var/www/html directory in your user’s home directory as follows:

$ ln -s /var/www/html ~/public_html

Also, give everyone read, write and execute permission to the directory /var/www/html as follows:

$ sudo chmod -R 777 /var/www/html

Now, you should be able to access /var/www/html directory as ~/public_html from your user’s home directory and make changes to the files and directories there as required.

When you’re done developing your website, you can secure the web root directory /var/www/html again as follows:

$ sudo chmod -R 660 /var/www/html

So, that’s how you configure a LAMP server on CentOS 8 for PHP web development. Thanks for reading this article.

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

Debian 10 (Buster) Net Install

This tutorial shows how to install Debian through the internet from a minimal installer called “Net Install”...
29/12/2020

Deploy Apache Kafka using Docker Compose

Microservice oriented design patterns have made our applications more scalable than ever. RESTful API servers, front-end...
29/12/2020

Libreboot T400 Tutorial

(This post contains affiliate links. It is a way for this site to earn advertising fees by advertising or linking to certain...
28/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