Nginx has rapidly overtaken Apache to become the most popular web host across the world's top 1 million sites. With a low-performance profile but high flexibility, it's an excellent choice for both first-time and experienced web developers. Google, Netflix, Adobe, and more use this tool, and in this blog we're going to show you how you can, too.
Before you start, you'll need to set a few things up:
- A VPS server to host on
- A non-root user with
- Have your firewall active
- Apply Basic server security practices applied
- Fully update your server
- Install the EPEL software repository added (
sudo yum install epel-release)
Installing your Nginx web server on CentOS
The easy way
The fastest way to install Nginx is to install is as part of the LEMP stack via the one-click BitLaunch LEMP app. Simply choose "'LEMP stack with SSL" in the "Apps" tab when you launch your BitLaunch CentOS server.
Manually Installing Nginx on CentOS 7
Installing Nginx manually is as simple as running:
sudo yum install nginx
As usual, you'll need to enter
y to accept the install and wait for Nginx and its dependencies to install. Once the installation is complete, you can enable the Nginx on startup in systemctl:
sudo systemctl enable nginx
And then start it:
sudo systemctl start nginx
You can make sure it's running with
sudo systemctl status nginx. If it failed to start, make sure you match the pre-requisites listed above.
Configuring your Firewall
Naturally, external Nginx connections need to be allowed through the firewall before you can serve web content to all your users. Nginx runs on port 80 (HTTP) by default, which is easy enough to open:
sudo firewalll-cmd --permanent --zone=public --add-service=httpsudo firewall-cmd --permanent --zone=public --add-service=httpssudo firewall-cmd --reload
You can then check your firewall using:
sudo firewall-cmd --permanent --list-all
You should now be able to visit the server in your browser using http://your.server.ip. If the Nginx installation was successful, you'll see the 'Welcome to CentOS' webpage.
Configuring your Nginx server
Nginx has several configuration files you can modify. Here are their locations:
- Server root: the default server root directory is
/usr/share/nginx/html. This is where you should place the files you want to be served (ie. your website files). It is specified in
/etc/nginx/conf.d/default.conf, and can be modified.
- Server block config: located at
/etc/nginx/conf.d. Server blocks are the equivalent of virtual hosts in Apache. Adding an additional .conf file will allow you to specific additional directories to be served, to, for example, host multiple sites on one server. For more information about server blocks, see our Ubuntu server blocks guide.
- Nginx global config file: Found at
/etc/nginx/nginx.conf. Here you can change everything from the user to log locations, server ports, TLS settings, etc.
That's it. You'll probably want to install PHP and MySQL too if you want to host anything other than a basic site.