Nginx发音为“ engine x”,是一种开源的高性能HTTP和反向代理服务器,负责处理Internet上一些最大站点的负载。它可用作HTTP和非HTTP服务器的独立Web服务器,负载平衡器,内容缓存和反向代理。
与Apache相比,Nginx可以处理大量并发连接,并且每个连接的内存占用量较小。
必要条件
您需要一台CentOS 8服务器:
1、至少 1GB的RAM
2、具有sudo权限的非root用户
【腾讯云】热卖云产品3折起,云服务器、云数据库特惠,服务更稳,速度更快,价格更优
安装Nginx
从CentOS 8开始,Nginx软件包在默认的CentOS存储库中可用。
在CentOS 8上安装Nginx只需输入以下内容即可:
#yum install nginx
安装完成后,使用以下命令启用并启动Nginx服务:
#systemctl enable nginx #systemctl start nginx
要验证服务是否正在运行,请检查其状态:
#systemctl status nginx
输出应如下所示:
[root@localhost ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2020-01-07 14:47:31 CST; 9s ago Process: 22797 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 22594 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 22593 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 22800 (nginx) Tasks: 2 (limit: 25725) Memory: 6.9M CGroup: /system.slice/nginx.service ├─22800 nginx: master process /usr/sbin/nginx └─22803 nginx: worker process 1月 07 14:47:31 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server... 1月 07 14:47:31 localhost.localdomain nginx[22594]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 1月 07 14:47:31 localhost.localdomain nginx[22594]: nginx: configuration file /etc/nginx/nginx.conf test is successful 1月 07 14:47:31 localhost.localdomain systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument 1月 07 14:47:31 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
调整防火墙
FirewallD是Centos 8上的默认防火墙解决方案。
在安装过程中,Nginx使用预定义的规则创建防火墙服务文件,以允许访问HTTP(80)和HTTPS(443)端口。
使用以下命令永久打开必要的端口:
#firewall-cmd --permanent --zone=public --add-service=http #firewall-cmd --permanent --zone=public --add-service=https #firewall-cmd --reload
现在,您可以通过http://YOUR_IP在Web浏览器中打开来测试Nginx的安装。您应该看到默认的Nginx欢迎页面,其外观应类似于下图:
Nginx配置文件的结构说明
所有Nginx配置文件都位于/etc/nginx/目录中。
Nginx的主要配置文件是/etc/nginx/nginx.conf。
为每个域创建一个单独的配置文件使服务器易于维护。
Nginx服务器阻止文件必须以结尾.conf并存储在/etc/nginx/conf.d目录中。您可以根据需要拥有任意数量的服务器块。
遵循标准命名约定是一个好习惯。例如,如果域名是,mydomain.com则配置文件应命名为mydomain.com.conf
如果在域服务器块中使用可重复的配置段,则最好将这些段重构为片段。
Nginx日志文件(access.log和error.log)位于/var/log/nginx/目录中。建议有不同access和error日志文件每个服务器模块。
您可以将域文档的根目录设置为所需的任何位置。webroot的最常见位置包括:
/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>
/usr/share/nginx/html
发表评论