美文网首页全栈工程师通往架构师之路服务器方面
第五个模块 Ubunt14.04 LANMP环境、Nginx虚拟

第五个模块 Ubunt14.04 LANMP环境、Nginx虚拟

作者: 霄峰 | 来源:发表于2016-09-22 22:56 被阅读100次

    参考 Ubuntu 下php7 mysql5.7 LNMP 环境搭建
    第五个模块 Ubunt14.04 LANMP环境、Nginx虚拟主机配置-(第一部分)
    第五个模块 Nginx负载均衡、动静分离-(第三部分)
    306-Ubuntu16.04 安装LNAMP


    第一部分 安装LANMP环境(apache2|php5.6|mysql5.6)

    1. 修改apt源(Ubuntu 14.04)添加AliYun 源

    vim /etc/apt/sources.list
    

    AliYun 源

    # AliYun
    deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
    

    更新

    sudo apt update
    sudo apt upgrade
    

    2. 安装apache2

    sudo apt-get install apache2
    //开启rewrite
    sudo a2enmod rewrite
    

    在配置文件中支持.htaccess,修改apache2.conf

    <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>
    

    3. 安装Nginx

    sudo apt-get install nginx
    

    4. 安装mysql5.6

    sudo apt-get install mysql-server
    sudo service mysql start
    

    5. 安装php5.6

    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    sudo apt upgrade
    sudo apt-get install php5.6 php5.6-mysql php-gettext php5.6-mbstring libapache2-mod-php5.6 php5.6-fpm
    //将5.6的地方改为7.0就可以安装php7.0
    sudo service php5.6-fpm restart
    sudo service apache2 restart
    

    第二部分 Nginx虚拟主机配置

    场景:
    配置两台虚拟主机:

    IP 域名 root目录
    172.16.0.51 a.com /home/用户名/a
    172.16.0.51 b.com /home/用户名/b
    • 在用户家目录下创建目录
    cd ~
    mkdir a b
    //分别在a、b目录下创建index.php文件内容自定义
    
    • 在Nginx的配置目录下添加两个配置文件
    sudo vim /etc/nginx/sites-available/a.com //创建配置文件
    

    内容如下:

    server {
            listen 80;
            root /home/scort/a; # 另一台b.com虚拟主机这里要写`/home/scort/b`, 其中`scort`是我服务器的用户名,要把它修改成你的
            index index.php index.html index.htm;
    
            # Make site accessible from http://localhost/
            server_name a.com; # 另一台改为b.com
    
            location / {
                    try_files $uri $uri/ =404;
            }
    
            # PHP支持
            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
            #   # `/etc/php/5.6/fpm/php-fpm.conf`查看php5.6-fpm.sock的文件位置,该文件位置与pid文件在同一位置
            #   # With php5-fpm:
                    fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; # php5.6-fpm.sock的文件位置
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    }
    
    • 修改php.ini中的;cgi.fix_pathinfo = 1;cgi.fix_pathinfo = 0;

    • 拷贝a.com另存一份b.com在配置中的已说明要修改的地方!

    接下来在Nginx的站点配置目录/etc/nginx/sites-enable/下软链配置文件

    cd /etc/nginx/sites-enable/
    sudo ln -s /etc/nginx/sites-available/a.com
    sudo ln -s /etc/nginx/sites-available/b.com
    
    • 重启服务
    sudo service php5.6-fpm restart
    sudo service nginx restart
    
    • 客户端访问
    sudo vim /etc/hosts
    添加如下内容
    172.16.0.51 a.com
    172.16.0.51 b.com
    

    浏览器访问

    http://a.com
    http://b.com
    

    Nginx 配置文件
    主配置文件 /etc/nginx/nginx.conf,Ubuntu 系统自动为我们的 Nginx 配置文件做了分片, 这样方便我们阅读, 编辑主配置文件.

    主配置放置一些配置信息, 新手暂时不用理会, 在配置文件 70 行有如下配置

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*; 
    

    /etc/nginx/conf.d/*.conf
    这里值得注意的是 conf.d 目录下放的也是 Nginx的配置, 在加载顺序上 conf.d 里面的配置在最下面, 它会覆盖原有配置文件里面的配置, 如果我们需要去修改配置, 要在 conf.d 里面修改, 而不是直接修改主配置文件, 这样就算改错了也能恢复.

    /etc/nginx/sites-enabled/*
    sites-enabled 是虚拟机主机配置文件, 我们去添加一个站点的不会再 nginx.conf 里面进行修改, 当然也不会再 sites-enabled 里面, 而是在 sites-available 文件夹里面, 从两个文件夹的字面意思也能知道 sites-available是可用站点, sites-enabled 是已用站点.

    相关文章

      网友评论

      本文标题:第五个模块 Ubunt14.04 LANMP环境、Nginx虚拟

      本文链接:https://www.haomeiwen.com/subject/wextyttx.html