【CentOS】LAMP

时间:2021-12-18 17:39:41

文章需要整合,学习需要归纳,博主把一连四篇的LAMP合并成为一片长篇的大部头,并梳理了一下他们的关系,希望对各位有所帮助

  最近一次更新:2016年12月21日21:38:31

本文为博主JerryChan所有,如需转载,请联系博主747618706@qq.com

/////////////////////////目录/////////////////////////////////////////

一、LAMP的安装

  (1)mysql的安装

  (2)Apache的安装

  (3)php的安装

二、LAMP的配置

  (1)mysql的配置

  (2)Apache的配置

  (3)php的安装

三、LAMP常见的问题

  A、安装相关的问题  

    (1)mysql

    (2)Apache

    (3)php

  B、日常使用的问题

    (1)mysql

    (2)Apache

    (3)php

四、LAMP的常用操作

  (1)mysql常用操作

  (2)Apache常用操作

  (3)php常用操作

五、LAMP相关拓展

//////////////////////////////////////////////////////////////////////

一、LAMP的安装
  (1)mysql的安装(免编译)
    1.解压后, mv mysql.... /usr/local/mysql
    2.useradd -s /sbin/nologin mysql
    3.mkdir -p /data/mysql
    4.chown -R mysql:mysqll /data/mysql
    5../scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    6.cp support-files/my-large.cnf /etc/init.d/my.cnf
    7.cp support-files/mysql.server /etc/init.d/mysqld
    8.chmod 755 /etc/init.d/mysql
    9.vim /etc/init.d/mysql在里面修改datadir=/data/mysql,basedir=/usr/local/mysql
    10.chkconfig --add mysql
    11.chkconfig mysqlon
    12.service mysqlstart 或者 /etc/init.d/mysqld start

    13.完事之后记得去配置mysqld文件的datadir和basedir,血的教训,浪费了好多时间

  (2)Apache的安装(编译)
    1../configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-so
    2.make && make install

  (3)php的安装(编译)
    1../configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6
    2.make && make install
    3.cp php.ini-development(因为我们是测试用,所以使用这个) /usr/local/php/etc/php.ini
    4.安装完毕后我们要拷贝配置文件 cp /usr/local/src//php.5.5/php.ini-development /usr/local/php/etc/php.ini

