zoukankan      html  css  js  c++  java
  • 使用LNMP构建企业web站点

    实验目的:

    使用LNMP架构搭建一个web站点

    一、环境准备

    1、一台Centos7服务器,配置好IP地址,服务器名称为nginx.web.com;

    2、为每台服务器配置yum源;

    3、准备好源码编译环境;

    4、实验所需要的软件包可使用以下方式获取。

    链接:https://pan.baidu.com/s/1pi1XsjFE8FL4LChfbDVoJg
    提取码:04as

    二、服务器初始状态准备

    1、为服务器配置好IP地址192.168.4.150(可以自己设定);

    2、检查防火墙是否关闭,如果没关闭可使用iptables -F关闭

    3、检查selinux是否关闭,如果没有关闭,可使用setenforce 0命令临时修改

    三、部署Nginx服务

    1、将准备好的软件包传入虚拟机

    [root@nginx ~]# mkdir tools    //创建存放软件包的目录

    [root@nginx ~]# cd tools 

    [root@nginx tools]# rz         //上传工具,通过yum install -y lrzsz来安装

    [root@nginx tools]# ls         //查看软件包是否都上传成功(共六个软件包)

    cmake-2.8.6.tar.gz      php-5.3.28.tar.gz

    mysql-5.5.22.tar.gz     SKYUC.v3.4.2.SOURCE.zip

    nginx-1.6.0.tar.gz        ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz

    (也可以联网,使用wget直接在网站上下载)

    2、安装编译环境以及Nginx的依赖包

    [root@nginx ~]# yum -y install gcc gcc-c++ make    //安装编译环境

    [root@nginx ~]# rpm -aq gcc gcc-c++ make           //检查软件包是否安装成功

    make-3.82-21.el7.x86_64

    gcc-4.8.5-4.el7.x86_64

    gcc-c++-4.8.5-4.el7.x86_64

    [root@nginx ~]# yum -y install pcre-devel zlib-devel openssl-devel    //安装Nginx的依赖包

    [root@nginx ~]# rpm -aq pcre-devel zlib-devel openssl-devel

    pcre-devel-8.32-15.el7.x86_64

    openssl-devel-1.0.1e-42.el7.9.x86_64

    zlib-devel-1.2.7-15.el7.x86_64

    3、对Nginx进行编译安装:

    检测系统是否安装Apache服务,如果安装则需要卸载Apache;

    [root@nginx ~]# rpm -aq httpd   

    (1)创建Nginx程序用户

    [root@nginx ~]# useradd -M -s /sbin/nologin nginx          //创建Nginx用户

    [root@nginx ~]# tail -1 /etc/passwd;tail -1 /etc/group       //检查用户是否创建成功

    (2)编译安装

    [root@nginx ~]# cd tools/                          //进入存放软件包的目录

    [root@nginx tools]# tar xf nginx-1.6.0.tar.gz -C /usr/src/       //解压nginx这个包到 /usr/src目录

    [root@nginx tools]# cd /usr/src/nginx-1.6.0/                        //进入解压完的目录文件中

    [root@nginx nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio  --with-http_mp4_module --with-http_ssl_module && make && make install  //编译安装

    [root@nginx nginx-1.6.0]# echo $?    //检查上一条命令是否执行成功,0表示成功

    0

    (3)修改Nginx配置文件

    [root@nginx ~]# cd /usr/local/nginx/conf/                //进入存放配置文件目录

    [root@nginx conf]# cp nginx.conf nginx.conf.bak    //备份

    [root@nginx conf]# vim nginx.conf                         //修改配置文件,以下是修改的文件的内容

    2 user  nginx nginx;
    
    5 worker_processes  1;
    
    7 error_log  logs/error.log  info;
    
    9 pid        logs/nginx.pid;
    
    12 events {
    
    13     use epoll; 
    
    14     worker_connections  10240;
    
    15 }
    
    18 http {
    
    19     include       mime.types;
    
    20     default_type  application/octet-stream;
    
    21
    
    22     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    
    23                       '$status $body_bytes_sent "$http_referer" '
    
    24                       '"$http_user_agent" "$http_x_forwarded_for"';
    
    25
    
    26     access_log  logs/access.log  main;
    
    27
    
    28     sendfile        on;
    
    29     tcp_nopush     on;
    
    36     server {
    
    37         listen       80;
    
    38         server_name  localhost;
    
    39
    
    40         charset utf-8;
    
    41
    
    42         access_log  logs/nginx.yunban.cn.access.log  main;
    
    43
    
    44         location / {
    
    45             root   html;
    
    46             index  index.html index.htm;
    
    47         }

    修改完保存并退出!

    (4)创建软连接

    [root@nginx conf]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

    (5)修改系统最大打开文件数

    [root@nginx conf]# ulimit -HSn 65535

    [root@nginx conf]# ulimit –n

    65535

    [root@nginx conf]# echo "ulimit -HSn 65535" >> /etc/profile

    [root@nginx conf]# tail -1 /etc/profile

    ulimit -HSn 65535

    (6)配置文件语法检测

    [root@nginx conf]# nginx -t

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    4、启动Nginx服务,并设置为开机自启动

    [root@nginx ~]# cd /usr/local/nginx/sbin

    [root@nginx sbin]# ./nginx

    [root@nginx sbin]# netstat -anput|grep 80  

    5、测试Nginx访问情况

    在浏览器中输入服务器的ip地址,出现以下页面表示成功;

    四、安装MySQL服务

    1、 检查是否安装Mariadb数据库,如果安装则使用rpm卸载,安装依赖包

    [root@nginx ~]# rpm -aq mariadb mariadb-server mariadb-client              //检查是否安装mariadb数据库

    [root@nginx ~]# yum -y install ncurses-devel                          //安装数据库依赖包

    [[root@nginx ~]# rpm -aq ncurses-devel  

    ncurses-devel-5.9-13.20130511.el7.x86_64

    2、 安装cmake为作为MySQL编译工具

    [root@nginx ~]# cd tools/

    [root@nginx tools]# tar xf cmake-2.8.6.tar.gz -C /usr/src/

    [root@nginx tools]# cd /usr/src/cmake-2.8.6/

    [root@nginx cmake-2.8.6]# ./configure && gmake &&gmake install      //编译安装

    [root@nginx cmake-2.8.6]# echo $?

    0

    3、 编译安装MySQL数据库

    [root@nginx cmake-2.8.6]# cd /root/tools/

    [root@nginx tools]# tar xf mysql-5.5.22.tar.gz -C /usr/src/

    [root@nginx tools]# cd /usr/src/mysql-5.5.22/

    [root@nginx mysql-5.5.22]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install           //编译安装数据库

    4、 安装后配置

    [root@nginx mysql-5.5.22]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile 

    [root@nginx mysql-5.5.22]# . /etc/profile 

    [root@nginx mysql-5.5.22]# echo $PATH

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

    准备相关配置文件:

    [root@nginx~]# /bin/cp-p /usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf

    [root@nginx ~]# /bin/cp -p /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld

    [root@nginx ~]# chmod +x /etc/init.d/mysqld         //授予执行权限

    [root@nginx ~]# chkconfig --add mysqld

    [root@nginx ~]# chkconfig mysqld on

    5、 初始化数据库

    [root@nginx ~]# useradd -M -s /sbin/nologin mysql              //创建mysql用户

    [root@nginx ~]# chown -R mysql:mysql /usr/local/mysql/     //修改所属组

    [root@nginx ~]# /usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

    6、 启动MySQL数据库,并为MySQL的root用户创建密码

    [root@nginx ~]# /etc/init.d/mysqld start       //启动数据库

    Starting MySQL... SUCCESS!

    [root@nginx ~]# netstat -anptu|grep 3306    //查看数据库是否启动

    [root@nginx ~]# mysqladmin -uroot  password "123456" ;history -c    //为root用户创建密码

    7、 登录数据库

    登录成功!

    五、部署PHP服务

    1、 安装PHP服务

    [root@nginx ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel    

    [root@nginx ~]# cd tools/

    [root@nginx tools]# tar xf php-5.3.28.tar.gz -C /usr/src/

    [root@nginx tools]# cd /usr/src/php-5.3.28/

    [root@nginx php-5.3.28]# ./configure  --prefix=/usr/local/php5  --with-gd  --with-zlib --with-mysql=/usr/local/mysql/  --with-config-file-path=/usr/local/php5  --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install                                //编译安装php服务

    [root@nginx php-5.3.28]# echo $?

    0

    2、 安装后调整优化

    [root@nginx php-5.3.28]# cp -p /usr/src/php-5.3.28/php.ini-development /usr/local/php5/php.ini

    [root@nginx php-5.3.28]# ln -s /usr/local/php5/bin/* /usr/local/bin/                 //创建软连接

    [root@nginx php-5.3.28]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/

    3、 安装 ZendGuardLoader (PHP  的优化模块)

    [root@nginx php-5.3.28]# cd /root/tools/

    [root@nginx tools]#tar xf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz -C /usr/src/

    [root@nginx tools]cp /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/php5/lib/php/

    [root@nginx tools]# echo -e "zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so zend_loader.enable=1" >> /usr/local/php5/php.ini

    [root@nginx tools]# tail -2 /usr/local/php5/php.ini

    zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so

    zend_loader.enable=1

    4、 配置并启动

    [root@nginx tools]# cd /usr/local/php5/etc/

    [root@nginx etc]# cp -p php-fpm.conf.default php-fpm.conf

    [root@nginx etc]# vim php-fpm.conf

    25 pid = run/php-fpm.pid //指定 pid 文件位置
    
    140 user = nginx //程序用户
    
    141 group = nginx //程序组
    
    217 pm.max_children = 50 //子进程的最大数
    
    222 pm.start_servers = 20 //启动时开启的进程数
    
    227 pm.min_spare_servers = 5 //最少空闲进程数
    
    232 pm.max_spare_servers = 35 //最大空闲进程数

    5、启动PHP

    [root@nginx etc]# php-fpm

    [root@nginx etc]# netstat -anput|grep php    //查看php是否启动

    6、编写Nginx服务控制脚本

    [root@nginx etc]# vim /etc/init.d/nginx

    #!/bin/bash
    # chkconfig: 2345 99 20
    # description: Nginx Server Control Script
    PROG="/usr/local/nginx/sbin/nginx"
    PIDF="/usr/local/nginx/logs/nginx.pid"
    PROG_FPM="/usr/local/sbin/php-fpm"
    PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
    case "$1" in
            start)
                    if [ -f $PIDF ];then
                            echo "Nginx is running.. Start it is error"
                    elif [ -f $PIDF_FPM ];then
                            echo "PHP is running.. Start it is error"
                    else
                            $PROG &>/dev/null
                            $PROG_FPM &>/dev/null
                    fi
            ;;
            stop)
                    if [ -f $PIDF ] && [ -f $PIDF_FPM ];then
                            kill -s QUIT $(cat $PIDF) &> /dev/null
                            kill -s QUIT $(cat $PIDF_FPM) &>/dev/null
                    else
                            echo "Nginx or PHP is not running.. Stop it is error"
                    fi
            ;;
            restart)
                    $0 stop
                    $0 start
            ;;
            reload)
                    kill -s HUP $(cat $PIDF)
            ;;
            *)
                    echo "Usage: $0 (start|stop|restart|reload)"
                    exit 1
    esac
    exit 0

    [root@nginx ~]# chmod +x /etc/init.d/nginx          //给脚本授予执行权限

    [root@nginx ~]# echo "/etc/init.d/nginx start">> /etc/rc.d/rc.local

    [root@nginx ~]# chmod +x /etc/rc.d/rc.local

    [root@nginx ~]# tail -1 /etc/rc.d/rc.local

    /etc/init.d/nginx start

    7、测试脚本

    [root@nginx ~]# /etc/init.d/nginx start

    [root@nginx ~]# netstat -anput|egrep "nginx|php"     //查看是否启动成功

     8、配置 Nginx支持 PHP解析

    [root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

    46             index  index.php index.html index.htm;
    
    60         location ~ .php$ {
    
    61                 root html;
    
    62                 fastcgi_pass 127.0.0.1:9000;
    
    63                 fastcgi_index index.php;
    
    64                 include fastcgi.conf;
    
    65         }

    修改完保存并退出!

    [root@nginx ~]# nginx -t      //查看配置是否成功

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    [root@nginx ~]# /etc/init.d/nginx reload     //重启脚本

    9、测试

    [root@nginx ~]# vim /usr/local/nginx/html/index.php

    <?php
    
    phpinfo();
    
    ?>

    10、测试结果如下

    11、测试通过PHP访问数据库

    [root@nginx ~]# vim /usr/local/nginx/html/test.php

    [root@nginx ~]# cat /usr/local/nginx/html/test.php

    <?php
    
    $link=mysql_connect('localhost','root','123123');
    
    if($link) echo "<h1>successful</h1>";
    
    mysql_close();
    
    ?>

    12、测试结果如下

    如果没出现以下页面就去检查配置文件是否有错误;

    六、部署SKYUC应用

    1、 解压SKYUC,部署程序代码  

    [root@nginx ~]# yum -y install unzip     //安装解压缩工具

    [root@nginx ~]# rpm -aq unzip

    unzip-6.0-15.el7.x86_64

    [root@nginx ~]# cd tools/

    [root@nginx tools]# unzip SKYUC.v3.4.2.SOURCE.zip

    [root@nginx tools]# cd SKYUC.v3.4.2.SOURCE

    [root@nginx SKYUC.v3.4.2.SOURCE]# cp -rp wwwroot/ /usr/local/nginx/html/skyuc

    [root@nginx SKYUC.v3.4.2.SOURCE]# cd /usr/local/nginx/html/skyuc/

    [root@nginx skyuc]# chown -R nginx:nginx ./

    2、 创建数据库,并进行授权

    [root@nginx ~]# mysql -uroot -p123123

    mysql> create database skyucdb;        //创建名为skyucdb的数据库
    
    Query OK, 1 row affected (0.00 sec)
    
    mysql> grant all on skyucdb.* to runskyuc@localhost identified by 'admin123';    //创建runskyuc用户密码为admin123
    
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit
    
    Bye

    3、web界面安装

    点击下一步:

    填写数据库信息,设置管理员:

    安装成功:

    4、删除install的安装目录:

    [root@nginx ~]# cd /usr/local/nginx/html/skyuc/

    [root@nginx skyuc]# rm -rf install/

    5、登录页面:

    管理员登录:

    登录管理界面:

    实验成功!(* ̄︶ ̄)

     

  • 相关阅读:
    八字案例董易奇
    nginx和tomcat二合一服务器配置SSL证书
    RecyclerView,内容不居中的解决办法。
    cxf接口生成WSDL带密码的code实践
    去除server.key的密码
    Window下openssl的安装教程(通俗易懂)
    Nginx配置https证书
    自己做CA
    自己生成ssl证书
    自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书
  • 原文地址:https://www.cnblogs.com/hmm01031007/p/11569070.html
Copyright © 2011-2022 走看看