NGINX (Engine X) is an powerful web server, Its getting popular very fast due to its fast speed. NGINX is also used as a reverse proxy server. This tutorial will help you to setup NGINX on CentOS/RHEL 7/6 and Fedora systems.
第 1 步 – 安装 Nginx
Nginx 软件包在默认存储库中可用。执行以下命令在您的系统上安装最新的 Nginx 可用版本。
$ yum install nginx
$ dnf install nginx #On Fedora 22+ systems
步骤 2 – Nginx 基本配置
Do some initial setting before starting nginx. Edit Nginx main configuration file /etc/nginx/nginx.conf and update following values.
$ vim /etc/nginx/nginx.conf
更新以下值:
worker_processes 4; # Number of CPU available in system
listen 80; # Port on which nginx will listen
步骤 3 – 启动 Nginx 服务
使用以下命令启动 Nginx Web 服务器并将 Nginx 配置为在系统启动时自动启动。
$ systemctl enable nginx.service
$ systemctl start nginx.service
现在使用您最喜欢的浏览器在端口 80 上访问您的服务器,它将显示默认的 Nginx 页面。
步骤 4 – 在 NGINX 中创建虚拟主机
对 NGINX 进行初始配置更改后,让我们开始配置第一个虚拟主机。首先为您的应用程序创建文档根目录。
$ sudo mkdir -p /var/www/example.com/httpdocs
$ sudo chown -R nginx.nginx /var/www/example.com
现在创建一个包含示例文本的索引文件
$ echo "Welcome to NGINX" > /var/www/example.com/httpdocs/index.html
现在创建一个虚拟主机配置文件。我只是创建名为 example.com.conf 的 default.conf 文件的副本,并在编辑器中编辑文件。
$ cp /etc/nginx/conf.d/virtual.conf /etc/nginx/conf.d/example.com.conf
$ vim /etc/nginx/conf.d/example.com.conf
在配置文件中进行一些更改,如下所示,并保持其他设置不变。
server {
listen 80;
server_name example.com;
location / {
root /var/www/example.com/httpdocs;
index index.html index.htm;
}
最后,使用以下命令重新启动NGINX服务
$ sudo systemctl restart nginx.service
Congratulation’s! You have successfully installed Nginx. Read out other articles Install Nginx, MySQL, PHP (LEMP) Stack with PHP-FPM