二、LAMP的配置
先放一张图来表示 Apache mysql 和 php 三者之间的关系
  (1)mysql的配置
      1.编辑文件 /etc/init.d/mysqld
      添加参数  basedir=/usr/local/mysql
              datadir=/data/mysql

      其中,mysql的配置文件为/etc/init.d/my.cnf

  (2)Apache的配置
      1.Apache主配置文件为:
      /usr/local/apache2/conf/httpd.conf
      其中,httpd 是 Hyper Text Transfer Protocol Daemon
            是 超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程

      2.默认的网站根目录:
      /usr/local/apache2/htdocs,所有的php文件都在里面执行

      3.(用discuz来演示)Apache的配置
      a. discuz 的安装     

        1.下载discuz
        2.mkdir /data/www
        3.cd /data/www/
        4.wget discuz的安装网站
        5.unzip Discuz....
        6.mv upload/* .
        7.rm - rf Dis....zip readme/ utility/

      b. 配置虚拟主机 VirtualHost      

        #Include conf/extra.httpd-vhosts.conf
          这是我们的子配置文件!!!
        vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

        加入如下配置
        <VirtualHost*:80>
          DocumentRoot"/data/www"
          ServerName www.123.com
        </VirtualHost>

      c. 配置mysql,给Discuz增加一个账户

        创建新的库,并且创建一个新的账号对该库有所有权限:
          >create database dazuo;
          >grant all on dazuo.* to 'root'@'localhost' identified by 'mycode';
          >quit

      d. 安装discuz     

        绑定hosts, 192.168.70.128 www.123.com

        (Windows的host在system32/etc/hosts)

        浏览器输入:www.123.com/install/

        根据提示,修改对应目录的权限:
        cd /data/www
        chown daemon:daemon data uc_server/data uc_client/data config

      e. 为某个虚拟主机配置用户认证        

        vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

        加上这一段
        <Directory /data/www> 或者是<Directory *>
          AllowOverride AuthConfig
          AuthName "name"
          AuthType Basic
          AuthUserFile /data/.htpasswd #这个目录可以随便写,没有限制
          require valid-user
        </Directory>

        注:.htpasswd是一个密码文件,
          用/usr/local/apache2/bin/htpasswd -c /data/.htpasswd username
          (第一次用要加-c)生成

      f. 域名跳转    

        <IfModule mod_rewrite.c>
          RewriteEngin on
          RewirteCond %{HTTP_HOST}^www.domain1.com$
          RewirteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
        </IfModule>

        如果是多个域名,可以这样设置:

        <IfModule mod_rewrite.c>
          RewriteEngin on
          RewirteCond %{HTTP_HOST}^www.domain.com$ [OR]
          RewirteCond %{HTTP_HOST}^www.domain1.com$
          RewirteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
        </IfModule>

      g. 配置Apache的访问日志

        rotatelog ,这个是我们的日志切割工具:
        我们在配置文件里面写入/usr/local/apache2/conf/extra/httpd-vhosts
        #配置日志
        ErrorLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/oem.discuz.qq.com-error _%Y%m%d:log 86400"
          SetEnvIF Request_URI ".*.gif$" image-request
          SetEnvIF Request_URI ".*\.jpeg$" image-request
          SetEnvIF Request_URI ".*\.png$"image-request
          SetEnvIF Request_URI ".*\.bmp$"image-request
          SetEnvIF Request_URI ".*\.swf$"image-request
          SetEnvIF Request_URI ".*\.js$"image-request
          SetEnvIF Request_URI ".*\.css"image-request
        CustomLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/1.com-access _%Y%m%d:log 86400 " combined env!=image-request
        (这个有两个格式,一个是common,一个是combined,在主配置文件里面的Format有定义)

      h. 配置静态文件缓存

        <IfModule mod_expires.c>
          ExpiresActive on
          ExpiresByType image/gif "access plus 1 days"
          ExpiresByType image/jpeg "access plus 24 hours"
          ExpiresByType image/png "access plus 24 hours"
          ExpiresByType text/css "now plus 2 hours"
          ExpiresByType application/x-javascript "now plus 2 hours"
          ExpiresByType application/x-shockwave-flash "now plus 2 hours"
          ExpiresDefault "now plus 0 min"
        </IfModule>
      (这个是用来设置最大缓存时间的,即max-age)

      i. 配置防盗链

          SetEnvIFNoCase Referer "^http://www.1.com" local_ref (对我来说是http://www.dazuo.com)
          SetEnvIFNoCase Referer "www.baidu.com" local_ref (http://www.chenzelin.com)
          SetEnvIFNoCase Referer "^$" local_ref          #这些都是白名单,除此之外不能访问
        <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">        (针对哪些做防盗链)
          Order Allow,Deny
          Allow from env=local_ref
        </filesmatch>

      j. 访问控制

        <Directory /data/www>
          Order deny,allow (Order,后面的deny,allow,哪个在前面,先执行哪个)
          Deny from all
          Allow from 127.0.0.1
        </Directory>

        针对请求的url去限制

        <filesmatch"(.*)admin(.*)">
          Order deny,allow
          Deny from all
          Allow from 127.0.0.1
        </filesmatch>

        某个登陆下禁止解析PHP

        <Directory /data/www/path>
            php_admin_flag engine off
          <filesmatch "(.*)php">
            Order deny,allow
            Allow from all
          </filesmatch>
        </Directory>

  (3)php的配置  

    1.php的配置文件为:
      /usr/local/php/etc/php.ini

    2.在/usr/local/apache2/conf/httpd.conf 下加上AddType application/x-httpd-php .php

    3.配置disable_function
    disable_functions=eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close

    4.配置错误日志error_log
    display_error=on
    (相当于是调试开关可看到具体的错误)

    但是这样会被别有用心的人看到
    那要怎么办呢,既记录日志又不给别人看
      三个步骤:
        1.display_error=off
        error_log=/path/to/logfile,例如:/usr/local/php/logs/error.log
        2.log_errors = On
        3.error_reporting = E_ALL|E_STRICT(日志级别)

    错误级别参考 http://www.aminglinux.com/bbs/thread-6973-1-1.html

       注意: /usr/local/php/logs/error.log,这个文件的权限,如果daemon没有写权限的话,写不了

    5.配置open_basedir(这是一个安全配置)
    应用场景:服务器有多个网站,把它分割开,令不同网站不相互影响

    在php.ini文件里面:open_basedir=/dir1/:/dir2

    eg: php.ini : open_basedir=/data/www2/:/tmp/
    这是限定一个的访问的目录,

    但是我们如果我们有几个网站的话,把所有的目录都打开,会很危险,那该怎么办呢

    *因为我们(不同的)虚拟主机都是在Apache里面配置的,所以这个时候我们就要去那里配置了,不同的VitualHost配置不同的base_dir
      /usr/local/apache/conf/extra/v...conf :
      在VirtualHost里面加上这句:
        php_admin_value open_basedir "/dir1/:/dir2"
        eg: php_admin_value open_basedir "/data/www2/:/tmp/"

    6.安装php的拓展模块

    (memcache,这是个缓存用的)
    http://www.aminglinux.com/thread-45-1-1.html

三、LAMP常见的问题
  A.安装相关问题
  (1)mysql
      a. error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
      解决办法 yum install -y libaio-devel

      b. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
      解决办法 替换掉系统原来自带的 my.cnf :
      mv /etc/my.cnf /etc/my.cnf_bak (原因未知,mysql版本号为 mysql-5.5.52-linux2.6-x86_64)

  (2)Apache
      a. configure: error: no acceptable C compiler found in $PATH
      解决办法,安装gcc

  (3)php
      a.  Sorry, I cannot run apxs. Possible reasons follow:
     The output of /usr/local/apache2/bin/apxs follows:
     ./configure: /usr/local/apache2/bin/apxs: /replace/with/path/to/perl/interpreter: bad interpreter: No such file or directory
      解决办法:因为没安装perl,先 yum install -y perl-devel ,然后修改apxs文件 ,把最上面一行改为/usr/bin/perl 解决

      b.上述安装步骤所需要的包
    configure: error: xml2-config not found. Please check your libxml2 installation.
      yum install -y libxml2-devel
    configure: error: Cannot find OpenSSL's <evp.h>
      yum install -y openssl openssl-devel
     configure: error: Please reinstall the BZip2 distribution,
      yum install -y bzip2 bzip2-devel
     configure: error: jpeglib.h not found.
      yum install -y libjpeg-devel
      configure: error: png.h not found.
      yum install -y libpng libpng-devel
     configure: error: freetype-config not found.
      yum install -y freetype freetype-devel
     configure: error: mcrypt.h not found. Please reinstall libmcrypt.
      yum install -y libmcrypt-devel

      c.动态编译memcache
     使用 /usr/local/php/bin/phpize 后报错:
       Cannot find autoconf. Please check your autoconf installation and the
     $php_AUTOCONF environment variable. Then, rerun this script.
      解决办法:
      yum install -y autoconf

  B.日常使用问题
     (1)mysql
    a. 在一次实验当中,不知道什么原因出现了错误
     ERROR 2002 (HY000): Can't connect to local mysql server through socket '/tmp/mysql.sock' (2)
     在网上排除了挺久的也没找到结果,然后在/data/mysql文件夹里面偶然间瞄到了一个错误日志,/data/mysql/主机名.error,错误日志如下
     InnoDB: The error means mysqld does not have the access rights to
     InnoDB: the directory.
     InnoDB: File name ./ibdata1
       显然,mysql没有权限访问这个文件,排查出整个data文件夹权限全部为root,修改权限后解决问题
       (论错误日志的重要性)



  (2)Apache

      a. 网页里面访问网页出现forbidden
      1.查看主配置文件,如果是Deny from all,改为Allow
      对于php7.0来说,默认是Require all denied,我们需要把denied改为granted

  (3)php
      a.遇到php无法解析
      1.先查看 /usr/local/apache2/bin/apachectl -M |grep 'php' ,看一下有没有装载 php5_module
      2.查看配置文件 /usr/local/apache2/conf/httpd.conf ,看一下有无 AddType application/x-httpd-php .php
      3.getenforce ,看看是否关闭了防火墙

      b.如果出现空白页但是状态码是200

      参考上述的php配置4、5,查看错误日志

四、LAMP的常用操作
  (1)mysql

    1.忘记root密码
      编辑主配置文件my.cnf,在[mysqld]字段下添加参数skip-grant,
      重启数据库,这样就可以进入数据库不用授权了mysqld -uroot
      修改相应用户密码use mysql ;
      update user set password=password('your password')where user='root';(更新user表)
      flush privileges;
        最后修改/etc/my.cnf去掉skip grant,重启mysql服务

    2.skip-innodb 我们可以增加这个参数不使用innodb引擎

    3.配置慢查询日志
    (慢查询是什么:mysql有一个功能就是可以log下来运行的比较慢的sql语句)
      #log_slow_queries=/path/to/slow_queries
      #long_query_time=1
      MyISAM和INNODB的对比,面试的时候可能会被问到的概率高达90%
      http://ask.apelearn.com/question/97 MyISAM和INNODB的对比

    4.mysql常用操作
      a.登陆相关的
       [mysql]
        -e Execute command and quit.
            eg:mysql -uroot -p123456 -e"command"
        -S The socket file to use for connection.(指定socket登陆)
            eg:mysql -uroot -p123456 /var/lib/mysql/mysql.sock
        -P Port number to use for connection or 0 for default to, in order of preference, my.cnf
            eg:mysql -uroot -p123456 -h127.0.0.1 -P3306
        -h Connect to host.
            eg:mysql -uroot -p123456 -h127.0.0.1 -P3306
        -p password
        -u user

       其中:
       用SOCKET形式登陆:mysql -uroot -p123456 -S /var/lib/mysq/mysq.sock (指定socket文件连接)
       用TCP/IP形式登陆: mysql -uroot -p123456 -h127.0.0.1 -P3306 (指定主机和端口号)

      b.数据库的操作
        查看:
        查看都有哪些库 show databases;
        查看某个库的表 use db;show table;
        查看表的字段 desc tb;
        查看建表语句 show create table tb;
        show create table tb\G格式化;
        查看数据库版本 select version();
        查看mysql状态 show status;
        查看mysql参数 show variables like 'max_connect%';
        查看mysql队列 show processlist;/show full processlist(相当于Linux里面的ps)

        创建:
        创建库 create database db1;
        创建表 create table t1(`id` int(4),
                `name` char(40));

        配置/权限相关:
        修改mysql参数 set global max_connect_errors=1000;
        (重启后还会更改,除非在my.cnf里面修改)

        创建普通用户并授权 grant all on *.* (什么库什么表的意思)to ’user1‘ identified by '123456'
        grant all on db1.* to 'user2'@'10.0.2.100'(来源IP) identified by '111222';
        grant all on db1.* to 'user3'@'%' identified by '23122'; insert into tb1(id,name) values(1,'aming');
        更改密码 UPDATE mysql.user SET password=PASSWIRD("newpasswd") WHERE user='username';
        (第一次登陆mysql没有加密码,可以这样 mysqladmin -uroot -p'12345')

        查询
        当前是哪个用户 select user()
        当前库 select database();
        查行数 select count(*) from mysql.user;
        select * from mysql.db;
        select * from mysql.db host like '10.0.%';(from mysql库user表)
        seletc * from mysql.db1 where user='user1'\G;

        更改:
        插入
        update db1.t1 set name='aaa' where id=1;
        insert into tb1(name,age) values('hello',99);

        删除 delete from tab1 where name='1'
        删除表 drop table t1
        删除数据库 drop database db1;
        清空表 truncate table tab1,只清空,不删除表


        修补表 repair table tb1 [use frm];use_frm 就是使用frm文件修复

        用于储存mysql命令历史的文件
        .mysql_history命令历史

       c.mysql备份与修复
        备份 mysqldump -uroot -p db > 1.sql
        只把创建表的语句备份出来 mysqldump -uroot -p -d db >/tmp/post.sql
        只备份一个表 mysqldump -uroot -p db tb1 >2.sql
        备份时指定字符集 mysqldump -uroot -p --default-character-set=utf8 db >1.sql
    
        修复 mysql -uroot -p db <1.sq;
        修复也指定字符集 mysql -uroot -p --default-character-set=utf8 db <1.sql

  (2)Apache
      1. /usr/local/apache2/bin/apachectl -M 查看加载了的模块
-                         -l 只查看静态加载的模块
                          -t 测试用的,如果有语法错误等的会报错
                         graceful 不杀死进程情况下重新调度配置文件

      2. 查看当初安装时候的选项,查看这个文件
        /usr/local/apache2/build/config.nice

      

  (3)php
      1. /usr/loca/php/bin/php phpname.php 执行脚本
                          -m 列出模块
                     -i 查看php的一些参数,不过我们大多数情况下会使用phpinfo()

     

五、拓展知识

  1.httpd.conf详解:

    http://www.php100.com/html/webkaifa/apache/2009/0418/1192.html

  2.mysql5.5编译安装

    http://ask.apelearn.com/question/1059

  3.Apache安装httpd.2.4.17:
    http://ask.apelearn.com/question/9674
  4.mysql5.5源码编译安装
    http://www.aminglinux.combbs/thread-1059-1-1.html
  5.mysql5.6源码安装报错
    http://www.aminglinux.com/bbs/thread-7743-1-1.html
  6.如何指定使用worker/prefork
    http://www.lishiming.net/thread-944-1-1.html
  7.apache3种工作方式(默认2.2为prefork,2.4为event)
    http://www.cnblogs.com/fnng/archive/2012/11/20/2779977.html
  8.apache的动态和静态
    http://www.cnblogs.com/eoiioe/archive/2008/12/23/1360476.html
    http://blog.sina.com.cn/s/blog_6238358c01017gdu.html
  9.php5.5,5.6编译安装方法
    http://www.aminglinux.com/bbs/thread-7284-1-1.html

  9.深入理解Apache虚拟主机

    http://yahoon.blog.51cto.com/13184/36239/

  第三方源: epel-release-6-7.noarch.rpm