由于磁盘分区的事情,赢得领导信任。负责网站服务器的同事有事暂时需要请假,领导把你叫到办公室,给你看了一下图1让你想办法实现:

LMAP+NFS_blank

              图 1

领导的意思是:

    1、客户通过客户访问公司网站,外网映射方面由其它同事搞定,所有静态网站在httpserver。

   2、PHP资源本应该都存在php服务器里,但是因为前些日子因为存储空间不足,需要把新资源放在nsfserver服务器里。

  3.有些动态网页需要连接数据库服务器,经数据库执行后返回结果给php服务器和http服务器。

  说完他又打开一张图片,对你说想了解上面那张图怎么工作的,那么你就要知道lamp的流程走向,见图2

    LMAP+NFS_blank_02

                         图2

一、看了这个图更觉得自己要怎么都要想办法实现领导布置的任务,你想了想觉得先要了解以下内容:

  1、什么叫lmap?

  lamp就是架设在linux服务器上的apache[httpd]+mysql(mariadb)[数据库]+php(python,perl,ruby)[脚本语言] 的组合。

  2、marriadb有什么优点让自己选择它而不选择mysql?

      1)更多的存储引擎
         MyISAM --> Aria
         innodb --> XtraDB        
      2)速度显著提升
      3)诸多扩展及新特性
      4)较多的测试组件
      5)较少的bug
      6) 真开源

      7)兼容mysql大多数命令

  3、什么是nfs

NFS--Network File System :Unix/linux系统间的文件共享,主要用于局域网;可以将NFS服务器共享的目录挂载到本地,然后像访问本地目录一样使用。

二、实施安装部署lamp+nfs

  一)安装nfsserver

  1、  yum install nfs-utils –y

    LMAP+NFS_网站服务器_03

2 、开启服务:

      service rpcbind start

      service nfs start

      LMAP+NFS_办公室_04

  3、验证nfs服务器是否开启:

    rpcinfo -p 192.168.3.51

    LMAP+NFS_target_05

4、设置nfs共享目录:

   # vim /etc/exports

   /mageedu/ 192.168.3.0/24(rw,sync,all_squash,anouid=1025,anongid=1025)

LMAP+NFS_办公室_06

    这个配置文件就这样简单一行。共分为三部分,第一部分就是本地要共享出去的目录,第二部分为允许访问的主机(可以是一个IP也可以是一个IP段)第三部分就是小括号里面的,为一些权限选项。第三部分,参数详解如下 :

    rw :读写;

    ro :只读;

    sync :同步模式,内存中数据时时写入磁盘;

    async :不同步,把内存中数据定期写入磁盘中;

    no_root_squash :加上这个选项后,root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。不安全,不建议使用;

    root_squash :和上面的选项对应,root用户对共享目录的权限不高,只有普通用户的权限,即限制了root;

    all_squash :不管使用NFS的用户是谁,他的身份都会被限定成为一个指定的普通用户身份;

    anonuid/anongid :要和root_squash 以及 all_squash一同使用,用于指定使用NFS的用户限定后的uid和gid,前提是本机的/etc/passwd中存在这个uid和gid。

  5、在PHP服务器上加载nfs /mageedu为 /data/myimg

       1)setfacl -m group:nfsuser1:rwx /mageedu 服务器上给访问权限

       LMAP+NFS_target_07

       2)php服务器

       mkdir –pv /data/myimg

       mount -t nfs 192.168.3.51:/mageedu/ /data/myimg/

       LMAP+NFS_style_08

       3)开机自动加载

        vim /etc/fstab

    192.168.3.51:/mageedu/ /data/myimg              nfs     defaults,_netdev          0 0

       LMAP+NFS_target_09

二)安装数据库mariadb

   1、 下载文件mariadb-5.5.36-linux-x86_64.tar.gz到192.168.3.56,并解压

    tar xf mariadb-5.5.36-linux-x86_64.tar.gz –C /usr/local

    cd /usr/local

    ln –sv mariadb-5.5.36-linux-x86_64 mysql

    mkdir –pv /data/mydata

    groupadd -g 306 mysql

    useradd -u 306 -g 306 mysql

    LMAP+NFS_target_10

  /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mydata --user=mysql

