Creating a PHP Web Server Based on CentOS 8 with Docker
The problem is not as clear to me as it seems. I think it’s a php-fpm issue, but have no idea how to fix it. Looked this up, but all solutions seem complicated. Is there a simple way to tell PHP to work with the server in the Docker image?
I recently read about using process managers like supervisord where one needs to start several services per container. But, is it possible to start PHP-FPM in a simpler way inside the web container?
CentOS
My problem seems very similar to other ones around PHP installation one can find on the www but I tried dozens of solutions, none worked at the end.
A few days ago, I installed CentOS 8 (for the first time) on a Xeon server. I never encountered problems with CentOS 6 but now I’m stuck with this CentOS 8. The short version is : PHP is installed, enabled, works but does not read .php files as php (does it as html)
Here is what I did, step by step :
Installed Apache : sudo dnf install httpd
Installed Firewalld : sudo dnf install firewalld
sudo systemctl enable firewalld
sudo systemctl start firewalld
Checked Firewalld : sudo firewall-cmd —state
sudo firewall-cmd —permanent —add-service=https
sudo firewall-cmd —zone=public —add-port=80/tcp —permanent
sudo firewall-cmd —add-port=10000/tcp —zone=public —permanent
sudo firewall-cmd —zone=public —add-port=443/tcp —permanent
sudo firewall-cmd —reload
Installed Apache mod_ssl : sudo dnf install httpd mod_ssl
Started web server : sudo systemctl start httpd
sudo systemctl status httpd
Installed MySql : sudo dnf install mysql-server
sudo systemctl start mysqld.service
sudo systemctl status mysqld
sudo systemctl enable mysqld
sudo systemctl enable httpd
sudo mysql_secure_installation
Installed wget : sudo yum search wget
sudo yum install wget
Installed Webmin :
wget http://prdownloads.sourceforge.net/weba . noarch.rpm
sudo dnf install perl perl-Net-SSLeay openssl perl-Encode-Detect
sudo rpm -Uvh webmin-1.984-1.noarch.rpm
sudo systemctl enable webmin
At this point, I tried to access Webmin via ipaddress:80 , and it did not work, I did not notice that the previous command gave the (strange) address to use. So I did :
sudo iptables -I INPUT 1 -p tcp —dport 10000 -j ACCEPT
sudo service iptables save
sudo /etc/init.d/webmin restart
sudo firewall-cmd —reload
Installed Vim : sudo dnf search vim
sudo dnf install vim-enhanced
alias vi=’vim’
Configured Webmin : sudo /etc/webmin/miniserv.conf
At this point, I uninstalled Webmin & reinstalled it, found the right address to use.
I created Virtual Hosts
Then did : yum install epel-release
Installed Snapd : sudo dnf install snapd
sudo systemctl enable snapd.socket —now
Installed Certbot (SSL) : sudo snap install core; sudo snap refresh core
sudo dnf remove certbot
sudo yum remove certbot
sudo snap install —classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot —apache
Winter is coming : sudo yum module list php (php 7.2, 7.3, 7.4 installed but I did not even try to read a .php file)
I had to try to install PHP 5.6 for an old website
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo yum —enablerepo=remi install php56
sudo yum module list php (php 5.6 does not appear)
sudo yum search php (php 5.6 appears. )
sudo find / -name php.ini
I needed a recent version of PHP as well :
Php did not start. sudo systemctl restart httpd.service does not help.
Installed PHP 8.1 :
sudo yum —enablerepo=remi install php81
sudo dnf module list php
sudo dnf module enable php:remi-8.0 -y
sudo yum install php-cli
php -v (php now works in command line but does not read .php files)
Tried to edit /etc/httpd/conf.d/php81-php.conf
php -r ‘echo «Hello World!n»;’ (ok)
apachectl -M (no php module there)
yum install php
apachectl -M | grep -i php
Got «Could not reliably determine the server’s fully qualified domain name, using nsxxxxxx.ip-xx-xx-xx.eu. Set the ‘ServerName’ directive globally to suppress this message» , so I changed ServerName in httpd.conf, uncommented the line. No more error message
I tried to uninstall php : yum remove php , yum install php . now there is /etc/httpd/conf.d/php.conf
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
sudo yum module list php
sudo yum module reset php
sudo yum module enable php:remi-8.1
sudo systemctl reload httpd
yum list installed | grep php (I can see php 5.6 & 8.1)
yum -y remove php* (removed all versions)
And again : sudo yum —enablerepo=remi install php81
sudo yum install php-cli
php -v (php 8.1)
Restarted Apache, now I get 503 Service Unavailable when reading .php files. I did not find any working solution on the www.
yum -y remove php*
yum install php (php 8.1, restart Apache, still 503)
sudo dnf module list php (php 8.1 enabled)
sudo dnf module reset php (no more php enabled)
sudo dnf module enable php:7.2 (php 7.2 enabled but php -v gives 8.1)
Reinstalled Apache : sudo dnf remove httpd
sudo dnf install httpd
sudo dnf install httpd mod_ssl
sudo systemctl enable httpd
sudo dnf module reset php
sudo dnf module install php:remi-8.1
Recreated Virtual Hosts, certbot again : sudo certbot —apache
sudo systemctl start httpd
sudo dnf module list php gives :
Last metadata expiration check: 2:04:29 ago on Thu 27 Jan 2022 09:00:13 PM UTC.
CentOS Linux 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
Remi’s Modular repository for Enterprise Linux 8 — x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 [e] common [d] [ i], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [ i]nstalled
Now php 8.1 is enabled & installed. but still does not read .php files (readed as html)
apachectl -M (or) apachectl -M | grep -i php (no module php)
sudo journalctl -xe
sudo vi /etc/httpd/conf.modules.d/00-base.conf (via Webmin)
LoadModule rewrite_module modules/mod_rewrite.so (is ok)
sudo dnf install php php-cli php-common
At this point I tried to change the files rights, owner & place (/home/website or /var/www/)
I tried all solutions I found. I am really stuck. What did I miss ? Any idea?
Many thanks in advance. Cheers!
How To Install Linux, Apache, MariaDB, PHP (LAMP) Stack on CentOS 8 [Quickstart]
In this tutorial, you’ll install a LAMP stack on a CentOS 8 server. Although MySQL is available from the default repositories in CentOS 8, this guide will walk through the process of setting up a LAMP stack with MariaDB as the database management system.
For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on CentOS 8.
Prerequisites
To follow this guide, you’ll need access to a CentOS 8 server as a sudo user.
Step 1 — Install Apache
Install the httpd package with:
After the installation is finished, run the following command to enable and start the server:
If firewalld is active, you’ll need to run the following command to allow external access on port 80 (HTTP):
Reload the firewall configuration so the changes take effect:
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. You’ll see a page like this:
Step 2 — Install MariaDB
We’ll now install MariaDB, a community-developed fork of the original MySQL server by Oracle. To install this software, run:
When the installation is finished, enable and start the MariaDB server with:
To improve the security of your database server, it’s recommended that you run a security script that comes pre-installed with MariaDB. Start the interactive script with:
The first prompt will ask you to enter the current database root password. 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.
Step 3 — Install PHP
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
Step 4 — Test 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. The following command will change the ownership of the default Apache document root to a user and group called sammy :
We’ll now create a test PHP page to make sure the web server works as expected. First, you might want to install nano , a more user-friendly text editor, since that doesn’t come installed with CentOS 8 by default:
Now, create a new PHP file called info.php at the /var/www/html directory:
The following PHP code will display information about the current PHP environment running on the server:
/var/www/html/info.php
phpinfo();
When you are finished, save and close the file.
To test whether our web server can correctly display content generated by a PHP script, go to your browser and access your server hostname or IP address, followed by /info.php :
http://server_host_or_IP/info.php
You’ll see a page similar to this:

Related Tutorials
Here are links to more detailed guides related to this tutorial:
- Initial Server Setup on CentOS 8
- How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on CentOS 8
- Managing DNS records on DigitalOcean
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Installing and Configuring FastCGI and PHP-FPM on CentOS 8
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit .
This credit will be applied to any valid services used during your first days.
mod_fcgid is an Apache module that uses the FastCGI protocol to provide an interface between Apache and Common Gateway Interface (CGI) programs. CGI helps a web server handle dynamic content generation and processing for scripting languages like PHP. This dynamic functionality is commonly used when running content management systems like WordPress on a LAMP stack.
This guide will show you how to install mod_fcgid and PHP-FPM on CentOS 8. It will also provide a basic configuration that uses socket based connections, instead of TCP. These steps will enable you to run PHP through mod_fcgid . Running PHP through mod_fcgid helps to reduce the amount of system resources used by forcing the web server to act as a proxy and only pass files ending with the .php file extension to PHP-FPM. Additionally, using PHP-FPM allows each virtual host to be configured to run PHP code as individual users.
This guide assumes that you are familiar and comfortable with setting up a LAMP stack on CentOS 8. If you are new to Linux server administration, you may be interested in reading our Linux System Administration Basics guide.
Before You Begin
- Complete the steps in the How to Install a LAMP Stack on CentOS 8 guide. After completing the LAMP stack guide, you should have an Apache virtual hosts configuration for your own website. This guide will continue to refer to the site as example.com .
This guide’s examples will use PHP version 7.2. By default, PHP 7.2 is available for installation from the default CentOS 8 repositories. When running commands related to PHP, ensure you replace any version numbers with your own system’s PHP version.
Install mod_fcgid and PHP-FPM
In this section, you will install the mod_fcgid and PHP-FPM modules on your CentOS 8 Linode.
-
Update your system if you have not already done so and install the wget command line utility.
sudo yum update && sudo yum install wget -y
sudo yum install mod_fcgid php-fpm
sudo httpd -t
sudo systemctl restart httpd
Configure Apache with PHP-FPM
You will now configure Apache to pass all requests for files with the .php extension, to the PHP wrapper through FastCGI.
-
Configure PHP-FPM to use UNIX sockets instead of TCP. In this command, you will use grep to determine if the sockets are already being used. This command will search your php-fpm installation’s default pool configuration file for the setting.
sudo grep -E '^\s*listen\s*=\s*[a-zA-Z/]+' /etc/php-fpm.d/www.conf
You should see the following output:
listen = /var/run/php-fpm/www.sock
