LNMP的虚拟主机配置http和https

项目实战:
    搭建LNMP环境:Linux+Nginx+Mysql(MariaDB)+php(php-fpm),创建多个虚拟主机:
    主机1提供正常的http服务,用于安装wordpress博客;
    主机2提供正常的https服务,用于安装phpMyAdmin,用图形界面去管理MySQL(MariaDB)数据库;

项目实战步骤:
1.安装nginx:
    (1)使用已经下载好的nginx的rpm包进行安装:
        CentOS6:yum install -y nginx-1.6.2-1.el6.ngx.x86_64.rpm
        CentOS7:yum install -y nginx-1.10.0-1.el7.ngx.x86_64.rpm
    (2)使用阿里云的epel源进行直接安装
        CentOS6:http://mirrors.aliyun.com/epel/6/x86_64
        CentOS7:http://mirrors.aliyun.com/epel/7/x86_64
        yum -y install nginx
2.安装数据库:
        CentOS6:yum -y install mysql mysql-server
        CentOS7:yum -y install mariadb mariadb-server
        vim /etc/my.cnf
        [mysqld]
        skip_name_resolve=ON
        innodb_file_per_table=ON
        mysql_secure_installation(启动服务之后,进行初始化mysql数据库)
3.安装php的插件:
        yum install -y php-fpm php-mysql php-gd php-mcrypt php-mbstring
4.启动服务:
        CentOS6:
            service nginx start
            service mysqld start
            service php-fpm start
        CentOS7:
            systemctl start nginx.service
            systemctl start mariadb.service
            systemctl start php-fpm.service
5.关闭防火墙和SELinux:
        iptables -F
        setenforce 0
