LNMP-Nginx服务配置

LNMP 快速搭建

1.准备工作

设置IP 配置yum源

源码包 二进制包(yum)

2.安装步骤

1)解压缩

tar -zxvf lnmp1.4-full.tar.gz

2)进入压缩目录

cd lnmp1.4-full

3)安装

./install.sh lnmp

二.LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录 : /home/wwwroot/default/phpmyadmin/ 
默认网站目录 : /home/wwwroot/default/
Nginx日志目录:/home/wwwlogs/

三.LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
LNMP 状态管理: lnmp {start|stop|reload|restart|kill|status}
LNMP 各个程序状态管理: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}

Nginx 配置文件

  1. vim /usr/local/nginx/conf/nginx.conf

    66 listen 8080 default_server;

    lnmp nginx restart

    测试 192.168.103.254:8080

  1. vim /usr/local/nginx/conf/nginx.conf

​ 70 root /www;

​ mkdir /www

​ vim /www/index.html

​ hello www!

​ lnmp nginx restart

​ 测试 192.168.103.254

  1. vim /usr/local/nginx/conf/nginx.conf

    72 error_page 404 /404.html;

cd /home/wwwroot/default

vim 404.html

hi 404!

lnmp nginx restart

测试 192.168.104.254/aaaaa.html

  1. 状态监控

    测试 192.168.104.254/nginx_status

检查配置文件是否有语法错误

​ /usr/local/nginx/sbin/nginx -t

平滑重启

pkill -HUP nginx

实验1:虚拟主机

1)域名解析 文件解析

192.168.103.254 www.sina.com
192.168.103.254 www.sohu.com

2)网站目录规划

mkdir /home/wwwroot/sina/

mkdir /home/wwwroot/sohu/

vim /home/wwwroot/sina/index.html

hi sina!

vim /home/wwwroot/sohu/index.html

hello sohu!

3)修改配置文件

vim /usr/local/nginx/config/nginx.conf

66 listen 80;

4)建立虚拟主机文件

vim /usr/local/nginx/conf/vhost/v.conf

  1 server {
  2         listen 80;
  3         server_name = www.sina.com;
  4         index index.html index.htm index.php;
  5         root /home/wwwroot/sina;
  6 
  7         include enable-php.conf;
  8 }
  9 server {
 10         listen 80;
 11         server_name = www.sohu.com;
 12         index index.html index.htm index.php;
 13         root /home/wwwroot/sohu;
 14 
 15         include enable-php.conf;
 16 }

  1. 重启服务 测试

pkill -HUP nginx

测试 www.sina.com www.sohu.com

实验2: 列表页显示

vim /usr/local/nginx/conf/nginx.conf

64 server
65 {
66 listen 80;
67 # default_server;
68 #listen [::]:80 default_server ipv6only=on;
69 server_name www.lnmp.org;
70 index index.html index.htm index.php;
71 root /home/wwwroot/default;
72 autoindex on;

pkill -HUP nginx

cd /home/wwwroot/default

mv index.html a.html

测试 192.168.103.254 列表

实验3:状态监控

vim /usr/local/nginx/conf/vhost/v.conf

  8 location /nginx_status{
  9         stub_status on;
 10         access_log  off;
 11         }

pkill -HUP nginx

测试 www.sina.com/nginx_status

实验4:rewrite 重写/重定向

www.sina.com -> www.sohu.com

1)修改配置文件

vim /usr/local/nginx/conf/vhost/v.conf

  1 server {
  2         listen 80;
  3         server_name = www.sina.com;
  4         index index.html index.htm index.php;
  5         root /home/wwwroot/sina;
  6         autoindex on;
  7         include enable-php.conf;
  8         if ($http_host = www.sina.com) {
  9                         rewrite  (.*)  http://www.sohu.com  permanent;
 10                 }

2)重启服务 测试

pkill -HUP nginx

测试 www.sina.com ->www.sohu.com

网页文件跳转

1)vim /usr/local/nginx/conf/vhost/v.conf

  1 server {
  2         listen 80;
  3         server_name = www.sina.com;
  4         index index.html index.htm index.php;
  5         root /home/wwwroot/sina;
  6         autoindex on;
  7         include enable-php.conf;
  8         rewrite index(\d+).html /index.php?id=$1 last;

  1. 建立index.php

vim /home/wwwroot/sina/index.php

<?php echo “hello” ?>

3)重启服务 测试

pkill -HUP nginx

测试 www.sina.com/index3.html

实验5:代理负载均衡(反向代理)

实验准备: S Nginx 192.168.103.254 分发

​ S1 Nginx 192.168.103.140 解析

​ S2 Nginx 192.168.103.199 解析

1)修改配置文件 S 192.168.103.254

vim /usr/local/nginx/conf/nginx.conf

 65 upstream myweb1 {
 66         server 192.168.103.140:80;
 67         server 192.168.103.199:80;
 68 }       
 69 server {
 70         listen       80;
 71         server_name  www.sohu.com;
 72         location / {
 73                 proxy_pass http://myweb1;
 74                 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
 75                 proxy_set_header Host $host;
 76                 proxy_set_header X-Forwarded-For $remote_addr;
 77 }               
 78 } 

  1. 修改S1 192.168.103.140 文件

关闭虚拟主机

cd /home/wwwroot/default/

vim index.html

S11111111111111

测试 192.168.103.140 S11111111111111

  1. 修改S1 192.168.103.199 文件

关闭虚拟主机

cd /home/wwwroot/default/

vim index.html

S22222222222222222

测试 192.168.103.199 S222222222222222222

4)重启S 服务器 测试

pkill -HUP nginx

测试 www.sohu.com

    原文作者:Joncc
    原文地址: https://www.jianshu.com/p/5280aa816c05
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