Editor's note: This post was originally published on April 7, 2015 and updated on April 5, 2018.
So you've just gotten yourself new self-managed server. Congratulations on a wise decision!
In this article we’re going to look at disabling Apache, then installing & activating NGINX on a new server. It assumes a certain level of previous server administration knowledge, or at least a willingness to learn.
NOTE: This article is not meant to serve as a guide for migrating existing sites on a production server from Apache to NGINX. Please keep that in mind when following the steps below, as one of the steps we'll take is turning Apache off. Doing so on a server with live production sites will bring sites down until they are reconfigured for NGINX.
What is NGINX?
NGINX (pronounced “Engine Ex”) was released for production in 2004 and is rapidly becoming a popular alternative to the traditional Apache web server. It features an event-driven design, which can make better use of today’s computer hardware than Apache’s process-driven design.
Because of this, NGINX is often seen as the "faster" alternative to Apache, being able to handle a higher load of concurrent connections while using less resources.
There are many comparisons out there between Apache and NGINX; we'll leave the debate up to the community. But here are a few pointers that outline the key reasons to choose Apache versus NGINX.
Is NGINX or Apache right for you?
If you are...
- Using the server to host a single website with high traffic
- Comfortable with advanced configuration and tweaking, and have the skillset to do so
- Wanting to work with newer web development frameworks
- Wanting to use an alternative to CGI/FastCGI, such as WSGI
- Are OK with less add-ons, components, or modules
- Are OK with a more complex configuration
... then NGINX might be a good fit for you.
If you are …
- Using traditional MySQL/PHP applications, such as WordPress or Drupal
- Planning to host many websites with different configurations per site through an .htaccess
- Are more comfortable with a platform that is very well known and documented
- Want access to a variety of different modules, add-ons, and components
- Want your web server to work right out of the box
... then you probably want to stick with Apache.
Here's a good rule of thumb: If you want to run ONE site at lightning speed on an advanced configuration, NGINX is probably the server for you. If you want to run MANY sites with easy configuration and flexibility, Apache is still your bread and butter.
At the end of the day, both NGINX and Apache are a good fit for most sites. Apache is included with all major Linux distributions and requires much less configuration. However, most benchmarks have clocked NGINX at serving websites faster. You can also see some configurations that run both — it's all up to you as the admin.
At the end of the day, the choice of the web server platform is entirely dependent on what you're doing with the server.
Before we begin...
Before we begin, let's make sure we have everything we need:
An active CentOS 7 server. If you’re working on a lightweight project or are just getting started, we recommend going with a GoDaddy VPS plan. If you’re looking for more control and power, a Dedicated server plan might be a better fit.
A domain. We will, of course, need to tie a domain to the NGINX web server, so we'll need a domain to use. All of our examples will use the domain yourdomain.com. When going through the article, replace any instance of yourdomain.com with the domain you want to use for your site.
Ability to connect via SSH. Use an SSH client such as PuTTY or PowerShell (for Windows) or Terminal (Mac). You can find instructions here. If this is step is a challenge, this article might not be for you, since most of the work we’re doing is in the command line.
A browser window open to your search engine of choice. NGINX has a lot of custom tinkering that you might need to do according to your needs, and other variables that this article not account for. But this shouldn't phase you — search engines are your ally.
If all of these elements are in place, we're ready for departure. Let's go!
Step 0: Connect to your server
Log into your server via SSH, then get to the root user by running:
sudo su -
Optional: Install the nano text editor
If you're not a fan of VIM, you may find the nano editor easier to work with. If it’s not installed on your server, install it with:
yum install nano
This’ll make editing your configuration files easier.
Step 1: Turn off Apache
Every major Linux distribution comes packages with Apache by default. It’s integrated into the OS, similar to how Windows comes packaged with IIS.
Since we are setting up a dedicated space for NGINX, it’s possible that the existing Apache configuration can cause problems when NGINX is activated. So we’re going to turn Apache off, then configure Apache so that it does not start upon server reboot.
Note: Turning Apache off on a server with live sites will bring those sites down. Act accordingly!
Shut down Apache. Remember: This will bring down any current websites that are hosted on the server.
service httpd stop
Now we need to remove Apache from the boot cycle, so that it doesn't try to start up during server boot:
systemctl disable httpd
Apache is now fully shut down, and won't be starting up again until we say so.
NOTE: If you have buyers remorse later on about NGINX, and want Apache to start on boot again, you can easily correct this previous command by running:
systemctl enable httpd
Step 2: Install NGINX
Now that we’ve disabled Apache, we can start our NGINX installation.
First, we need to add the CentOS EPEL package so that we can install NGINX:
yum install epel-release
Now that our repository is installed on the server, we can now use yum to install NGINX, like so:
yum -y install nginx
Start NGINX:
service nginx start
What If you see a “test failed” error message for nginx.conf?
You may be dealing with an IP address issue. By default, NGINX listens for both IPv4 and IPv6. If your server doesn’t support IPv6, however, the test will fail.
You can fix this by opening up the configuration file:
nano /etc/nginx/nginx.conf
Comment out the following line:
listen [::]:80 default_server;
So it looks like this:
# listen [::]:80 default_server;
Close and save the file, then try to start the service again:
service nginx start
If you don’t see any errors, you’re good to go.
Configure the server to start NGINX upon reboot:
systemctl enable nginx
You should now be able to see an NGINX test page by going to your server’s IP address in your browser.
Step 3: Configure NGINX to serve for your domain
Alrighty, we've switched from the Apache schooner to the NGINX steamboat. Now it's time to get it working for your domain.
Let’s create a new directory for the site DocumentRoot.
It is a good idea to follow a standard naming convention if you are hosting multiple websites.
We'll follow the standard used by cPanel,mk and make our DocumentRoot based on the name public_html, like so:
mkdir -p /var/www/yourdomain.com/public_html
Let's create a test index.html in this directory so that we have something to look at when we test the configuration later:
nano /var/www/yourdomain.com/public_html/index.html
All we need is a simple line of text to show that the connection is working:
Hello world!
Close and save the index.html file.
We need to set permissions for this folder so that it can be viewed by the outside world:
chmod 755 /var/www/yourdomain.com/public_html
Our directory is now set up, and we have a test index.html waiting to be viewed.
Step 4: Configure NGINX to recognize server blocks
We can host multiple websites on a single Apache server by using Virtual Hosts. This effectively acts as routing instructions that points a domain to the appropriate directory on the server.
With NGINX, this sort of routing is handled by “server blocks” instead of Virtual Host entries. They’re similar, but the configuration file is a bit different.
First, we need to set up our directories where the server blocks will live:
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
NOTE: Yes, we could just edit the NGINX global configuration file (nginx.conf) instead of creating a directory structure. However, by setting up a directory tree (which is what Debian-based Linux distros like Ubuntu will do), it allows for an easier configuration down the line if more websites are added.
Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file:
nano /etc/nginx/nginx.conf
Add these lines to the end of the http {} block, then save the file:
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
Great! Now NGINX can recognize the server block.
Create a new file specifically for the server block for the yourdomain.com site:
nano /etc/nginx/sites-available/yourdomain.com.conf
We are going to paste a new NGINX server block in this file:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
root /var/www/yourdomain.com/public_html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Let's break down a few important parts of the server block.
server_name: This is the domain you will be using for your site. Instead of localhost, we will use the public facing domain and www version of the domain you want to use, like so:
server_name yourdomain.com www.yourdomain.com;
root: This is the root directory for the site files.
root /var/www/yourdomain.com/public_html;
try_files: What we are doing here is telling the server to display a 404 error when a given file is not found.
try_files $uri $uri/ =404;
Create your server block using these parameters, then go ahead and save and close the file.
We need to create a symbolic link between sites-available and sites-enabled:
ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/yourdomain.com.conf
And finally, restart NGINX:
service nginx restart
You're done! Provided your DNS and/or hosts file is pointed for your domain, you should now be able to go to your domain in a web browser and see the test HTML page we created earlier.
Congratulations - you now have NGINX up and running on CentOS 7.
You have successfully disabled Apache on your system, and substituted it with the sleek and sexy NGINX web server. You're now ready to start tinkering and deploying the development stack of your choice.
As always, there will likely be more advanced configuration you want to do with NGINX to optimize the web server for your site. We highly suggest reviewing the NGINX documentation for any additional configuration you may want to do on the web server.
Otherwise, happy developing!
Meet the 27-hour day.
We built The Hub by GoDaddy Pro to save you time. Lots of time. Our members report saving an average three hours each month for every client website they maintain. Are you adding that kind of time to your day?