Install php 7.4 centos 7 nginx
IntroductionA LEMP software stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the ENginx web server (which replaces the Apache component of a LAMP stack). The site data is stored in a MySQL-based database, and dynamic content is processed by PHP. Show
In this guide, we’ll get a LEMP stack with PHP 7.4 installed on a CentOS 7 server, using MariaDB as the database management system. 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. PrerequisitesBefore you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-4 in the initial server setup for CentOS 7. Step 1 — Installing NginxIn order to display web pages to our site visitors, we are going to employ Nginx, a high-performance web server. To get the latest Nginx version, we’ll first install the EPEL repository, which contains additional software for the CentOS 7 operating system. To add the CentOS 7 EPEL repository, run the following command:
Since we are using a Now that the EPEL repository is installed on your server,
install Nginx using the following
Once the installation is finished, start the Nginx service with:
You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):
You will see the default CentOS 7 Nginx web page, which is there for informational and testing purposes. It should look something like this: If you see this page, then your web server is now correctly installed. To enable Nginx to start on boot, run the following command:
How To Find Your Server’s Public IP AddressIf you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH. From the command line, you can find this a few ways. First, you can use the
This will give you one or two lines back. They are both correct addresses, but your computer may only be able to use one of them, so feel free to try each one. An alternative method is to use an outside party to tell you how it sees your server. You can do this by asking a specific server what your IP address is:
Regardless of the method you use to get your IP address, you can type it into your web browser’s address bar to get to your server. Step 2 — Installing MariaDBNow that we have our web server up and running, it is time to install MariaDB, a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational database management system. Again, we can use
When the installation is complete, we need to start MariaDB with the following command:
Now that our MariaDB database is running, we want to run a security script that will remove some dangerous defaults and lock down access to our database. Start the interactive script by running:
The prompt will ask you for your current
For the rest of the questions, you should hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made. The last thing you will want to do is enable MariaDB to start on boot. Use the following command to do so:
At this point, your database system is now set up and we can move on. Step 3 — Installing PHPPHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display. The PHP version available by default within CentOS 7 servers is outdated, and for that reason, we’ll need to install a third-party package repository in order to obtain PHP 7+ and get it installed on your CentOS 7 server. Remi is a popular package repository providing the most up-to-date PHP releases for CentOS servers. To install the Remi repository for CentOS 7, run:
After the installation is done, you’ll need to run a command to enable the repository containing your preferred version of PHP. To check which PHP 7+ releases are available in the Remi repository, run:
You’ll see output like this:
In this guide, we’ll install PHP 7.4, which is currently the most updated stable version of PHP. To enable the correct Remi package to get PHP 7.4 installed, run:
Now we can proceed to use
To confirm that PHP is available as your chosen version, run:
You’ll see output like this:
PHP is now successfully installed on your system. Next, we need to make a few adjustments to the default configuration. To facilitate editing
files on CentOS, we’ll first install
Open the
Now look for the /etc/php-fpm.d/www.conf
You’ll notice that both the /etc/php-fpm.d/www.conf
Next, locate the /etc/php-fpm.d/www.conf
Finally, we’ll need to change the owner and group settings for the socket file we just
defined within the /etc/php-fpm.d/www.conf
Save and close the file when you’re done editing. If you are using To enable and start the
Your PHP environment is now ready. Next, we’ll configure Nginx so that it sends all requests for PHP scripts to be processed by Step 4 — Configuring Nginx to Process PHP PagesNow, we have all of the required components installed. The only configuration change we still need to do is tell Nginx to use our PHP processor for dynamic content. Nginx has a dedicated directory where we can define each hosted website as a separate configuration file, using a server block. This is similar to Apache’s virtual hosts. With the default installation, however, this directory is empty. We’ll create a new file to serve as the default PHP website on this server, which will override the default server block defined in the First, open a new file in the
Copy the following PHP server definition block to your configuration file, and don’t forget to replace
the /etc/nginx/conf.d/default.conf
Save and close the file when you’re done. Next, restart Nginx to apply the changes:
Your web server is now fully set up. In the next step, we’ll test the PHP integration to Nginx. Step 5 — Testing PHP Processing on your Web ServerNow that your web server is set up, we can
create a test PHP script to make sure Nginx is correctly handling Before creating our script, we’ll make a change to the default ownership settings on Nginx’s document root, so that our regular sudo user is able to create files in that location. The following command will change the ownership of the default Nginx 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.
We’ll now create a test PHP page to make sure the web server works as expected. Create a new PHP file called
The following PHP code will display information about the current PHP environment running on the server: /usr/share/nginx/html/info.php
When you are finished, save and close the file. Now we can 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
You’ll see a page similar to this: After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your CentOS
server. You can use
You can always regenerate this file if you need it later. ConclusionIn this guide, you’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Nginx as web server and the latest PHP release version. You’ve set up Nginx to handle PHP requests through How do I download PHP 7.4 on CentOS 7?How To Install PHP 7.4 on CentOS 7. Step 1: Add EPEL and REMI Repository. Run the commands below to add required repositories. ... . Step 2: Install PHP 7.4 on CentOS 7. We can now enable PHP 7.4 Remi repository and install PHP 7.4 on CentOS 7. ... . Step 3: Install PHP 7.4 Extensions.. Does Nginx install PHP?After the restart, PHP is fully enabled on Nginx. To prove this, create a PHP file in Nginx's /var/www/html folder and test to ensure the page renders properly on the server. This creates the most basic PHP file outside of a “Hello World” example you could create.
How connect PHP to Nginx?Connecting NGINX to PHP FPM
If you're using unix socket change fastcgi_pass to: fastcgi_pass unix:/var/run/php-fpm. sock; Restart NGINX.
Can I install Nginx on CentOS 7?Nginx is not available in the standard repositories that come with the CentOS package, so you will need to install the EPEL repository on your server. EPEL is free to use and provides numerous open-source packages to install with Yum.
|