Setting Html Nginx Centos 7


Setting HTML Nginx Centos 7

Overview

This tutorial provides instructions on setting up an Nginx server to host HTML content on a CentOS 7 machine. It covers installing and configuring Nginx, installing and configuring a database server, configuring PHP to run scripts, and setting up the necessary file permissions for the web server to access the files and directories. It also provides instructions for setting up secure access to the Nginx web server.

Nginx is a light-weight web server that is fast, secure, and easy to configure. It is typically used as a reverse proxy server, meaning that it is used to send requests from the outside world to other web servers or applications running on the same machine. Nginx can also be used as a standalone web server to serve up static HTML files.

Nginx is available on many operating systems, including CentOS. This tutorial will provide instructions for setting up the Nginx web server on a CentOS 7 machine to serve HTML content.

Install Nginx

The first step is to install Nginx. Nginx is available from the CentOS repositories. To install it, use the following command:

$ sudo yum install nginx

This will download and install the latest version of Nginx from the CentOS repository. Once the installation is complete, you can start and enable the Nginx service with the following commands:

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

At this point, Nginx should be up and running. You can verify this with the following command:

$ systemctl status nginx

If Nginx is running, you should see output similar to the following:

● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-04-30 06:37:06 EDT; 1h 26min ago
Main PID: 437 (nginx)
CGroup: /system.slice/nginx.service
└─437 nginx: master process /usr/sbin/nginx -c
/etc/nginx/nginx.conf

If you see output similar to this, it means that Nginx is installed and running correctly.

Configuring Nginx for HTML Content

Now that Nginx is installed, the next step is to configure it to serve up HTML content. The main Nginx configuration file is located at /etc/nginx/nginx.conf. This is the file that you will need to modify in order to configure Nginx to serve HTML content.

In order to configure Nginx, you will need to add a block of code to the nginx.conf file. This block of code should look something like this:

server {
listen 80;
server_name example.com;

location / {
root /usr/share/nginx/html;
index index.html;
}
}

This block tells Nginx to listen on port 80 (the default HTTP port) and to serve up content from the /usr/share/nginx/html directory. This directory is the default location for the HTML files that you want Nginx to serve. You can change the root directory to any location that you want, as long as Nginx has access to it.

Now that you have configured Nginx to serve up HTML content, you should restart Nginx for the changes to take effect. This can be done with the following command:

$ sudo systemctl restart nginx

At this point, Nginx should be configured and ready to serve HTML content. You can test this by creating a file in the root directory (the one specified in the nginx.conf file) and then accessing it from a web browser.

Configure PHP

If you will be running scripts that use PHP, you will need to install and configure a PHP interpreter. PHP is not installed by default on CentOS 7, so you will need to install it. This can be done with the following command:

$ sudo yum install php-fpm

Once PHP is installed, you will need to modify the Nginx configuration file. Find the block of code that you added earlier and add the following lines to it:

location ~ .php$ {
root /usr/share/nginx/html;
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}

This tells Nginx to pass any requests for files ending in .php to the PHP interpreter.

Now that you have configured PHP, you will need to start and enable the php-fpm service. This can be done with the following commands:

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

At this point, PHP should be working correctly. You can test this by creating a file in the root directory (the one specified in the nginx.conf file) and then accessing it from a web browser.

Configure File Permissions

In order for the web server to access the files and directories that it needs to, we need to configure the correct file permissions. The web server runs as the user “nginx”. Therefore, we need to give this user read and execute permissions on the files and directories that it needs to access.

The following command will give the nginx user read and execute permissions on all files and directories under the /usr/share/nginx/html directory:

$ sudo chmod -R 755 /usr/share/nginx/html

This command should be run on any files or directories that the web server needs to access.

Now that you have configured the correct file permissions, you should restart Nginx for the changes to take effect. This can be done with the following command:

$ sudo systemctl restart nginx

At this point, Nginx should be configured and ready to serve content.

Secure Access

In order to secure access to the web server, we need to enable HTTPS. HTTPS is the secure version of HTTP and it allows us to securely transfer data over the internet.

In order to enable HTTPS, we need to generate a “certificate signing request” (CSR). This CSR will be used to generate a SSL certificate that will be used to secure the web server.

There are a number of tools available for generating a CSR, including OpenSSL. To generate a CSR with OpenSSL, use the following command:

$ openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr

This command will generate two files – a key file and a CSR file. The key file will be used to secure the web server, and the CSR file will be used to request a certificate from a Certificate Authority (CA).

Once you have generated the CSR file, you will need to use it to request a certificate from a CA. There are a number of different CAs available, such as Comodo, Thawte, and GlobalSign.

Once you have obtained the certificate, you need to add it to the Nginx configuration file. To do this, add the following lines to the nginx.conf file:

ssl_certificate  /path/to/yourdomain.crt;
ssl_certificate_key /path/to/yourdomain.key;

Once you have added the lines, restart Nginx for the changes to take effect. This can be done with the following command:

$ sudo systemctl restart nginx

At this point, Nginx should be configured to use HTTPS. You can verify this by accessing the web server from a web browser and checking for the lock icon in the address bar.

Conclusion

In this tutorial, we have covered the steps necessary for setting up an Nginx web server to serve

Leave a Reply

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