6.http配置wordpress;
    (1)配置php-fpm:
        vim /etc/php-fpm.d/www.conf
        将里面的user和group 由apache更改为nginx
        创建/var/lib/php/session并将其属主属组更改为nginx
        mkdir /var/lib/nginx/session
        chown -R nginx:nginx /var/lib/nginx/session
        systemctl restart php-fpm.service 
    (2)配置php-fpm的虚拟主机:
        将默认的default.conf进行备份:
        mv /etc/nginx/conf.d/default.conf{,.bak}
        新创建虚拟nginx主机的配置文件:
        vim /etc/nginx/conf.d/wordpress.conf
        server {
            listen 80;
            server_name www.sjsir.com;
            location / {
                root /var/www/html;
                index index.php index.html index.htm; 
            }
            location ~* \.(php)$ {
                root /var/www/html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
                include fastcgi_params; 
            }

        }
    (3)搭建wordpress;(wordpress-4.6.1.zip设置界面为英文版)
        cd /var/www/html
        rz wordpress-4.6.1.zip
        unzip wordpress-4.6.1.zip
        rm -rf wordpress-4.6.1.zip
        mv wordpress/* .
    (4)为wordpress创建mysql数据库的数据;
        #mysql -uroot -p(root用户的mysql密码)
        mysql>CREATE DATABASE wordpress;
        mysql>GRANT ALL ON wordpress.* TO 'wordpress'@'127.0.0.1' IDENTIFIED BY 'wordpresspass';
        mysql>exit
    (5)用浏览器访问192.168.1.104:
        点击Let's go!
        blob.png

        按照之前设置的信息,将下面修改正确,之后Submit;

        blob.pngimages\4-2.png
        将上图蓝色中的内容复制下来,并创建后粘贴到/var/www/html/wp-config.php

        blob.pngimages\4-3.png
            vim /var/www/html/wp-config.php
            粘贴到此处
            回到浏览器点击Run the install 
            按照下图填入必要信息:
         blob.png

            然后会出现以下页面,表示安装成功;
        blob.png   
            登录wordpress,会发现英文版的wordpress-4.6.1已经安装成功:

        blob.pngimages\4-6.png
7.https配置pma:
    (1)配置pma的虚拟主机:
        server {
            listen 443 ssl;
            server_name www.sjsir.wang;
            ssl on;
            ssl_certificate /etc/nginx/ssl/nginx.crt;
            ssl_certificate_key /etc/nginx/ssl/nginx.key;
            ssl_session_cache shared:sslcache:20m;
            location / {
                root /var/www/pma;
                index index.php index.html index.htm;
            }
            location ~* \.(php)$ {
                root /var/www/pma;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /var/www/pma$fastcgi_script_name;
                include fastcgi_params;
            }
        }
    (2)配置https:
        cd /etc/pki/CA/
        (umask 077;openssl -out private/cakey.pem 4096)
        openssl genrsa -new -x509 private/cakey.pem -out cecurt.pem
            CN Beijing Beijing Sjsir Ops ca.sjsir.cn
        touch index.txt
        echo 01 > serial
        cd /etc/nginx
        mkdir ssl
        cd ssl
        (umask 077;openssl genrsa -out nginx.key 4096)
        openssl req -new -key nginx.key -out nginx.csr

            CN Beijing Beijing Sjsir Ops ca.sjsir.cn

       openssl ca -in nginx.csr -out nginx.crt
    (3)配置pma:
        mkdir /var/www/pma
        cd /var/www/pma
        rz phpMyAdmin-4.0.5-all-languages.zip
        unzip phpMyAdmin-4.0.5-all-languages.zip
        mv phpMyAdmin/* .
        rm -rf phpMyAdmin/
        mv config.sample.inc.php config.inc.php 
    (4)测试:https://192.168.1.104
        就可以看到:

       blob.png
        输入账号和密码登录进去,就可以看到mariadb数据库:
        blob.png        
    注意:
        (1)在实验之前一定要关闭防火墙和SELinux,以免系统因为iptables和SELinux的设置而连接不上;
        (2)配置pma时,一定要配置php的fastcgi模块,或单独的php模块,不配置的话,用php编写的pma就无法使用;
        (3)配置pma时,一定要安装php-mcrypt模块,该模块位于epel源中(以阿里云的7系列的epel源:http://mirrors.aliyun.com/epel/7/x86_64)。
        (4)搭建该实验环境时,应该注意参考日志文件,以确定发生什么样的问题,/var/log/messages /var/log/nginx/error.log /var/log/nginx/access.log
            当测试时,发现无法连接服务器,例如500的错误,需要查看/var/log/nginx/error.log
            当测试时,发现页面能打开,但是不正常显示,而且/var/log/nginx/error.log日志无日志记录时,一定要去查看/var/log/nginx/access.log日志信息。有时服务器已经响应了,但是可能由于wordpress或pma的安装包配置的问题而报错,error.log不会存储这些已经响应的日志,需要去查看access.log。

原创文章,作者:sjsir,如若转载,请注明出处:http://www.178linux.com/56083

(0)
sjsirsjsir
上一篇 2016-10-30 16:48
下一篇 2016-10-30 16:52

相关推荐

  • 文件查看和正则表达式

    文件查看工具         cat,tac,rev,more,less,head,tail cat:查看文本          cat [OPTION]… [FILE]… &…

    Linux干货 2016-08-07
  • 软链接和硬链接的区别

    软链接和硬链接的区别 什么是链接: 文件都是文件名和数据组成的,在linux中被分成两部分:数据和元数据。数据是记录文件的真实内容的地方;元数据是记录文件属性的地方:创建者、大小、创建时间等信息。元数据中的inode号这是唯一标识文件身份的属性。在linux中,文件的inode号可以通过ls –i命令查看。在linux中为了解决文件共享使用,引入了两种链接:…

    Linux干货 2016-10-20
  • N25-第六周博客作业

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@han ~]# cp /etc/rc.d/rc.sysinit /tmp [root@han ~]#&n…

    Linux干货 2017-02-16
  • 关于HISTCONTROL命令及对快捷键Ctrl+o命令的影响

            在linux中环境变量HISTCONTROL可以控制历史的记录方式。         HISTCONTROL有以下的选项:         &n…

    Linux干货 2017-02-18
  • 文本处理工具-2

    1、 sed 是一种行/流编辑器,它一次处理一行内容;处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ),接着用sed 命令处理缓冲区中该行内容,处理完成后,把缓冲区的该行内容送往屏幕,接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出,Sed 可以用来自动编辑一个或多个文件。 2…

    Linux干货 2016-08-15
  • 使用pyenv管理不同版本的python

    安装: 安装: $ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 在你的shellrc文件中添加: export PATH=”$HOME/.pyenv/bin:$PATH” eval “$(pyenv init …

    Linux干货 2015-03-12