nginx的编译安装 centos7
2019-05-28 16:31
2306 查看
nginx的编译安装
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed 准备编译安装的基础环境: 官方源码包下载地址: https://nginx.org/download/
cd /usr/local/src/ 进入此目录 将压缩包下载到此目录
wget https://nginx.org/download/nginx-1.12.2.tar.gz 这边是直接从网站上下载过来的,如果事先准备好包 拖到此目录即可
tar xf nginx-1.12.2.tar.gz 解压包
cd nginx-1.12.2/ 进入包目录
./configure --prefix=/apps/nginx \--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_stub_status_module \--with-http_gzip_static_module \--with-pcre \--with-stream \--with-stream_ssl_module \--with-stream_realip_module 这是我自己选择的一些nginx功能参数 ,根据自己需求可增删
make 编译开始
make install 创建目录,并将生成的模块和文件复制到相应的目录
useradd nginx -s /sbin/nologin -u 2000 创建一个 nginx 专用的系统账户
chown nginx.nginx -R /apps/nginx/ 将自定义的 nginx 配置文件目录的所属组和所属主更改为 nginx
备注:nginx完成安装以后,有四个主要的目录: conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的核心主要的配置文件,其他的.conf 则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件, 配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。 html:目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件 是默认的错误页面提示页面。 logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比 如/var/logs/nginx里面。 sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
/apps/nginx/sbin/nginx -V 核对版本及编译参数是否与自己编写的一致
vim /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/apps/nginx/logs/nginx.pid #Nginx will fail to start if /run/nginx.pid already exists but has the wrong #SELinux context. This might happen when running `nginx -t` from the cmdline. https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /apps/nginx/logs/nginx.pid ExecStartPre=/apps/nginx/sbin/nginx -t ExecStart=/apps/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID #KillSignal=SIGQUIT #TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target 在此目录下编写nginx的自启动脚本
systemctl daemon-reload 对于新创建的unit文件或,修改了的unit文件,要通知systemd重载此配置文件;
systemctl start nginx 启动nginx服务
systemctl enable nginx 设定开机自启nginx服务
systemctl status nginx ● 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 Mon 2019-02-18 11:11:38 CST; 10s ago Main PID: 6348 (nginx) CGroup: /system.slice/nginx.service ├─6348 nginx: master process /apps/nginx/sbin/nginx 查看nginx的启动状态 active(running)值为running 代表启动成功
Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx 打开网页输入本机IP 测试nginx web界面!
配置测试: 新建一个pc web站点
测试之前在nginx的主配置文件的 keepalive_timeout 65; 下面加入 include /apps/nginx/conf/conf.d/*.conf; 表示在conf/conf.d/目录下以.conf结尾的为子配置目录,方便实验的增删! 由于是编译安装 所以 conf.d这个目录我们手动创建;mkdir /apps/nginx/conf/conf.d
vim /apps/nginx/conf/conf.d/pc.conf server { listen 80; server_name www.lvyao.com; location / { root /data/nginx/html/pc; } } 编写新建pc web站点的配置
mkdir /data/nginx/html/pc -p 创建配置对应web主页目录
echo "pc web" > /data/nginx/html/pc/index.html 创建主页目录显示的 html 文件
在windows的C:\Windows\System32\drivers\etc hosts文件中加入DNS地址解析 本机IP 自定义域名 172.22.143.132 www.lvyao.com
systemctl reload nginx 重启服务 并通过域名进行测试访问
新建一个Mobile web站点:
vim /apps/nginx/conf/conf.d/mobile.confserver { listen 80; server_name mobile.lvyao.net; location / { root /data/nginx/html/mobile; } } 编写新建mobile web站点的配置
mkdir /data/nginx/html/mobile -p 创建配置对应web主页目录
echo "mobile web" >> /data/nginx/html/mobile/index.html 创建主页目录显示的 html 文件
在windows的C:\Windows\System32\drivers\etc hosts文件中加入DNS地址解析 本机IP 自定义域名 172.22.143.132 mobile.lvyao.net
systemctl reload nginx 重启服务 并通过域名进行测试访问
root与alias
指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location,如 server { listen 80; server_name www.lvyao.com; location / { root /data/nginx/html/pc; } location /about { root /data/nginx/html/pc; index index.html; } } 编写wep家目录指定的配置
mkdir /data/nginx/html/pc/about 创建配置所对应的about文件
echo about > /data/nginx/html/pc/about/index.html 创建wep指定家目录主页显示的 html 文件
重启Nginx并访问测试
alias:定义路径别名,会把访问的路径重新定义到其指定的路径,如: vim /apps/nginx/conf/conf.d/alias.conf server { listen 80; server_name www.lvyao.com; location / { root /data/nginx/html/pc; } location /about { #使用alias的时候uri后面如果加了斜杠则下面的路径配置必须加斜杠,否则403 alias /data/nginx/html/pc; #当访问about的时候,会显示alias定义的/data/nginx/html/pc 里面的内容。 index index.html; } } 编写alias的文件配置
重启Nginx并访问测试
相关文章推荐
- Centos7编译安装Nginx
- centos7编译安装nginx1.8
- 开发人员学Linux(5):CentOS7编译安装Nginx并搭建Tomcat负载均衡环境
- 在 CentOS7最小化 下的编译安装:Nginx 1.5.2 + PHP 5.5.7 + MySQL 5.6.10
- centos7编译安装nginx及无缝升级https
- Centos7 编译安装 Nginx、MariaDB、PHP
- CentOS7编译安装Nginx
- centos7编译安装nginx
- centos7编译安装nginx的方法步骤
- Nginx笔记(一)----Centos7编译安装Nginx
- CentOS7编译安装nginx
- centos7 编译安装nginx1.9.8
- centos7搭建lnmp编译安装nginx【二】
- CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx1.6)
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.0 + Mariadb 10.1.9 + Nginx 1.9.9 + PHP 7.0.2)
- centos7编译安装nginx1.9.7
- 编译安装在centos7的nginx
- Centos7编译安装nginx
- CentOS7编译安装nginx1.8.1
- 在centos7上编译安装nginx