Nginx服务的搭建与配置

Nginx服务的搭建与配置

一、关闭防火墙并安装epel源

1、关闭selinux

①修改selinux的配置文件

[root@localhost ~]# vim /etc/selinux/config

SELINUX= disabled

②关闭selinux

[root@localhost ~]# setenforce 0

2、关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

3、安装epel.repo源

[root@localhost ~]# yum -y install epel-release.noarch

[root@localhost ~]# cd /etc/yum.repos.d/

二、Nginx源码包安装

1、网站:https:

2、下载:wget https:

三、 设置nginx安装源[如同安装阿里云源配置一样]

1、安装官方提供的Centos安装nginx源

[root@www www]# rpm -Uvh https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Nginx服务的搭建与配置

2、或者手动添加nginx安装源

[root@www www]# vim /etc/yum.repos.d/nginx.repo

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/7/$basearch/

gpgche=0

enabled=1

3、安装Nginx

通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx。

[root@www yum.repos.d]# yum -y install nginx

4、Nginx的 配置

①Nginx的主配置文件

[root@www ~]# vim /etc/nginx/ nginx.conf

②Nginx配置支持PHP

/etc/nginx/conf.d目录下存放着多个配置文件,这些配置项会在Nginx运行时加载到主配置项目中(类似虚拟机)。Nginx是通过php-fpm来通讯的,所以需要监听 9000端口。

在这个目录下生成一个自己的配置文件例如admin.conf,并进行添加php配置属性信息

[root@www nginx]# vim /etc/nginx/conf.d/admin.conf

