I have installed a CentOS 8 server on a virtualbox.
I need to run a hello world page in PHP on it.
How can I install Apache and PHP for my purpose?
The first step is to install Apach
sudo dnf install httpd
then activate the Apache service
sudo systemctl start httpd
and verify it is working
sudo systemctl status httpd
The next step is to install PHP
sudo dnf install php php-fpm php-common php-cli
Verify the installed PHP version
php --version
The next step is to configure your PHP.
Enable your short tags. For this purpose, find short_open_tag
inside your /etc/php.ini
and set it to On.
short_open_tag = On
Alternatively, you can run this bash command:
sed -i -e 's/short_open_tag = Off/short_open_tag = On/g' /etc/php.ini
If you are not sure where your php.ini
is located or which .ini
file will be loaded, you can run the following command:
php-fpm -i | grep ini
Enable PHP-fpm
sudo systemctl enable php-fpm sudo systemctl start php-fpm sudo systemctl status php-fpm
Setup the website folders
sudo mkdir -p /var/www/my-site-example/html sudo mkdir -p /var/www/my-site-example/log sudo chown -R $USER:$USER /var/www/my-site-example/html sudo chmod -R 755 /var/www
Write your site:
<?php phpinfo();
Create sites-available
and sites-enabled
folders:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Add a new line to your Apache config:
IncludeOptional sites-enabled/*.conf
Create your site config in the sites-available
folder:
<VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/my-site-example/html <Proxy "unix:/var/run/php-fpm/www.sock|fcgi://php-fpm"> ProxySet disablereuse=off </Proxy> <FilesMatch \.php$> SetHandler proxy:fcgi://php-fpm </FilesMatch> </VirtualHost>
Activate your site config
sudo ln -s /etc/httpd/sites-available/my-site-example.conf /etc/httpd/sites-enabled/my-site-example.conf
Restart Apache
sudo systemctl restart httpd sudo systemctl status httpd
Adjust SELinux:
sudo setsebool -P httpd_unified 1 sudo setbool httpd_can_network_connect 1 sudo setbool httpd_can_network_connect_db 1
Adjust the firewall
sudo firewall-cmd --list-all sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
If you face with any problem during the setup, first have a look at the virtualhosts that are currently running via this command:
sudo httpd -S
It may help you finding out what is wrong
To find php-fpm
errors, you may have a look at the logs:
sudo tail /var/log/php-fpm/www-error.log