(CentOS 7) Install PHP7.4
1. Install the EPEL package:
yum -y install epel-release
2. Install remi source (after installation, there will be php source under /etc/yum.repos.d/):
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
3. Install the yum extension package:
yum -y install yum-utils
4. Install and install extensions
yum -y --enablerepo=remi install php74-php php74-php-fpm php74-php-bcmath php74-php-ctype php74-php-curl php74-php-dom php74-php-gd php74-php-hash php74-php-iconv php74-php-intl php74-php-mbstring php74-php-pdo_mysql php74-php-simplexml php74-php-soap php74-php-xsl php74-php-zip php74-php-xml php74-php-sockets php74-php-session php74-php-snmp php74-php-mysql php74-php-cli php74-php-json php74-php-mcrypt php74-php-mysqlnd php74-php-pecl-crypto php74-php-pecl-mcrypt php74 -php-pecl-geoip php74-php-recode php74-php-xmll
Restart your computer, otherwise you may not be able to start php
reboot
5. Start and add startup
systemctl start php74-php-fpm systemctl enable php74-php-fpm
Setting up PHP
1. View the PHP installation package
[root@iZ0jl3k2hw9j986aenvjwpZ html]# rpm -qa | grep php php74-php-cli-7.4.33-8.el7.remi.x86_64 php74-php-pecl-mysql-1.0.0-0.23.20190415.d7643af.el7.remi.x86_64 php74-php-pecl-crypto-0.3.2-1.el7.remi.x86_64 php74-php-pecl-zip-1.22.2-1.el7.remi.x86_64 php74-php-sodium-7.4.33-8.el7.remi.x86_64 php74-php-mysqlnd-7.4.33-8.el7.remi.x86_64 php74-php-snmp-7.4.33-8.el7.remi.x86_64 php74-php-7.4.33-8.el7.remi.x86_64 php74-php-intl-7.4.33-8.el7.remi.x86_64 php74-php-bcmath-7.4.33-8.el7.remi.x86_64 php74-php-mbstring-7.4.33-8.el7.remi.x86_64 php74-php-fpm-7.4.33-8.el7.remi.x86_64 php74-php-pecl-geoip-1.1.1-11.el7.remi.x86_64 php74-php-pecl-recode-1.0.0~DEV.20190723-6.el7.remi.x86_64 oniguruma5php-6.9.9-1.el7.remi.x86_64 php74-runtime-7.4-3.el7.remi.x86_64 php74-php-common-7.4.33-8.el7.remi.x86_64 php74-php-pdo-7.4.33-8.el7.remi.x86_64 gd3php-2.3.3-7.el7.remi.x86_64 php74-php-gd-7.4.33-8.el7.remi.x86_64 php74-php-pecl-mcrypt-1.0.6-1.el7.remi.x86_64 php74-php-xml-7.4.33-8.el7.remi.x86_64 php74-php-soap-7.4.33-8.el7.remi.x86_64 php74-php-json-7.4.33-8.el7.remi.x86_64
2. Find: php74-php-fpm-7.4.24-1.el7.remi.x86_64 installation location
[root@iZ0jl3k2hw9j986aenvjwpZ html]# rpm -ql php74-php-fpm-7.4.33-8.el7.remi.x86_64 /etc/logrotate.d/php74-php-fpm /etc/opt/remi/php74/php-fpm.conf /etc/opt/remi/php74/php-fpm.d /etc/opt/remi/php74/php-fpm.d/www.conf /etc/opt/remi/php74/sysconfig/php-fpm /etc/systemd/system/php74-php-fpm.service.d /opt/remi/php74/root/usr/sbin/php-fpm /opt/remi/php74/root/usr/share/doc/php74-php-fpm-7.4.33 /opt/remi/php74/root/usr/share/doc/php74-php-fpm-7.4.33/php-fpm.conf.default /opt/remi/php74/root/usr/share/doc/php74-php-fpm-7.4.33/www.conf.default /opt/remi/php74/root/usr/share/fpm /opt/remi/php74/root/usr/share/fpm/status.html /opt/remi/php74/root/usr/share/licenses/php74-php-fpm-7.4.33 /opt/remi/php74/root/usr/share/licenses/php74-php-fpm-7.4.33/fpm_LICENSE /opt/remi/php74/root/usr/share/man/man8/php-fpm.8.gz /usr/lib/systemd/system/php74-php-fpm.service /var/opt/remi/php74/lib/php/opcache /var/opt/remi/php74/lib/php/session /var/opt/remi/php74/lib/php/wsdlcache /var/opt/remi/php74/log/php-fpm /var/opt/remi/php74/run/php-fpm
3. Find the location of php.ini:
[root@iZ0jl3k2hw9j986aenvjwpZ ~]# find /etc/opt/remi/php74 -name php.ini /etc/opt/remi/php74/php.ini
4. Edit /etc/opt/remi/php74/php.ini and replace ;cgi.fix_pathinfo=1 with cgi.fix_pathinfo=0;
[root@iZ0jl3k2hw9j986aenvjwpZ ~]# vim /etc/opt/remi/php74/php.ini cgi.fix_pathinfo=0
5. Restart php74-php-fpm
systemctl restart php74-php-fpm
6. More operations
systemctl restart php74-php-fpm #Restart systemctl start php74-php-fpm #Start systemctl stop php74-php-fpm #Close systemctl status php74-php-fpm #Check status
Set PHP soft links and environment variables
1. Establish a soft connection to the php program
ln -s /opt/remi/php74/root/usr/bin/php /usr/bin/php
2. Modify the rc file
1. Environment variables of the current user
#If you are using bash as the shell vim ~/.bashrc #Add a line of environment variables export PATH=/opt/remi/php74/root/usr/bin:$PATH #Refresh the environment variables source ~/.bashrc
2. Modify global environment variables
vim /etc/profile export PATH=/opt/remi/php74/root/usr/bin:$PATH
3. View PHP
Verify whether the installation is successful:
[root@iZ0jl3k2hw9j986aenvjwpZ ~]# php -v PHP 7.4.33 (cli) (built: Aug 1 2023 08:30:09) (NTS) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
4. If you are running nginx instead of apache, modify
vi /etc/opt/remi/php74/php-fpm.d/www.conf user=apache group=apache
change into
user = nginx group=nginx
5. Enable php session permissions for nginx
cd /var/opt/remi/php74/lib/php/ #Enter the php directory chown -R nginx:nginx session #Enable nginx to save session permissions
Configuration file directory
/opt/remi/php74/root/usr/bin/php-config /var/opt/remi/php74/lib/php/session /etc/opt/remi/php74/php.ini /etc/opt/remi/php74/php-fpm.d/www.conf
uninstall
yum remove php74*
—————————Test your own php page access————– —————
[root@iZ0jl3k2hw9j986aenvjwpZ ~]# cd /var/www/html/ [root@iZ0jl3k2hw9j986aenvjwpZ html]# vim phpinfo.php <?php phpinfo(); ?>
Use a browser to access and test the php function
If you cannot access it, there are several solutions:
- Check if PHP file exists
In case the PHP file is inaccessible, one step is to ensure that the PHP file exists in the specified location. Sometimes, we may not be able to find the required PHP file in a specific directory. At this time, we need to ensure that the PHP file is placed in the correct directory and the file name must be correct.
- Check PHP file permissions
Each server has unique permission settings that may prevent any access to PHP files. If you have a strict server setup, you may need to manually change the permissions on the files to allow the web server to access these PHP files. You can change file permissions using the CHMOD command. Granting 644 permissions to PHP files is a common practice. For example, change the file permissions to ‘chmod 644 /var/www/html/index.php’.
- Check if the web server is configured correctly
If you have tried the above two methods and still cannot access the PHP file, you need to check whether the web server is configured correctly to handle PHP files. Configuring your web server to correctly interpret PHP files is very important for running PHP code. For the Apache server, you need to check whether the PHP module is enabled and the php.ini file is configured correctly. For the Nginx server, you need to check whether the PHP interpreter is installed correctly and the corresponding php-fpm is configured. If you are not familiar with the server, you can check the server provider’s documentation or ask professionals to help.
- Check your code for errors
If your web server is configured correctly to handle PHP files, but you still can’t access them, you may want to take a closer look at your PHP code for syntax errors or logic errors. It’s better to fix the PHP file before re-uploading it. If you can’t find any errors, try opening the PHP error log when accessing the PHP file so you can identify the problem. Logging the error message in the file will help you better understand the possible problem without more information.
If your server denies access to PHP files, there are many ways to fix the problem. Before implementing these solutions, we recommend looking at the following points: check if the file exists, check file permissions, check if the web server is configured correctly, check for bad code. We hope this article can help you solve this common problem so that you can better develop PHP programs and get better results.