server {
listen 80;#端口
server_name www.test.com admin.test.com;#域名
root /var/www/card/public;#网站根目录
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;#主要配置隐藏url中index.php
break;
}
}
location ~ .php$ {

root /var/www/card/public;

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

④安装php-fpm并修改用户组和用户名

[root@www nginx]#yum -y install php-fpm#安装php-fpm

[root@www nginx]#vim /etc/php-fpm.d/www.conf#修改php-fpm配置文件,用户和用户组默认是apache,改成nginx

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.

group = nginx

⑤创建php网站根目录和PHP访问页面

[root@www www]# mkdir -p card/public

[root@www www]# vim /var/www/card/public /php_info.php

hello world”;?>

⑥重启php-fpm

[root@www var]# systemctl restart php-fpm #启动 php-fpm
[root@www var]# lsof -i :9000 #php-fpm端口是否正常启动
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 55916 root 6u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55921 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55922 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55923 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55924 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 55925 nginx 0u IPv4 2089492 0t0 TCP localhost:cslistener (LISTEN)

⑦浏览器访问PHP页面

Nginx服务的搭建与配置

四、Nginx配置反向代理(用户给用户访问的地址,红色为反向代理的主要配置),后面补充完

[root@localhost conf.d]# vim /etc/nginx/conf.d/ default.conf

upstream test{

server 192.168.81.130:8080 weight=1;#这里的IP是后端服务器IP地址,权重1

server 192.168.81.131:8080 weight=1;

}

server {

listen 80;
server_name www.test.com;

access_log /var/log/nginx/host.access.log main;

location / {

proxy_pass http://test;#这里可直接写IP地址进行配置,如果需要配置负载均衡,可以只有http://test 和 upstream名称一样

以下是一些反向代理的配置,可删除

root /usr/share/nginx/html;

index index.html index.htm;

proxy_redirect off;

proxy_set_header Host $host; #指定请求服务器域名和端口号

}

error_page 404 /404.html;

redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

proxy the PHP scripts to Apache listening on 127.0.0.1:80

location ~ .php$ {

proxy_pass http://127.0.0.1;

}

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .php$ {

root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;

}

deny access to .htaccess files, if Apache’s document root
concurs with nginx’s one

location ~ /.ht {

deny all;

}

}

Original: https://www.cnblogs.com/blue-wlZ/p/16289219.html
Author: 孤立一点
Title: Nginx服务的搭建与配置

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582888/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 个人学习记录-Cpp基础-成员初始化列表

    https://blog.csdn.net/XIONGXING_xx/article/details/115553291https://blog.csdn.net/W_Y2010/…

    Linux 2023年6月6日
    098
  • git-config 配置多用户环境以及 includeIf用法

    方法一: 直接在 $path文件中添加 用户名和 邮箱,如: [use…

    Linux 2023年5月27日
    0134
  • CentOS7安装MySQL5.7并配置账户等

    注意: 有的Centos版本默认安装了mariadb, 可以先将其卸载 检查mariadb是否安装 yum list installed | grep mariadb 卸载mari…

    Linux 2023年6月6日
    079
  • 错误日志:Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

    csharp;gutter:true; 错误日志如下:</p> <pre><code> ;gutter:true;[org.springfram…

    Linux 2023年6月7日
    0155
  • 项目经验示例

    一,期中项目经验示例 1,根据现有结构部署工具(PXE+kickstart)2,结合应用系统需求定制部署模版3,制作系统优化等一键执行脚本4,自动化部署实施5,根据定制的优化内容对…

    Linux 2023年6月7日
    091
  • (十)redis源码解读

    一、redis工作机制 redis是 单线程,所有命令(set,get等)都会加入到队列中,然后一个个执行。 二、为什么redis速度快? 1、基于内存 2、redis协议resp…

    Linux 2023年5月28日
    0114
  • Arthas-开源的java诊断工具,非常有用

    常用命令 help 查看帮助 help COMMAND 查看指定&#…

    Linux 2023年6月13日
    0140
  • tomcat 9 搭建文件服务器 失败

    场景 服务器上某个目录,想开放给别人浏览权限,图省事儿用Python开了个SimpleHTTPServer,但总是断断续续的,没太找到原因。 想到有tomcat,就搜了一下用tom…

    Linux 2023年6月8日
    095
  • Nginx禁止ip加端口访问

    使用 iptables 限制对应端口,再利用Nginx将80端口转发到对应端口 CentOS7默认的防火墙是 firewalle,先看看服务器中有没有安装 iptables [ro…

    Linux 2023年5月27日
    093
  • Linux与Windows文件同步

    本次采用的同步方式是rsync,Rsync是一款免费且强大的同步软件,可以镜像保存整个目录树和文件系统,同时保持原来文件的权限、时间、软硬链接。第一次同步时会复制全部内容,下次只传…

    Linux 2023年6月8日
    0143
  • vscode搜索所有文件夹中所有文件的方法

    最近在看opencv相关的内容,看到画图这一部分时,提示我 这些代码都来自OpenCV代码的sample文件夹。 按照他的提示,我打开了相应的文件夹,却发现,so many 文件 …

    Linux 2023年6月14日
    0276
  • Docker 安装 MySQL、Redis

    1 Docker 中安装 Redis 1.1 创建目录 在硬盘上创建 redis 的数据目录: mkdir -p /Users/yygnb/dockerMe/redis/data …

    Linux 2023年6月7日
    0108
  • centos7自动化ssh免密

    在做免密前要先手动生成公钥: ssh-keygen -t rsa 敲击三次回车即可 写一个shell脚本: expect命令可以获取到命令返回结果并且根据指定内容进行自动发送相应字…

    Linux 2023年6月6日
    0151
  • 实验一-密码引擎-加密API实现与测试

    任务详情 1 下载并查找GMT 0018-2012密码设备应用接口规范原始文档进行学习 (5分) 2 实现GMT 0018-2012密码设备应用接口规范的接口函数,至少实现:1)设…

    Linux 2023年6月8日
    093
  • NoteOfMySQL-14-日志管理

    一、MySQL日志 日志是MySQL数据库的重要组成部分,日志文件记录了MySQL数据库的日常操作和错误信息,可以通过分析这些日志文件了解MySQL数据库的运行情况。MySQL数据…

    Linux 2023年6月14日
    091
  • 常见题目

    这几天有朋友反映给小编说让多发点关于面试的文章,小编深知从事IT行业的难处,跳槽多,加班多,薪资不乐观,大多数朋友都想找新的工作,进入一个好的公司,今天小编就给大家带来了C语言面试…

    Linux 2023年6月13日
    097
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球