Nginx on Centos 7

Nginx on Centos 7

Download Latest Nginx


To install Nginx’s mainline repository in CentOS 7, run commands below to create a Nginx’s repository on your system

vi /etc/yum.repos.d/nginx.repo

Then copy and paste the lines below into the file and save it.

name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

To install Nginx CentOS 7 after adding the new repository, run the commands below.

yum update
yum install nginx

Enabling

Nginx does not start on its own. To get Nginx running, type:

systemctl start nginx
systemctl enable nginx

Firewall

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Create some users

Its safer to create another user to own the directories which is not the owner of the Nginx process.

groupadd www-data
useradd -g www-data -s /sbin/nologin -M www-data

SELinix

If the port or top level directory is changed then the following lines need to be added.

setsebool httpd_can_network_connect on –P
chcon -Rt httpd_sys_content_t /var/www/wordpress
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/wordpress(/.*)?"
restorecon –R –v /var/www/wordpress

Basic Setup

/etc/nginx/conf.d/default.conf

server {
  listen       80;
  server_name  localhost;

  access_log  /var/log/nginx/access.log  main;

        root /var/www/;
        index index.html index.htm index.php;

        location / {
                try_files $uri $uri/ /index.html index.php;
        }

  fastcgi_buffers 16 16k;
  fastcgi_buffer_size 32k;

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }
}