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:
- Setting Ssl Nginx Multiple Port Setting SSL Nginx Multiple Port What is SSL? Secure Sockets Layer (SSL) is a protocol used to secure data transmitted between two systems, such as a web server and a…
- Install Nginx Php Mysql Windows Title Here Install Nginx Php Mysql Windows Introduction For a variety of reasons, many web developers and programmers look to install a local development version of a web server, such…
- How To Configure Websocket Nginx Fpm How To Configure Websocket Nginx Fpm Introduction Websocket is a modern web technology that provides bidirectional communication between a web server and a web client. The websocket protocol allows for…
- Nginx Change Port 80 To 3000 Nginx Change Port 80 To 3000 What Is Nginx? Nginx is an open-source web server created by Igor Sysoev in 2002. Nginx is a powerful, reliable, and robust web server…
- 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…
- Vestacp Nginx And Apache Inactive After Migrate Ip Vestacp Nginx and Apache Inactive After Migrate IP What is Vestacp? Vesta Control Panel or VestaCP is an open-source hosting control panel. It can be freely used to manage websites,…
- Install Nginx Ssl On Centos 7 Install Nginx Ssl On Centos 7 1. Overview Of Nginx SSL NGINX SSL (Secure Socket Layer) is an open source web server designed to provide reliable and secure web application…
- Wordpress Nginx Ubuntu 18.04 Wordpress Nginx Ubuntu 18.04 Overview WordPress is a popular web software used by millions of people around the world. It allows users to easily create and manage websites without the…
- 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…
- Nginx Listen To Differnt Port Nginx Listen To Differnt Port What is Nginx? Nginx is an open-source web server software used to serve content to the web. It is used to host web applications and…
- Nginx Php Fpm 7.2 Nginx Php Fpm 7.2 Introduction to Nginx and PHP-FPM Nginx is an open-source web server, reverse proxy server, and load balancer. It is known for being lightweight and fast, and…
- Nginx Multiple Web Sites One Ip Nginx Multiple Web Sites One Ip Introduction Nginx is a powerful and popular web server used by millions of websites and web application around the world. It is highly performant,…
- Nginx Css And Image Not Showing Centos Nginx Css And Image Not Showing Centos Common Problems When Nginx CSS And Images Aren't Showing When you're working with Nginx on the Centos operating system, you may have run…
- Error Nginx When Installing Certbot Ubuntu Error Nginx When Installing Certbot Ubuntu What is Nginx? Nginx is a web server software often used to serve web pages. It is known as a high-performance web server and…
- Nginx Vs Apache Performance 2019 Nginx Vs Apache Performance 2019 What is Nginx? Nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy. It is much faster than Apache and has been…
- 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.…
- Nginx Config Multiple Proxy_Pass Nginx Config Multiple Proxy_Pass What is Nginx? Nginx is a high-performance web server that is widely used for deploying web applications and handling static content. It is also capable of…
- How To Install Nginx And Mariadb 10 How To Install Nginx And Mariadb 10 What is Nginx and MariaDB 10 Nginx is a free, open-source web server that is known for its scalability and performance. It is…
- Nginx Centos 7.6 Virtual Host Nginx Centos 7.6 Virtual Host Introduction to Nginx Virtual Hosts Virtual Hosts, also called Virtual Servers, are a very important function of web hosting. They allow multiple websites to run…
- Install Nginx 1.17 Centos 8 Install Nginx 1.17 Centos 8 Introduction to Nginx Nginx is one of the most popular web servers in the world. It is reliable, free, and open source software. It is…
- Nginx Dev Mapper Centos-Root Is 100 Full Nginx Dev Mapper CentOs-Root Is 100 Full What Is Nginx Dev Mapper? Nginx Dev Mapper is a file system space mapping tool for the Linux operating system and is shipped…
- 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…
- 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 Windows Create Virtual Host Nginx Windows Create Virtual Host What Is Nginx? Nginx is an open source web server software. First released in 2004, it has steadily become the preferred choice of web administrators…
- How To Configure Nginx Debian How To Configure Nginx Debian Introduction to Nginx Debian Nginx is a web server and reverse proxy that is used to host websites and manage incoming traffic over the world…
- Laravel 5.5 Configure Nginx Laravel 5.5 Configure Nginx Introduction To Nginx Nginx is a web server software often deployed as a reverse proxy. It is open-source and available to download for free. Nginx has…
- Php-Fpm Cache Nginx Centos Php-Fpm Cache Nginx Centos Introduction to Nginx, Php-Fpm, and Centos Nginx, PHP-FPM, and Centos are three powerful, open-source technologies that are used to create powerful applications, websites, and services. Nginx…
- Install Webmin Plugin Nginx Ubuntu Install Webmin Plugin Nginx Ubuntu What is Nginx? Nginx is a web server that runs on the Linux operating system. It is a popular web server software and can handle…
- Docker Nginx Location For Multiple Sites Docker Nginx Location For Multiple Sites If you are a web developer or an IT professional, you know that one of the major headaches in web development is finding a…
- How To Install Nginx On Centos 7 Rhel 7 How To Install Nginx On Centos 7 Rhel 7 Nginx is one of the most popular web servers around the globe – being an open-source application, it drives a large…