LMAP+NFS_style_11

  LMAP+NFS_target_12

  chown :mysql /data/mydata/*

  chmod +x /data/mydata

  chmod 640 /data/mydata

  LMAP+NFS_blank_13

2、cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf

vim /etc/mysql/my.cnf

并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2

另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data

3、为mysql提供sysv服务脚本:

# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysql
# chmod +x /etc/rc.d/init.d/mysql

4、添加至服务列表:
# chkconfig --add mysql
# chkconfig mysql on

5、编辑路径给marriadb能够启动

vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH wq保存退出

chmod +x /etc/profile.d/mysql.sh

. /etc/profile.d/mysql.sh 重新载入路径配置文件

vim /etc/selinux/config

SELINUX=disabled wq保存退出重启机器

LMAP+NFS_style_14

6、mysql的进入

#service  mysql start

LMAP+NFS_blank_15

请vim /etc/mysql/my.cnf

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

添加以上字段,没有启动起来是配置文件缺少PID启动文件

#mysql

LMAP+NFS_网站服务器_16

三)数据库的创建与管理

1、获得任何帮助信息 请打help或者\h +空格+关键字;

LMAP+NFS_办公室_17

2)任何一个数据库都有其自己的排序规则和所支持的字符集

SHOW COLLATION; 显示mariadb支持的排序规则

LMAP+NFS_target_18

3)显示所支持的字符集:

SHOW CHARACTER SET;

LMAP+NFS_网站服务器_19

2、创建数据库

1)CREATE DATABASE  IF NOT EXISTS testdb ;

LMAP+NFS_blank_20

2)修改数据库支持字符集

ALTER DATABASE testdb CHARACTER SET utf8;

LMAP+NFS_办公室_21

3)删除数据库

DROP DATABASE IF EXISTS testdb;

LMAP+NFS_target_22

4)打开数据库

USE databasename;

LMAP+NFS_style_23

3、表操作

1)创建表

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
                    (create_definition,...)|PRIMARYKEY(col1,col2,..) UNIQUE KEY (col1,clo2)

IF NOT EXISTS :如果表存在就退出

  PRINMARYKEY : 主键约束。

  UNIQUE KEY :唯一建约束。

CREATE TABLE IF NOT EXISTS paiming (SID INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, NAME VARCHAR(50) NOT NULL,AGE TINYINT UNSIGNED,JUEJI VARCHAR(60) NOT NULL,UNIQUE (NAME,JUEJI),GENDER ENUM('M','F') DEFAULT 'M');

LMAP+NFS_网站服务器_24

 

2)给表插入数据

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

INSERT INTO paiming (NAME,AGE,JUEJI,GENDER) VALUES('张无忌',24,'九阳神功','F'),('周芷若',20,'峨眉剑法','M');

LMAP+NFS_target_25

3)查询表单数据

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

SELECT * FROM paiming where age<=30;

LMAP+NFS_网站服务器_26

4)更新表

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

update paiming set GENDER ='F'LIMIT 3;

update paiming set GENDER ='M'where age >20;

LMAP+NFS_target_27

5)删除表

DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]

DROP TABLE paiming;

LMAP+NFS_办公室_28

 

4、索引操作

1)建立索引

CREATE [ONLINE|OFFLINE] [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (index_col_name,...)
    [index_option] ...

index_col_name:
    col_name [(length)] [ASC | DESC]

index_type:
    USING {BTREE | HASH}

index_option:
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER parser_name
  | COMMENT 'string'

CREATE INDEX DAZHAO ON paiming (JUEJI DESC);

LMAP+NFS_target_29

2)查看索引

SHOW {INDEX | INDEXES | KEYS}
    {FROM | IN} tbl_name
    [{FROM | IN} db_name]
    [WHERE expr]

SHOW INDEX IN paiming From testdb;

LMAP+NFS_办公室_30

3)删除索引:

DROP [ONLINE|OFFLINE] INDEX index_name ON tbl_name

DROP INDEX DAZHAO FROM paiming;
LMAP+NFS_target_31

5、用户管理与权限

1)创建用户

CREATE USER user_specification
    [, user_specification] ...

user_specification:
    user
    [
        IDENTIFIED BY [PASSWORD] 'password'
      | IDENTIFIED WITH auth_plugin [AS 'auth_string']
    ]

CREATE USER 'USERNAME'@'HOST' IDENTIFIED BY 'PASSWORD';

HOST:           主机名;
                IP地址或Network;
                通配符:
                    %, _: 172.16.%.%

CREATE USER 'mytest'@'192.168.3.%' IDENTIFIED BY 'test@1996';

LMAP+NFS_blank_32

2)更改用户

RENAME USER old_user TO new_user
    [, old_user TO new_user] ...

RENAME USER 'test1'@'192.168.3._' TO 'mytest'@'192.168.3.%';

LMAP+NFS_网站服务器_33

3)更改密码

SET PASSWORD [FOR user] =
    {
        PASSWORD('cleartext password')
      | OLD_PASSWORD('cleartext password')
      | 'encrypted password'
    }

select password ('test@1996');将明文加密

SET PASSWORD for 'mytest'@'192.168.3.%' = '*C8CDFE2C06E199C8D1D78EF6D257D56660679043';

LMAP+NFS_target_34

4)授权

GRANT priv_type[,...] ON [{table|function|procedure}] db.{table|routine} TO 'USERNAME'@'HOST' [IDENTIFIED BY 'password']
            [REQUIRE SSL] [WITH with_option]

            with_option:
                GRANT OPTION
              | MAX_QUERIES_PER_HOUR count
              | MAX_UPDATES_PER_HOUR count
              | MAX_CONNECTIONS_PER_HOUR count
              | MAX_USER_CONNECTIONS count

GRANT all ON testdb.*  TO 'mytest'@'192.168.3.%';

5)取消授权

REVOKE
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM user [, user] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...

REVOKE PROXY ON user
    FROM user [, user] ...

REVOKE INSERT,DELETE ON testdb.* FROM 'mytest'@'192.168.3.%';

LMAP+NFS_网站服务器_35

6)查看授权

show grants for 'mytest'@'192.168.3.%';

LMAP+NFS_target_36

7)删除用户

DROP USER user [, user] ...

DROUP USER 'mytest'@'192.168.3.%';

三)Apache httpd2.4编译安装

1、下载所需源码包并解压

wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.17.tar.gz

wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gz

wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz

tar xf apr-1.5.2.tar.gz

tar xf apr-util-1.5.4.tar.gz

tar xf httpd-2.4.17.tar.gz

2.编译安装apr 和apr-util

1)yum groupinstall ‘Development Tools’’Development Tools’ –y

    yum install pcre-devel openssl-devel zlib-devel -y

安装支持库组件

2)cd apr-1.5.2

   ./configure --prefix=/usr/local/apr

   make && make install

3)cd /usr/download

  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

  make && make install

4)编译安装apache httpd2.4

cd  httpd-2.4.17

./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-proxy --enable-proxy-fcgi --enable-so --sysconfdir=/etc/httpd24 --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms=shared=all --with-mpm=event

make && make install

vim /etc/httpd24/httpd.conf

开启 ServerName localhost:80

开启 LoadModule slotmem_shm_module modules/mod_slotmem_shm.so

开启 PidFile  "/var/run/httpd.pid"

5)编写服务脚本:

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#        HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
  echo -n $"Stopping $prog: "
  killproc -p ${pidfile} -d 10 $httpd
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
        status -p ${pidfile} $httpd
  RETVAL=$?
  ;;
  restart)
  stop
  start
  ;;
  condrestart)
  if [ -f ${pidfile} ] ; then
    stop
    start
  fi
  ;;
  reload)
        reload
  ;;
  graceful|help|configtest|fullstatus)
  $apachectl $@
  RETVAL=$?
  ;;
  *)
  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
  exit 1
esac

exit $RETVAL  保存退出

chmod +x /etc/rc.d/init.d/httpd

service start httpd

四)php的编译安装

1、yum install bzip2-devel libmcrypt-devel libxml2-devel openssl openssl-devel软件支持包

2、下载php安装包,解压编译

    wget  http://ar2.php.net/get/php-5.5.30.tar.gz

    tar xf php-5.5.30.tar.gz

    cd php-5.5.30

    ./configure --prefix=/usr/local/php --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-libxml-dir=/usr --with-openssl  --with-zlib --with-bz2 --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-mcrypt --enable-sockets --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd

LMAP+NFS_blank_37

make && make install

LMAP+NFS_style_38

3、修改配置文件

1)PHP的配置文件

cp php.ini-production /etc/php.ini

2)配置启动脚本

cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

3)给脚本执行权限

chmod +x /etc/rc.d/init.d/php-fpm

4)开机启动

chkconfig --add php-fpm

chkconfig php-fpm on

chkconfig --list php-fpm

LMAP+NFS_网站服务器_39

5)配置php-fpm脚本

cd /usr/local/php/etc/

cp php-fpm.conf.default php-fpm.conf

vim  php-fpm.conf

listen = 192.168.3.51:9000 #这里替换成你的PHP主机IP。

pm.max_children = 30

pm.start_servers = 3

pm.min_spare_servers = 2

pm.max_spare_servers = 6

LMAP+NFS_target_40

service php-fpm start

LMAP+NFS_blank_41

五)php与apache的结合

1、在php主机上 mkdir –pv /web/{www,myadmin},在http主机上也执行相同操作。

LMAP+NFS_style_42

2、编辑配置文件开启虚拟主机

1)[root@www ~]# vi /etc/httpd24/httpd.conf

Listen *:8080

#DocumentRoot "/usr/local/apache/htdocs" 注释掉主服务器

Include /etc/httpd24/extra/httpd-vhosts.conf 开启虚拟主机配置

找到AddType添加PHP和PHPS文件支持

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

搜寻DirectoryIndex

添加 index.php索引

LMAP+NFS_blank_43

找到并开启proxy和proxy-fcgi模块

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

LMAP+NFS_网站服务器_44

2)编辑虚拟主机配置文件

[root@www /]# vim /etc/httpd24/extra/httpd-vhosts.conf

<VirtualHost *:8080>
#    ServerAdmin webmaster@lj.com
    DocumentRoot "/web/www/"
#    ServerName  www.lj.com
#   ServerAlias www.lijia.com
    ErrorLog "logs/www.lj.com-error_log"
    CustomLog "logs/www.lj.com-access_log" common
    ProxyRequests Off #关闭正向代理
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.3.51:9000/web/www/$1
   设定反向代理路径

    <Directory "/web/www"> 设定目录权限

        Options none

        AllowOverride none

        Require all granted

    </Directory>

</VirtualHost>

3、安装配置 xchache

  [root@php download]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
  tar xf xcache-3.2.0

  cd xcache-3.2.0

  运行/usr/local/php/bin/phpize生成configure文件

[root@php xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

 LMAP+NFS_target_45

make && make install

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

mkdir /etc/php.d

cp xcache.ini /etc/php.d/

vim xcache.ini

[xcache-common]
;; non-Windows example:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/xcache.so 修改xcache动态库路径,wq保存

重启php-fpm服务 service php-fpm restart

在/web/www/目录下建立index.php

vim /etc/www/inidex.php

<?php
  $link = mysql_connect('192.168.3.56','test1','123qwe');

        if($link)

                echo "Success...";

        else

                echo "Failed....";

        mysql_close;

        phpinfo();
?>

浏览器输入

http://192.168.3.52:8080

LMAP+NFS_办公室_46

搞定,等待领导验收!