CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CentOS 7.2 安装Nginx服务大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

一、 Nginx简介 Nginx (“ENGIne x”) 是一个高性能的http和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6

一、 Nginx简介

  • Nginx (“ENGIne x”) 是一个性能http和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,Nginx 1.0.4发布。
  • Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师IgorSysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用Nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
  • Nginx下载地址:http://nginx.org/
  • Nginx官方网站:https://www.nginx.com/

二、Nginx安装

# install from EPEL
[root@linuxprobe~]# yum --enablerepo=epel -y install Nginx
# 基础设置
 [root@linuxprobe~]# vi /etc/Nginx/Nginx.conf
# line 40: change hostname
server_name linuxprobe.org; 
[root@linuxprobe ~]# systemctl start Nginx 
[root@linuxprobe ~]# systemctl enable Nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/Nginx.service to /usr/lib/systemd/system/Nginx.service.
[root@linuxprobe ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain linuxprobe.org 
10.1.1.56 vdevops.com
# 开启防火墙
[root@linuxprobe ~]# firewall-cmd --add-service=http --peRMANent 
success
[root@linuxprobe ~]# firewall-cmd --reload 
success
  • 客户端设置hosts,从浏览器访问linuxprobe.org

虚拟主机设置

[root@linuxprobe ~]# vi /etc/Nginx/conf.d/linuxcool.com.conf
# create new

server {
    listen       80;
    server_name  linuxcool.com;

    @R_772_5352@n / {
        root   /usr/share/Nginx/linuxcool;
        index  index.html index.htm;
    }
}
[root@linuxprobe ~]# mkdir /usr/share/Nginx/linuxcool
[root@linuxprobe w ~]# systemctl restart Nginx
[root@linuxprobe ~]# vi /usr/share/Nginx/virtual.host/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx LinuxCool Test Page
</div>
</body>
</html>

Nginx用户目录配置

[root@linuxprobe ~]# vi /etc/Nginx/Nginx.conf
# add into "server" section
        @R_772_5352@n ~ ^/~(.+?)(/.*)?$ {
            alias /home/$1/public_html$2;
            index  index.html index.htm;
            autoindex on;
        }
[root@linuxprobe ~]# systemctl restart Nginx 
# 切到普通用户
[wang@linuxprobe~]$ chmod 711 /home/cent
[wang@linuxprobe~]$ @H_192_56@mkdir ~/public_html [wang@linuxprobe~]$ chmod 755 ~/public_html 
[wang@linuxprobe~]$ vi ~/public_html/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx UserDir Test Page
</div> </body>
</html>
  • 浏览器访问测试配置是否正确

配置Nginx开启SSL

[root@linuxprobe~]# vi /etc/Nginx/Nginx.conf
# add into "server" section
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        listen       443 ssl;
        server_name  linuxprobe.org;
        root         /usr/share/Nginx/html;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers ECDHE+RSAGCM:ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:!aNULL!eNull:!EXPORT:!DES:!3DES:!@H_665_248@mD5:!DSS;
        ssl_certificate      /etc/pki/tls/certs/server.crt;
        ssl_certificate_key  /etc/pki/tls/certs/server.key;
[root@linuxprobe~]# systemctl restart Nginx 
  • 防火墙开启https通信
[root@linuxprobe~]# firewall-cmd --add-service=https --peRMANent
success
[root@linuxprobe~]# firewall-cmd --reload
success
@H_842_301@Nginx 设置访问认证
  • 对web页面访问进行控制
# 以auth_basic目录为例
[root@linuxprobe~]# yum -y install httpd-tools
[root@linuxprobe~]# vi /etc/Nginx/Nginx.conf
# add into "server" section

        @R_772_5352@n /auth_basic {
            auth_basic            "Basic Auth";
            auth_basic_user_file  "/etc/Nginx/.htpasswd";
        }
[root@linuxprobe~]# htpasswd -c /etc/Nginx/.htpasswd wang
New password:     # set password
Re-type new password:
Adding password for user wang
[root@www ~]# systemctl restart Nginx
# 创建目录及测试页面
[root@linuxprobe ~]# mkdir /usr/share/Nginx/html/auth_basic
[root@linuxprobe ~]# vim /usr/share/Nginx/html/auth_basic/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx UserDir Test Page
</div>
</body>
</html>
  • 客户端从浏览器访问测试,查看结果

Nginx 反向代理设置

  • 配置通过http方式,Nginx的80端口转发到后端apache的80端口
[root@linuxprobe ~]# vi /etc/Nginx/Nginx.conf
# change like follows in "server" section
    server {
        listen      80 default_server;
        listen      [::]:80 default_server;
        server_name linuxprobe.org;
        proxy_redirect           off;
        proxy_set_header         X-Real-IP $remote_addr;
        proxy_set_header         X-ForWARDed-For $proxy_add_x_forWARDed_for;
        proxy_set_header         Host $http_host;
        @R_772_5352@n / {
            proxy_pass http://vdevops.org/;
        }
    }
[root@linuxprobe ~]# systemctl restart Nginx 
  • 后端apache服务设置
[root@vdevops~]# vi /etc/httpd/conf/httpd.conf
# line 196: change
LogFormat "\"%{X-ForWARDed-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@vdevops~]# systemctl restart httpd 
# 通过elinks(模拟浏览器访问)访问验证
[root@localhost ~]# yum -y install elinks
[root@linuxprobe ~]# elinks http://linuxprobe.org/

Nginx && PHP-FPM

[root@linuxprobe ~]# yum --enablerepo=epel -y install PHP PHP-mbString PHP-pear PHP-fpm 
  • 配置 Configure PHP-FPM and Ngin
[root@linuxprobe ~]# vi /etc/PHP-fpm.d/www.conf
# line 39: change
user = Nginx
# line 41: change
group = Nginx
[root@linuxprobe ~]# systemctl start PHP-fpm
[root@linuxprobe ~]# systemctl enable PHP-fpm
[root@linuxprobe ~]# vi /etc/Nginx/Nginx.conf
# add into "server" section
        @R_772_5352@n ~ \.PHP$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILename $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
            include        fastcgi_params;
        }
[root@linuxprobe ~]# systemctl restart Nginx
  • 创建PHP测试页
[root@www ~]# echo "<?PHP PHPinfo() ?>" > /usr/share/Nginx/html/info.PHP

大佬总结

以上是大佬教程为你收集整理的CentOS 7.2 安装Nginx服务全部内容,希望文章能够帮你解决CentOS 7.2 安装Nginx服务所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。