zoukankan      html  css  js  c++  java
  • LAMP.md

    LAMP

    Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。随着开源潮流的蓬勃发展,开放源代码的LAMP已经与J2EE和.Net商业软件形成三足鼎立之势,并且该软件开发的项目在软件方面的投资成本较低,因此受到整个IT界的关注。

    工作流程

    我们在日常上网访问网站时,大部分时间都是查看网站的内容,而这种使用都是通过web服务器(httpd或则nginx)提供,例如:html文件、css样式表、还有js文件。但是有些时候查看一些数据这就需要服务器的PHP/Perl/Python来执行并提供给我们,而这些语言在执行中可能会和端的数据库进行交互但有时候并不需要。而我们在购买一个商品或者注册一个帐号后则需要用PHP/Perl/Python 把我们提交的数据写入到MySQL数据库中。在以上的操作中由于使用到了LAMP 这套经典的组合,所以下面就列出这个组合的工作模式。

    静态资源:Client -- http协议 --> httpd
    动态资源:Client -- http协议 --> httpd --> libphp5.so ()
    动态资源:Client -- http协议 --> httpd --> libphp5.so () -- mysql --> MySQL server

    安装方式

    在httpd+php 中的工作方式可以有下面三种:

    • modules

    • cgi

    • fastcgi

    上面三种安装方式中cgi的已经不会使用,而modules和fastcgi可以根据httpd的版本进行选择,modules可以和httpd2.2和httpd2.4结合使用。而fastcgi一般是和httpd2.4结合使用。所以我们在不同版本的CentOS 中的安装也会有不同的方式,下面介绍如何安装:

    CentOS 6

    yum安装

    # yum install -y httpd php php-mysql mysql-server 
    

    说明:在CentOS 6 中系统默认的httpd版本是2.2 而php版本是5.3.3 而MySQL版本是5.1.73,由于版本比较老所以实际使用中需要根据自己的需求进行部署。

    rpm包安装

    上面说的yum 安装是由OS 系统的提供商提供的rpm包,这种安装很方便但是其更新速度不是很快。所以有时候使用起来很不方便。但有些软件的作者会给我们提供自己软件的rpm包,这种提供方式的版本会比OS 所提供的版本会高。但如果软件的作者没有提供那我们只能通过源码编译,或者自己制作rpm包。在CentOS 6系统中rpm 包的后缀是带有el6的。

    源码安装

    下面给的路径是国内公司提供的镜像路径,如果公司有需要则自己在官网进行下载。
    httpd 下载路径:http://mirrors.aliyun.com/apache/httpd/
    php下载路径:http://mirrors.sohu.com/php/
    MySQL 下载路径:http://mirrors.sohu.com/mysql/
    MariaDB 下载路径:https://downloads.mariadb.org/mariadb/
    说明:由于MySQL 已经是Oracle 公司的产品,所以资源获取会很麻烦,建议可以试着尝试使用MariaDB。
    注意:如果在CentOS 6中安装httpd2.4 则需要自己手动安装apr-1.4+和apr-util-1.4+。

    CentOS 7

    yum安装

    Modules 工作方式:

    # yum install -y httpd php php-mysql mariadb-server
    

    FastCGI 工作方式:

    # yum install -y httpd php-fpm php-mysql mariadb-server
    

    注意:以上两种方式安装中php安装不能一起使用。

    rpm安装

    安装方式和CentOS 6相同但是需要注意的是在CentOS 7 中的rpm包的后缀是含有el7的。

    源码安装

    和CentOS 6源码编译安装一样,但是在安装httpd-2.4时如果源码编译则不用在编译apr和apr-util。

    安装LAMP(modules方式)

    环境说明

    L:CentOS 6.6
    A:httpd-2.4
    M:MySQL-5.7.15
    P:php-5.6.28

    httpd 安装配置

    安装

    # yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel
    # tar xf apr-1.5.2.tar.bz2
    # cd apr-1.5.2
    # ./configure --prefix=/usr/local/apr
    # make && make install
    # tar xf apr-util-1.5.4.tar.gz
    # cd apr-util-1.5.4
    # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
    # make && make install 
    # ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-ssl --enable-rewrite --with-pcre --with-z --enable-mods-shared=all --enable-mpms-shared=all --with-mpm=event
    

    关于MPM编译说明:

    • --enable-mpms-shared=all 这个是将prefork、worker和event模型已动态可装载的方式编译进httpd,默认是event模式。

    • --with-mpm=event 这个是指定默认的mpm工作模式,如果不指定在httpd2.4中是event。

    • 以上两个编译参数中如果第一个没有指定,则会默认将event模型已静态的模式编译进httpd。

    • 建议还是使用动态编译这样操作比较容易更换mpm的工作模型。

    配置

    • 添加环境变量

    # cat /etc/profile.d/httpd.sh 
    export PATH=/usr/local/httpd/bin:$PATH
    

    MySQL 安装配置

    创建用户的存储数据目录

    # groupadd -r mysql
    # useradd -g mysql -r -M -s /sbin/nologin mysql
    # mkdir -p /data/mydata
    # chown -R mysql.mysql /data/mydata/
    

    安装

    # tar xf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
    # ln -sv /usr/local/mysql-5.7.15-linux-glibc2.5-x86_64 /usr/local/mysql
    `/usr/local/mysql' -> `/usr/local/mysql-5.7.15-linux-glibc2.5-x86_64'
    # cd /usr/local/mysql
    # chown -R root:mysql ./*
    # bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mydata
    2016-09-13T08:24:28.761851Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2016-09-13T08:24:31.241466Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2016-09-13T08:24:31.542488Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2016-09-13T08:24:31.754866Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7ee23783-798b-11e6-97a5-000c291aa17f.
    2016-09-13T08:24:31.792287Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2016-09-13T08:24:31.795564Z 1 [Note] A temporary password is generated for root@localhost: %wgqEZG:t7zt
    # bin/mysql_ssl_rsa_setup --datadir=/data/mydata/
    

    配置

    # cat /etc/profile.d/mysql.sh
    export PATH=$PATH:/usr/local/mysql/bin
    # . /etc/profile.d/mysql.sh
    # cp support-files/mysql.server /etc/init.d/mysqld
    # chmod +x /etc/init.d/mysqld
    # egrep "^basedir=|^datadir=" /etc/init.d/mysqld
    basedir=/usr/local/mysql
    datadir=/data/mydata
    # cp support-files/my-default.cnf /etc/my.cnf
    # egrep "^basedir|^datadir" /etc/my.cnf 
    basedir = /usr/local/mysql
    datadir = /data/mydata
    

    说明:关于mysql的配置参数这边就不多做介绍,这个只是简单的将配置参数中需要文件路径的给修改。

    修改mysql密码

    初次登录mysql的密码是初始化数据库后自动生成的密码:%wgqEZG:t7zt。这个密码在登录后系统会要求我们进行修改:

    # mysql -uroot -hlocalhost -p
    
    mysql> set password=password('redhat');
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> select user,host,authentication_string  from mysql.user;
    +-----------+-----------+-------------------------------------------+
    | user      | host      | authentication_string                     |
    +-----------+-----------+-------------------------------------------+
    | root      | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
    | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
    +-----------+-----------+-------------------------------------------+
    2 rows in set (0.00 sec)
    

    注意事项

    • 在5.7中存储密码的字段不再是password了,变成了authentication_string。

    • 第一次登录进mysql需要修改密码。

    • 5.7.6之后的版本初始化数据库不再使用mysql_install_db。

    配置开机自动启动

    # chkconfig --add mysqld
    # chkconfig mysqld on
    

    php安装配置

    安装

    # yum install -y libxml2-devel bzip2-devel gd-devel libmcrypt-devel 
    # ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/httpd/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-pcre-dir --with-openssl --with-zlib-dir --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-mcrypt --enable-opcache --enable-sockets --enable-xml --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql=/usr/local/mysql
    # make && make install
    # cp php.ini-production /etc/php.ini
    

    配置

    # cd /usr/local/httpd/conf
    # vim httpd.conf
    209 ServerName localhost:80
    267 <IfModule dir_module>
    268     DirectoryIndex index.php index.html
    269 </IfModule>
    397     AddType application/x-httpd-php .php
    398     AddType application/x-httpd-php-source .phps
    

    验证

    在启动httpd之前请查看php编译后的模块是否加载进httpd中:

    # httpd -M |grep php
     php5_module (shared)
    # httpd -k graceful
    # cat /usr/local/httpd/htdocs/info.php 
    <?php
    phpinfo();
    ?>
    

    启动httpd 之后在浏览器输入:http://IP/info.php,之后就会出先php的phpinfo信息。

    安装typecho

    安装typecho 我会用www.a.com 域名来访问,所以请做好解析,请将域名写入到系统的hosts文件中。

    配置httpd

    # vim /usr/local/httpd/conf/httpd.conf
    486 #Include conf/extra/httpd-vhosts.conf
    # vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
    <VirtualHost *:80>
        DocumentRoot "/opt/a.com"
        ServerName www.a.com
        ServerAlias a.com
        DirectoryIndex index.php index.html index.htm
        ErrorLog "logs/a.com-error_log"
        CustomLog "logs/a.com-access_log" common
        <Directory "/opt/a.com">
        Options None
        AllowOverride None
        Require all granted
        </Directory>
    </VirtualHost>
    

    安装typecho

    # wget https://github.com/typecho/typecho/releases/download/v1.0-14.10.10-release/1.0.14.10.10.-release.tar.gz
    # mkdir /opt/a.com
    # tar xf 1.0.14.10.10.-release.tar.gz -C /opt/a.com/
    # cd /opt/a.com/
    # mv build/* .
    # ls
    admin  index.php  install  install.php  license.txt  usr  var
    # pwd
    /opt/a.com
    # httpd -k graceful
    

    下面就需要在浏览器输www.a.com,进行安装。安装前请在数据库中创建typecho库。

    访问测试

    未使用Opcache的压力测试结果:

    # ab -c 10 -n 1000 http://www.a.com/admin/login.php
    .....
    Requests per second:    72.72 [#/sec] (mean)
    Time per request:       137.510 [ms] (mean)
    Time per request:       13.751 [ms] (mean, across all concurrent requests)
    Transfer rate:          486.40 [Kbytes/sec] received
    .....
    # ab -c 100 -n 1000 http://www.a.com/admin/login.php
    .....
    Requests per second:    69.63 [#/sec] (mean)
    Time per request:       1436.185 [ms] (mean)
    Time per request:       14.362 [ms] (mean, across all concurrent requests)
    Transfer rate:          465.71 [Kbytes/sec] received
    ....
    

    这种测试只是为了和后面使用Opcache测试相对比。
    Opcache 配置:

    zend_extension=opcache.so
    opcache.memory_consumption=128
    opcache.interned_strings_buffer=8
    opcache.max_accelerated_files=4000
    opcache.revalidate_freq=60
    opcache.fast_shutdown=1
    opcache.enable_cli=1
    opcache.enable=1
    

    使用Opcache后的压力测试结果:

    # ab -c 10 -n 1000 http://www.a.com/admin/login.php
    ......
    Requests per second:    325.27 [#/sec] (mean)
    Time per request:       30.743 [ms] (mean)
    Time per request:       3.074 [ms] (mean, across all concurrent requests)
    Transfer rate:          2175.58 [Kbytes/sec] received
    # ab -c 100 -n 1000 http://www.a.com/admin/login.php
    ......
    Requests per second:    326.60 [#/sec] (mean)
    Time per request:       306.186 [ms] (mean)
    Time per request:       3.062 [ms] (mean, across all concurrent requests)
    Transfer rate:          2184.45 [Kbytes/sec] received
    

    通过上面的测试结果可以看到有着明显的效果提升。

    APCu 安装

    • 源码编译安装

    APCu下载路径

    # tar xf apcu-4.0.11.tgz
    # cd apcu-4.0.11
    # /usr/local/php/bin/phpize
    Configuring for:
    PHP Api Version:         20121113
    Zend Module Api No:      20121212
    Zend Extension Api No:   220121212
    #  ./configure --enable-apcu --with-php-config=/usr/local/php/bin/php-config 
    # make && make install
    Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
    Installing header files:          /usr/local/php/include/php/
    # mkdir /etc/php.d
    # vim /etc/php.d/apcu.ini
    extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
    apc.enabled=1 
    apc.shm_segments = 1 
    apc.shm_size=128M 
    apc.ttl=0 
    apc.enable_cli=0 
    apc.entries_hint=0 
    apc.gc_ttl = 3600 
    apc.rfc1867 = off 
    apc.slam_defense = off 
    apc.use_request_time = 0
    

    注意:在使用APCu之前请把Zend Opcache关闭。

    访问测试:

    # ab -c 10 -n 1000 http://www.a.com/admin/login.php
    ......
    Requests per second:    81.47 [#/sec] (mean)
    Time per request:       122.749 [ms] (mean)
    Time per request:       12.275 [ms] (mean, across all concurrent requests)
    Transfer rate:          544.89 [Kbytes/sec] received
    ......
    # ab -c 100 -n 1000 http://www.a.com/admin/login.php
    .......
    Requests per second:    72.62 [#/sec] (mean)
    Time per request:       1376.995 [ms] (mean)
    Time per request:       13.770 [ms] (mean, across all concurrent requests)
    Transfer rate:          485.73 [Kbytes/sec] received
    
    • xcache 安装

    安装xcache之前,请把前面编译安装的apcu给删除。

    # httpd -k stop
    # rm -rf /usr/local/php/include/php/ext/apcu
    # rm -f /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/apcu.so
    # rm -f /etc/php.d/php.ini
    # wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
    # tar xf xcache-3.2.0.tar.gz
    # cd xcache-3.2.0
    # /usr/local/php/bin/phpize
    # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config 
    # make && make install
    # vim /etc/php.d/xcache.ini
    [xcache]
    extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/xcache.so
    xcache.shm_scheme = "mmap"
    xcache.size = 128M
    xcache.count = 1
    xcache.slots = 8K
    xcache.ttl = 0
    xcache.gc_interval = 0
    xcache.var_size = 4M
    xcache.var_count = 1
    xcache.var_slots = 8K
    xcache.var_ttl = 0
    xcache.var_maxttl = 0
    xcache.var_gc_interval = 300
    

    访问测试:

    # ab -c 10 -n 1000 http://www.a.com/admin/login.php
    ......
    Requests per second:    221.55 [#/sec] (mean)
    Time per request:       45.136 [ms] (mean)
    Time per request:       4.514 [ms] (mean, across all concurrent requests)
    Transfer rate:          1481.84 [Kbytes/sec] received
    ......
    # ab -c 100 -n 1000 http://www.a.com/admin/login.php
    ......
    Requests per second:    192.22 [#/sec] (mean)
    Time per request:       520.242 [ms] (mean)
    Time per request:       5.202 [ms] (mean, across all concurrent requests)
    Transfer rate:          1285.65 [Kbytes/sec] received
    

    关于上面三个php加速器的比较看还是Opcache 速度好些,其实不管使用那款软件,我还是建议用Opcache,这是因为Opcache进编译进php内核了,这样今后就会有人进行维护。而xcache 已经没有更新了。所以在选用软件是还要关注软件的社区活跃程度,别到时候出先问题就很麻烦了。

    安装LAMP(fastcgi方式)

    php-fpm 安装

    安装前请停止httpd 服务,同时为避免出现不必要的错误请将php.ini 等文件移走或删除。下面是php的编译参数,注意由于使用的fastcgi 则php则需要启用php-fpm,这种方式的编译和modules 方式在是有一些区别的。

    # ./configure --prefix=/usr/local/php-fpm  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-pcre-dir --with-openssl --with-zlib-dir --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-mcrypt --enable-opcache --enable-sockets --enable-xml --enable-fpm --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql=/usr/local/mysql
    # cp php.ini-production /etc/php.ini
    # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    # chmod +x /etc/init.d/php-fpm
    # chkconfig --add php-fpm
    # chkconfig php-fpm on
    # cd /usr/local/php-fpm/etc/
    # cp php-fpm.conf.default php-fpm.conf
    # /etc/init.d/php-fpm start
    Starting php-fpm  done
    

    httpd 配置

    # vim /usr/local/httpd/conf/httpd.conf
    #在配置文件内将基于proxy的模块取消注释和前面编译的php modules注释
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
    #LoadModule php5_module modules/libphp5.so
    
    #在文件内添加下面的内容,位置最好在AddType下面的行:
        AddType application/x-httpd-php .php
        AddType application/x-httpd-php-source .phps
    
    # vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
    <VirtualHost *:80>
        DocumentRoot "/opt/b.net"
        ServerName www.b.net
        ServerAlias b.net
        DirectoryIndex index.php index.html index.htm 
        ErrorLog "logs/b.net-error_log"
        CustomLog "logs/b.net-access_log" common
        <Directory "/opt/b.net">
        Options None
        AllowOverride None
        Require all granted
        </Directory>
        ProxyRequests off
        ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/opt/b.net/$1
    </VirtualHost>
    # httpd -k graceful
    

    测试

    安装phpmyadmin:

    # cd /opt/b.net/
    # wget https://files.phpmyadmin.net/phpMyAdmin/4.6.5.2/phpMyAdmin-4.6.5.2-all-languages.tar.xz
    # tar xf phpMyAdmin-4.6.5.2-all-languages.tar.xz
    # mv phpMyAdmin-4.6.5.2-all-languages/* .
    

    未使用Opcache:

    # ab -c 10 -n 1000 http://www.b.net/
    ......
    Requests per second:    24.37 [#/sec] (mean)
    Time per request:       410.303 [ms] (mean)
    Time per request:       41.030 [ms] (mean, across all concurrent requests)
    Transfer rate:          284.47 [Kbytes/sec] received
    # ab -c 100 -n 1000 http://www.b.net/
    ......
    Requests per second:    25.32 [#/sec] (mean)
    Time per request:       3949.086 [ms] (mean)
    Time per request:       39.491 [ms] (mean, across all concurrent requests)
    Transfer rate:          295.56 [Kbytes/sec] received
    

    使用Opcache:

    # vim /etc/php.ini
    zend_extension=opcache.so
    opcache.memory_consumption=128
    opcache.interned_strings_buffer=8
    opcache.max_accelerated_files=4000
    opcache.revalidate_freq=60
    opcache.fast_shutdown=1
    opcache.enable_cli=1
    opcache.enable=1
    # /etc/init.d/php-fpm restart
    # ab -c 10 -n 1000 http://www.b.net/
    ......
    Requests per second:    147.96 [#/sec] (mean)
    Time per request:       67.584 [ms] (mean)
    Time per request:       6.758 [ms] (mean, across all concurrent requests)
    Transfer rate:          1727.01 [Kbytes/sec] received
    # ab -c 100 -n 1000 http://www.b.net/
    Requests per second:    134.80 [#/sec] (mean)
    Time per request:       741.866 [ms] (mean)
    Time per request:       7.419 [ms] (mean, across all concurrent requests)
    Transfer rate:          1573.31 [Kbytes/sec] received
    

    编译php遇到的问题:

    configure:  error: Cannot find libmysqlclient_r under /usr/local/mysql
    Note that the MySQL client library is not bundled anymore!
    
    # cd /usr/local/mysql/lib/
    # ln -s libmysqlclient.so.20.3.2  libmysqlclient_r.so
    

    关于php加速器的一些博客:
    http://blog.oneapm.com/apm-tech/125.html
    http://blog.csdn.net/liuxinmingcode/article/details/8058864
    https://www.vpser.net/opt/apc-eaccelerator-xcache.html
    https://cnzhx.net/blog/zendopcache-accelerate-php/
    https://my.oschina.net/angelangel/blog/338458

    关于一些Opcache的gui界面的项目:
    https://github.com/PeeHaa/OpCacheGUI
    https://github.com/rlerdorf/opcache-status

  • 相关阅读:
    IDEA热部署插件JRebel使用
    IntelliJ IDEA 代码注释
    解决redis显示中文为乱码问题
    C#的六种修饰符
    Bat批处理把文件夹包括子文件夹下面的某个文件复制到另一个目录下
    html不识别<br/>,后台返回<br/>,前端不换行解决办法
    C# Task的应用
    c# 生成json字符串和解析json字符串处理
    在 C# 中将 List<dynamic> 转换为 List<string>
    C#读取主从文件的excel并把结果为pass的文件打包
  • 原文地址:https://www.cnblogs.com/cuchadanfan/p/6179678.html
Copyright © 2011-2022 走看看