Centos 7 Nginx Multiple Websites
Introduction
Are you looking for a way to set up multiple websites on your CentOS 7 server utilizing the Nginx web server? If so, you’ve come to the right place. In this article, we’ll detail the steps necessary for setting up multiple websites on a single server using the Nginx web server.
Requirements
To complete this guide, you should have an existing CentOS 7 server with Nginx already installed and configured. You should also have all requirements necessary to set up a website with Nginx met. See the guide How to Install and Set Up Nginx on CentOS 7 for more details.
Create Directories for the New Websites
The first step to setting up multiple websites is to create a new directory for each website. This is because Nginx bases its configuration off of the directory in which files are stored. In this guide, our starting path will be the /var/www/ directory, as this is where all websites should ideally be stored.
Using the sudo command create a matching directory with the same name you intend to use for your website (i.e. mywebsite.com). Replace “mywebsite.com” with the name of the directory you wish to create:
$ sudo mkdir -p /var/www/mywebsite.com
Assign the Proper Ownership to Directories
Once you’ve created the directory for your website, you should now assign the proper ownership. This is because if the wrong person or group owns the website directory, Nginx will not be able to serve content from it. This can cause major problems for your website.
In general, you should assign ownership to the user account that the Nginx web server is running under. To determine which account this is, use the following command:
$ ps -ef | grep nginx
Create a Sample Page
It’s time to test our setup. To do this, you’ll need to create a sample page so that you can assess whether your configuration will work correctly or not. To do this, you’ll need to create a index.html file in each website’s directory.
Make sure to use the proper file path (i.e. /var/www/mywebsite.com/index.html). Replace “mywebsite.com” with the name of the directory you created previously:
$ sudo touch /var/www/mywebsite.com/index.html
Configure the Nginx Server Block
Now that you’ve created the directories for your websites and assigned proper ownership, it’s time to configure the Nginx server block for each website. To do this, you’ll need to create a new configuration file in the /etc/nginx/conf.d/ directory.
Create a file with the same name of the directory you created previously (i.e. mywebsite.com.conf ) and paste the following configuration into it:
server {
listen 80;
server_name www.mywebsite.com;
root /var/www/mywebsite.com;
index index.html;
}
Reload Nginx to Activate the Server Blocks
Once you’ve created the Nginx server blocks, you’ll need to reload Nginx for the changes to take effect. To do this, use the following command:
$ sudo systemctl restart nginx
Testing the Setup
To test the setup, you’ll need to open a web browser and navigate to the websites you’ve configured. Use the IP address of your Nginx server to access the websites (i.e. http://127.0.0.1). If everything is configured correctly, you should see the sample page you created earlier.
Conclusion
In this article, we’ve outlined the steps necessary for setting up multiple websites on a single server using the Nginx web server. We’ve explained how to create the necessary directories and assign ownership, as well as create a sample page and configure the Nginx server blocks. After completing this guide, you should have a better understanding of how to set up multiple websites on a single server using Nginx.
FAQs
Q: What is Nginx?
A: Nginx is an open source web server that is commonly used to serve static content, such as images and HTML files. It is also used as a reverse proxy or load balancer to serve dynamic content, such as PHP or ASP.NET.
Q: What is a Server Block?
A: A Server Block is a configuration that is used to define how a server should handle requests for a particular domain. It contains information such as file paths, port numbers and domain names.
Q: What is the difference between Nginx and Apache?
A: The main difference between Nginx and Apache is that Nginx is event-driven and Apache is process-driven. This means that Nginx is better suited for handling multiple requests simultaneously, whereas Apache is better suited for running background processes such as cron jobs.
Thank you for reading this article. Please read other articles for detailed information and best practices.
Related Posts:
- Nginx Listen Multiple Ip Addresses Nginx Listen Multiple Ip Addresses What is Nginx? Nginx is a open-source web server created by Igor Sysoev and first publicly released in 2004. Since its release, Nginx has become…
- Nginx Config Domain Based Root Nginx Config Domain Based Root What is Nginx Config? Nginx (pronounced “engine-x”) is a popular web server software program. It is open source, high performance and is being used by…
- Setting Sites Available Di Nginx Ubuntu Setting Sites Available Di Nginx Ubuntu Understanding Nginx and its Parts Nginx is a great tool for managing web applications. It allows you to host multiple websites, as well as…
- Nginx On Ubuntu 18.04 Nginx On Ubuntu 18.04 What is Nginx? Nginx is a free, open-source, high-performance web server software. It is known for its stability, rich feature set, simple configuration, and low resource…
- Reverse Proxies Nginx Centos 6 Reverse Proxies Nginx Centos 6 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- Installing Nginx On Centos 7 Installing Nginx On Centos 7 What is Nginx? Nginx is an open source, high performance web server software. It is commonly used for caching static web pages to speed up…
- Install Phpmyadmin On Nginx Centos 7 Install Phpmyadmin On Nginx Centos 7 Introduction PhpMyAdmin is one of the most popular tools used to manage and administer a MySQL database. It is a web-based application and can…
- Cannot Start Nginx On Centos 7 Failed To Exec Airflow Cannot Start Nginx On Centos 7 Failed To Exec Airflow What Is Nginx in Centos 7? Nginx is an open source web server that powers some of the largest and…
- How To Configure Virtual Host In Nginx On Centos 7 How To Configure Virtual Host In Nginx On Centos 7 Introduction We all need to configure virtual host on Nginx webserver on CentOS 7 in order to place multiple websites…
- Nginx Service Is Failed On Centos 7 Nginx Service Is Failed On Centos 7 Introduction Nginx is an open source Web server and a reverse proxy for HTTP, SMTP, POP3 and IMAP protocols. It can also be…
- Openldap Slapd Php Nginx Ldap Centos Openldap Slapd Php Nginx Ldap Centos What is OpenLDAP OpenLDAP is an open-source implementation of the Lightweight Directory Access Protocol (LDAP) and is offered by the OpenLDAP project. OpenLDAP is…
- Install Nginx Server On Centos 7 Install Nginx Server On Centos 7 What Is Nginx? Nginx is an open source web server and reverse proxy developed by Igor Sysoev in 2004. It is an efficient web…
- Remove Nginx Completely Centos 7 Remove Nginx Completely Centos 7 Overview This article will provide a brief overview of Nginx, some of the ways it can be removed from a Centos 7 system and directions…
- Setting Proxy Nginx Centos 7 Setting Proxy Nginx Centos 7 Introduction Setting up a proxy server on a CentOS 7 server is a relatively straightforward task. Nginx is a powerful open source reverse proxy server…
- How To Ubuntu 16.04 Multiple Php Nginx How to Ubuntu 16.04 Multiple PHP Nginx Ubuntu 16.04 is a robust operating system that is commonly used for web application development. This operating system uses Nginx as the web…
- Reverse Proxy Nginx Centos 7 Reverse Proxy Nginx Centos 7 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- How To Add Domain In Nginx How To Add Domain In Nginx Introduction To Nginx Nginx (Internet Information Server) is a popular open-source web server that is able to serve a variety of content, from simple…
- Nginx Multi Domain Centos 7 Nginx Multi Domain Centos 7 Introduction to Nginx Nginx is an open source, high performance web server software written in C language, designed to be deployed on Linux and Unix-like…
- Remove Apache And Install Nginx Remove Apache And Install Nginx What is Apache and Nginx? Apache and Nginx are both popular web servers used to serve web pages and content to users on the web.…
- Centos 7 Install Nginx Php 7 Centos 7 Install Nginx Php 7 Nginx Server Overview and Prerequisites Nginx is a highly popular open source web server and reverse proxy software, known for its scalability and performance…
- Nginx Php-Fpm Php Mariadb Mysql Centos 7.2 Digitalocean Nginx, Php-Fpm, Php, Mariadb, Mysql, Centos 7.2 Digitalocean Introduction: What is Nginx? Nginx is a powerful web server that was first released in 2004. It is known for being a…
- Certbot Centos 7 Nginx Staging Certbot Centos 7 Nginx Staging: An Easy Step By Step Guide What Is Certbot? Certbot is a free, open-source software tool that allows you to easily obtain digital certificates from…
- Certbot Centos 7 Nginx Certificate Invalid Certbot Centos 7 Nginx Certificate Invalid What is Certbot & Centos 7 Nginx Certificate? Certbot is an open-source software project from the Electronic Frontier Foundation (EFF). It enables website owners…
- Nginx Proxy Cache Redis Wordpress Centos Nginx Proxy Cache Redis Wordpress Centos Understanding Nginx Proxy Cache Nginx Proxy Cache is a web-acceleration software from Nginx, Inc. It is designed to speed up dynamic web content delivery.…
- Nginx One Port Multiple Backend Nginx One Port Multiple Backend Introduction Nginx is one of the most popular web servers used today. It is a powerful, reliable and efficient web server. Nginx is also able…
- Install Nginx Php Java On Centos 7 Install Nginx Php Java On Centos 7 What is Nginx? Nginx is an open source Web server created to serve Web traffic efficiently, reliably, and quickly. Nginx has become the…
- Setting Serverblock For Domain Using Nginx On Ubuntu 18.04 Setting ServerBlock For Domain Using Nginx On Ubuntu 18.04 Introduction Nginx is a powerful open-source web server that can be used for serving static, dynamic websites and applications. Nginx is…
- Nginx Reverse Proxy Apache Change Document Root Nginx Reverse Proxy Apache Change Document Root What is a Reverse Proxy? A reverse proxy is a type of proxy server that takes HTTP or HTTPS requests from a client…
- Setup Centos 7 Nginx With Php Setup Centos 7 Nginx With Php Overview of Centos7 Nginx and PHP Centos 7 is one of the most popular operating systems for web servers. Many businesses use Centos 7…
- Centos 6 Nginx Connection Timeout Centos 6 Nginx Connection Timeout Understanding What is a Connection Timeout? A connection timeout is when a particular connection has not completed the necessary steps for establishing a full connection…