Friday, January 29, 2016

Install nginx on Oracle Linux

When you like to run nginx as a webserver, which makes very much sense if you like to run a high performance webserver which is at the same time not taking up much resources, you will have to do some additional tasks. As you might find out quickly is that Oracle is not including nginx in the mainstream repository for Oracle Linux

However, you can add the nginx repository to your local yum configuration as an additional repository. You simply need to create a new file in /etc/yum.repos.d and call it (in our case) nginx.repo 

you can use a simple touch command to do so:
touch /etc/yum.repos.d/nginx.repo

Now you will have to add the below to the content of the file. This will ensure that the nginx repository is now part of the repositories that are available when you use yum, 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/$basearch/
gpgcheck=0
enabled=1

after saving the file a simple yum command will ensure that nginx will be installed in your Oracle Linux system, shown below.
yum install nginx

One a number of things to remember are that, when you use a standard Oracle Linux 7 installation the standard implementation of the firewall will block port 80 for external traffic. You will have to disable the firewall or open the port. next to this you have to be aware that nginx will not be configured to start automatically when the system boots and that nginx will not be started directly after installation.

If you check the status of nginx (as shown below) you will see it is down:

[root@localhost ~]# systemctl status nginx
nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: inactive (dead)
     Docs: http://nginx.org/en/docs/

[root@localhost ~]# 

you are able to start nginx by executing: 
systemctl start nginx.service

the will ensure that the service is now up and running. If you would now check the status of nginx via systemctl you will note the following:

[root@localhost ~]# systemctl status nginx
nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: active (running) since Tue 2015-12-29 01:32:14 CET; 11s ago
     Docs: http://nginx.org/en/docs/
  Process: 2285 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 2284 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 2287 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2287 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─2289 nginx: worker process

Dec 29 01:32:14 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Dec 29 01:32:14 localhost.localdomain nginx[2284]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 29 01:32:14 localhost.localdomain nginx[2284]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 29 01:32:14 localhost.localdomain systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Dec 29 01:32:14 localhost.localdomain systemd[1]: Started nginx - high performance web server.
[root@localhost ~]# 

Even though you now have a running nginx service on your Oracle Linux 7 machine you can note from the above that it is still marked as disabled. Note the last part of the loaded line in the status output which states disabled. This indicates that when the system will be (re-)booted that nginx will not be started automatically. To make sure nginx is started each time the server is rebooted you will have to make sure it is enabled. you can enable this by executing the below shown systemcelt enable command for the nginx.service which is in essence “ nothing more” then creating a link.

[root@localhost ~]# systemctl enable nginx.service
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'
[root@localhost ~]# 

Now you will have a running nginx http server that will start each time you will reboot your machine and you are ready to configure nginx to be used. A basic configuration can be found in /etc/nginx/nginx.conf editing this file is however not best practice. nginx.conf makes of a include /etc/nginx/conf.d/*.conf statement. This means that it is a better way to edit the file *.conf files or add *.conf files in this directory rather then editing the main nginx.conf file. 




No comments: