Quick and dirty nginx installation on CentOS 7



CentOS 7 is a great Linux distribution but one thing it does lack is an official Nginx package in the default distribution mirrors. There are a few methods of working around this including the use of EPEL but for this tutorial I will use Nginx’s official repository for the installation.

Let’s get started!

Step 1.
Establishing a connection to the repository via yum is simple. simply create and edit a new repository file as shown below:

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

add the following:

name=nginx repo

Step 2.
Next we will install the nginx server by running the following yum installation command:

yum install nginx

You will have to acknowledge the install but you can make this silent by adding a -y to the installation command if you wish.

Step 3.
One the installation is completed you can start the nginx server by running the following command:

service nginx start

Step 4.
You will probably want nginx to auto start at boot time. In CentOS 7 you will use the following systemd command to do this:

systemctl enable nginx

Step 5.
You can verify it is properly enabled by running the following command in systemd:

systemctl is-enabled nginx

If the output says “enabled” then nginx is properly set to start on boot.

Here’s a few examples of useful commands to help you in the event you are having issues with your nginx server.

Check the sanity of a config file:

[example@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Check the status of the nginx server:

[example@proxy ~]# service nginx status
Redirecting to /bin/systemctl status nginx.service
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Sun 2015-02-15 11:15:22 CST; 25min ago
Docs: http://nginx.org/en/docs/
Main PID: 28703 (nginx)
CGroup: /system.slice/nginx.service
├─28703 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─28704 nginx: worker process

Feb 15 11:15:22 proxy.01.example.com nginx[28699]: nginx: the configuration...
Feb 15 11:15:22 proxy.01.example.com nginx[28699]: nginx: configuration fil...
Feb 15 11:15:22 proxy.01.example.com systemd[1]: Started nginx - high perfo...
Hint: Some lines were ellipsized, use -l to show in full.

In the event these two check out and it’s still not working you probably have a connectivity issue. The default policies on CentOS 7 have http blocked so that would be a great place to start assuming connectivity has been verified.

On a parting note one last step I would recommend when tidying up the install would be to disable the version header. You can edit the nginx.conf and add the following directive to do this:

server_tokens off;

This guide was meant to be very basic and I plan on doing a deeper dive into virtual hosts, proxies, and addition configurations in a future post.

Hope you enjoy!
– Dustin

A little script I wrote in PHP for GeoIP lookups


Being fully transparent I am a system admin by trade so my code is not the greatest. That being said, I figured I would release a little bit of PHP code I wrote for a tool I have created as it is useful for pulling GeoIP data and displaying it in HTML.  It uses PHP Curl, gethostbyname, JSON decode, and the Telize.com JSON GeoIP webservice.


// PHP GeoIP Lookup Script - 11/15/2014 by Dustin Larmeir (dustin@larmeir.com)
// Form Input

// Grabs the domain name via the input form and converts it to a IP address
$hostname = $_POST['INPUT'];
$ipaddress = gethostbyname($hostname);

// Grabs IP address from form (basic method)
$url = "http://www.telize.com/geoip/".$ipaddress;
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
// Closing

// get the results and json_decode the answer
$geoip_data = (json_decode($geoip_json, true));

// Echo output from JSON decoding and PHP arrays
echo "IP Address: "; echo($geoip_data["ip"]);
echo "Organization: "; echo($geoip_data["isp"]);
echo "City: "; echo($geoip_data[city]);
echo "State: "; echo($geoip_data[region]);
echo "Country: "; echo($geoip_data[country]);
echo "Time Zone: "; echo($geoip_data[timezone]);
echo "";