Centos 7 Install Nginx Php 7


Centos 7 Install Nginx Php 7

Nginx Server Overview and Prerequisites

Nginx is a highly popular open source web server and reverse proxy software, known for its scalability and performance in the face of increased web traffic. It is used for the hosting of websites, web applications, and other services. The Nginx server is easy to install, configure and make ready for use to serve websites and web services.

Before you start setting up Nginx on your Centos 7 server, you need to ensure that you have the necessary prerequisites such as an up-to-date Centos 7 server with a valid domain name and a static IP address. You should also make sure that your server is properly configured with a suitable firewall. Finally, you’ll need to set up some additional packages such as PHP, MySQL and more.

Installing and Configuring Nginx

Once the prerequisites are taken care of, you’re now ready to proceed with the installation and configuration of Nginx. To install Nginx on our Centos 7 system, we’ll be using the yum package manager. Start off by adding the Nginx software repository that contains all the packages needed for the installation.

Run the following command to add the Nginx repository:

sudo yum -y install epel-release yum-utils

Then, you can use the yum-config-manager to add the Nginx repository with the following command:

sudo yum-config-manager –add-repo https://nginx.org/packages/centos/7/x86_64/

Next, to install Nginx, run the following command:

sudo yum -y install nginx

Once you’ve installed Nginx, you’re now ready to proceed with its configuration. To do this, you’ll need to edit the Nginx configuration file, which is located at /etc/nginx/nginx.conf. This can be done by running the following command:

sudo vi /etc/nginx/nginx.conf

Edit the server section to look like the following:

server {
listen 80 default_server;
server_name your_domain_name_or_IP;
root /var/www/html;
index index.html index.htm;
}

Save and exit the file. Now, start and enable Nginx with the following commands:

sudo systemctl start nginx
sudo systemctl enable nginx

Configuring the Firewall

To make sure that Nginx is accessible from outside our local network, we need to configure the firewall to allow incoming requests on port 80. To do this, run the following command:

sudo firewall-cmd –permanent — add-service=http

Once this is done, you’ll need to reload the firewall for the changes to take effect. To do this, run the following command:

sudo firewall-cmd –reload

You can then go ahead and test if Nginx is working by running the following command:

sudo systemctl status nginx

Installing and Configuring PHP 7

Now, the next step is to install and configure PHP. To install PHP 7, we’ll need to add some additional repositories to our system. Run the following command to add the EPEL repository:

sudo yum -y install epel-release

Then, run the following command to add the Webtatic repository:

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Now, install PHP 7 with all its necessary modules by running the following command:

sudo yum -y install php70w php70w-common php70w-opcache php70w-fpm php70w-gd php70w-mysql php70w-mcrypt

Once the installation is completed, start and enable PHP-FPM by running the following commands:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Configuring Nginx and PHP-FPM

With Nginx and PHP-FPM now installed and running, the next step is to configure them to work together. This can be done by running the following command:

sudo vi /etc/nginx/nginx.conf

Add the following lines at the bottom of the file:

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

Save and exit the file. Now, restart Nginx for the changes to take effect by running the following command:

sudo systemctl restart nginx

Testing Nginx and PHP-FPM

To test whether Nginx and PHP-FPM are working correctly together, create a test PHP file with the following contents:

sudo vi /var/www/html/info.php

phpinfo();
?>

Save and close the file. Then, open your web browser and navigate to http://your_domain_or_IP/info.php. You should see the PHP info page, which confirms that Nginx and PHP-FPM are working correctly together.

FAQs

Q) How to install Nginx on Centos 7?

A) To install Nginx on your Centos 7 server, you need to add the Nginx software repository that contains all the packages needed for the installation. Then, you can use the yum package manager to install Nginx with the following command:

sudo yum -y install nginx

Q) How to configure a firewall to allow Nginx requests?

A) To make sure that Nginx is accessible from outside our local network, we need to configure the firewall to allow incoming requests on port 80. To do this, run the following command:

sudo firewall-cmd –permanent — add-service=http

Q) How to install PHP 7 on Centos 7?

A) To install PHP 7 on your Centos 7 system, you’ll need to add some additional repositories to your system. Then, use the yum package manager to install PHP 7 with all its necessary modules by running the following command:

sudo yum -y install php70w php70w-common php70w-opcache php70w-fpm php70w-gd php70w-mysql php70w-mcrypt

Conclusion

In this article, we’ve gone through the process of installing and configuring Nginx and PHP 7 on a Centos 7 server. We’ve also covered how to configure the firewall to allow incoming requests on port 80. We hope you’ve found this article helpful.

Thank you for reading this article. Please read other articles on this website as well.

Leave a Reply

Your email address will not be published. Required fields are marked *