微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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月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安装

  • 安装快速HTTP服务器“Nginx”并配置HTTP服务器
# 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;

    location / {
        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
        location ~ ^/~(.+?)(/.*)?$ {
            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~]$ 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:!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

Nginx 设置访问认证

  • 对web页面访问进行控制
# 以auth_basic目录为例
[root@linuxprobe~]# yum -y install httpd-tools
[root@linuxprobe~]# vi /etc/Nginx/Nginx.conf
# add into "server" section

        location /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;
        location / {
            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
        location ~ \.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

原文地址:https://www.jb51.cc/centos/380102.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。