Lnamp环境安装实录将采用的开源软件:Apache [WEB动态脚本服务器,做nginx的反向代理 8080端口]Tengine [WEB静态文件服务器 80端口]MySQL PHP 1.Apache安装A.apr安装wget -c http://mirror.bjtu.edu.cn/apache/apr/apr-1.5.1.tar.gztar -zxvf apr-1..5.tar.gzcd apr-1.5.1./configure --prefix=/usr/local/aprmakemake installB.apr-util安装wget -c wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.4.tar.gztar -zxvf apr-util-1.5.4.tar.gzcd apr-util-1.5.4./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/aprmakemake installC.pcre安装wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gztar -zxvf pcre-8.36.tar.gzcd pcre-8.36./configure --prefix=/usr/local/pcremakemake installD.apache安装wget -c http://mirror.symnds.com/software/Apache//httpd/httpd-2.4.10.tar.gztar -zxvf httpd-2.4.10.tar.gzcd httpd-2.4.10./configure --prefix=/usr/local/apache --enable-so-rewrite= --with-mpm=prefork --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre如果提示openssl(旧版),运行yum install openssl-develyum update opensslmakemake install E.把apache加入系统service,开机自启动 cp /usr/local/apache/bin/apachectl /etc/init.d/httpdvim /etc/init.d/httpd# beyound# chkconfig: 35 85 15# description: Apache is a World Wide Web server. chmod +x /etc/init.d/httpd/sbin/chkconfig --add httpd/sbin/chkconfig --list httpdln -s /sbin/chkconfig /usr/bin/chkconfigln -s /sbin/service /usr/bin/service2.MySQL安装A.准备工作 安装一些必须的软件:yum install cmake automake autoconf libtool gcc g++ bisonyum install ncurses-develmkdir -p /data/mysqlmkdir -p /var/run/mysqldgroupadd mysql //添加一个mysql标准组useradd -g mysql mysql //添加mysql用户并加到mysql组中B.下载安装wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.11.tar.gz/from/http://cdn.mysql.com/tar -zxvf mysql-5.6.11.tar.gzcd mysql-5.6.11cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql #安装路径-DMYSQL_DATADIR=/data/mysql/ #数据文件存放位置-DSYSCONFDIR=/etc #my.cnf路径-DWITH_MYISAM_STORAGE_ENGINE=1 #支持MyIASM引擎-DWITH_INNOBASE_STORAGE_ENGINE=1 #支持InnoDB引擎-DWITH_MEMORY_STORAGE_ENGINE=1 #支持InnoDB引擎-DWITH_READLINE=1 #快捷键功能(我没用过)-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock #连接数据库socket路径-DMYSQL_TCP_PORT=3306 #端口-DENABLED_LOCAL_INFILE=1 #允许从本地导入数据-DWITH_PARTITION_STORAGE_ENGINE=1 #安装支持数据库分区-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk #安装需要的字符集-DDEFAULT_CHARSET=utf8 #默认字符-DDEFAULT_COLLATION=utf8_general_ci #默认字符集-DMYSQL_USER=mysql如果安装过程中报错,解决错误后重新cmake前需删除CMakeCache.txt文件makemake install C.配置配置文件这一步中需要注意的是my.cnf的加载顺序,Linux优先级从高到低/etc/my.cnf->/etc/mysql/my.cnf->SYSCONFDIR/my.cnf->$MYSQL_HOME/my.cnf,高优先级的my.cnf设置会覆盖低优先级的my.cnf,所以一般把config文件copy到etc中即可。#如果/etc下没有my.cnfcp support-files/my-medium.cnf /etc/my.cnfvim /etc/my.cnf [client]default-character-set=utf8port=3306[mysqld_safe]log-error=/var/log/www/mysql/mysqld.logpid-file=/var/run/mysqld/mysqld.pid [mysqld]datadir=/var/lib/mysql#socket=/var/lib/mysql/mysql.socksocket=/tmp/mysqld.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0wait-timeout = 30max_connections = 512max_allowed_packet=64Mslow_query_logslow_query_log_file=/var/log/www/mysql/slow.loglog-error = /var/log/www/mysql/error.log#数据目录的权限chown mysql.mysql -R /data/mysqlchown -R mysql:mysql /var/run/mysqldchmod +x /usr/local/mysqlchown -R mysql.mysql /usr/local/mysql #数据初始化/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql#设置软连接使mysql, mysqldump, mysqladmin这三个bin命令能在shell中直接运行ln -s /usr/local/mysql/bin/mysql /usr/binln -s /usr/local/mysql/bin/mysqldump /usr/binln -s /usr/local/mysql/bin/mysqladmin /usr/bin#启动MySQL/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql & #配置开机自启动vim /etc/rc.local 添加/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &#修改mysql密码UPDATE user SET password=PASSWORD("newpassword") WHERE user='root';FLUSH PRIVILEGES;create database db_test;B.创建一个新用户用于管理 db_test 数据库insert into mysql.user(Host,User,Password) values("localhost","admin",password("newpassword"));flush privileges;C.赋予权限grant all privileges on db_test.* to db_test@localhost identified by 'newpassword';3.Tengine[Nginx]安装A.准备工作需要下载的东西wget -c http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzwget -c http://www.openssl.org/source/openssl-1.0.1e.tar.gztar -zxvf openssl-1.0.1e.tar.gzwget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar zxvf ngx_cache_purge-2.3.tar.gz groupadd www //添加一个www标准组useradd -g www www //添加www用户并加到www组中 B.下载安装wget -c http://tengine.taobao.org/download/tengine-2.1.0.tar.gztar -zxvf tengine-2.1.0.tar.gzcd tengine-2.1.0./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_sub_module --with-http_realip_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_addition_module --with-http_ssl_module --with-openssl=../openssl-1.0.1h --with-pcre=../pcre-8.36 --with-zlib=../zlib-1.2.8 --add-module=/usr/local/src/ngx_cache_purge-2.3make make install C.配置vim /etc/init.d/nginx#! /bin/sh# Author: rui ding # Modified: Geoffrey Grosenbach http://topfunky.com # Modified: Clement NEDELCU# Reproduced with express authorization from its contributorsset -ePATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="nginx daemon"NAME=nginxDAEMON=/usr/local/nginx/sbin/$NAMESCRIPTNAME=/etc/init.d/$NAME # If the daemon file is not found, terminate the script.test -x $DAEMON || exit 0start() { $DAEMON || echo -n " already running"}stop() { $DAEMON -s quit || echo -n " not running"}reload() { $DAEMON -s reload || echo -n " could not reload"}case "$1" in start) echo -n "Starting $DESC: $NAME" start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." reload echo "reloaded." ;; restart) echo -n "Restarting $DESC: $NAME" stop# Sleep for two seconds before starting again, this should give the# Nginx daemon some time to perform a graceful stop. sleep 2 start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2 exit 3 ;;esacexit 0chmod +x /etc/init.d/nginxservice nginx startservice nginx reloadservice nginx restartservice nginx stop vim /etc/rc.local 添加一行/usr/local/nginx/sbin/nginx & 4.PHP编译安装A.yum安装相关库包 yum install gd-devel php-gdyum install zlib zlib-develyum install freetype-devel freetype-demos freetype-utilsyum install libpng libpng-devel libpng10 libpng10-develyum install libjpeg libjpeg-develyum install ImageMagickyum install flexyum install ImageMagick-develyum install libxml2 libxml2-develyum install libxml2 curl curl-develyum -y install libtool libtool-ltdl-develyum install mhash-develyum install patch B.可能需要源码编译的包 wget -c ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gztar -zxvf libmcrypt-2.5.7.tar.gzcd libmcrypt-2.5.7./configure --prefix=/usr/local/libmcryptmake && make install wget -c http://iweb.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gztar -zxvf mhash-0.9.9.9.tar.gzcd mhash-0.9.9.9./configure --prefix=/usr/local/libmhashmake && make install C.编译安装 wget -c http://cn2.php.net/get/php-5.6.6.tar.gz/from/this/mirrortar -zxvf php-5.6.6.tar.gzcd php-5.6.6 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config --with-apxs2=/usr/local/apache/bin/apxs --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-ftp --enable-sigchild --with-pear --enable-cli --enable-exif --enable-calendar --with-bz2 --enable-pdo --with-pdo-mysql --enable-freetype --disable-fileinfo configure: error: Cannot find ldap.hyum install openldapyum install openldap-develConfigure: error: Please reinstall the BZip2 distributioncentos: yum install bzip2 bzip2-devel 错误:configure: error: Cannot find ldap libraries in /usr/lib解决办法:cp -frp /usr/lib64/libldap* /usr/lib/ 错误:configure: error: Unable to find your mysql installation解决办法:ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config编译出错后重新编译时需先make clean再makemakemake install修改apache的配置文件httpd.confvim /usr/local/apache/conf/httpd.conf 然后在文本最后面添加LoadModule php5_module modules/libphp5.so (注意,在apache安装目录下,modules下有libphp5.so,这是php安装时添加进去的,如果没有,php,你需要重装下)AddType application/x-httpd-php .phpAddType application/x-httpd-php .htmlcp php.ini-production /usr/local/php/etc/php.ini 5.环境整合A.整合apachemkdir -p /home/www/test.comchown -R www.www /home/wwwapache将被nginx代理,因此将apache的默认监听端口改为8080vim /usr/local/apache/conf/httpd.conf 将hosts定向到 Include conf/extra/httpd-vhosts.confListen 8080User wwwGroup wwwDirectoryIndex index.php index.html index.htm vim /usr/local/apache/conf/extra/httpd-vhosts.conf将里面的*:80全部改成yourdomain.name:8080并添加虚拟hostsServerAdmin webmaster@a.test.com DocumentRoot "/home/www/test.com" DirectoryIndex index.php index.html ServerName a.test.com vim /home/www/test.com/phpinfo.php service httpd start 访问http://a.test.com:8080/phpinfo.phpB.配置php-fpmcp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf#开启php-fpm慢脚本日志request_slowlog_timeout = 30slowlog = /var/log/www/php/php-fpm.log.slow#脚本等待时间request_terminate_timeout = 120pid = /usr/local/php/var/run/php-fpm.piderror_log = /var/log/www/php/php-fpm.log.errorlog_level = noticeuser = wwwgroup = wwwlisten = 127.0.0.1:9000pm = dynamicpm.max_children = 100pm.start_servers = 10pm.min_spare_servers = 5pm.max_spare_servers = 100pm.max_requests = 1000/usr/local/php/sbin/php-fpm#将php-fpm整合成系统服务vim /etc/init.d/php-fpm#! /bin/sh### BEGIN INIT INFO# Provides: php-fpm# Required-Start: $remote_fs $network# Required-Stop: $remote_fs $network# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: starts php-fpm# Description: starts the PHP FastCGI Process Manager daemon### END INIT INFOprefix=/usr/local/phpexec_prefix=${prefix}php_fpm_BIN=${exec_prefix}/sbin/php-fpmphp_fpm_CONF=${prefix}/etc/php-fpm.confphp_fpm_PID=${prefix}/var/run/php-fpm.pid php_opts="--fpm-config $php_fpm_CONF"wait_for_pid () { try=0 while test $try -lt 35 ; do case "$1" in 'created') if [ -f "$2" ] ; then try='' break fi ;; 'removed') if [ ! -f "$2" ] ; then try='' break fi ;; esac echo -n . try=`expr $try + 1` sleep 1 done} case "$1" in start) echo -n "Starting php-fpm " $php_fpm_BIN $php_opts if [ "$?" != 0 ] ; then echo " failed" exit 1 fi wait_for_pid created $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Gracefully shutting down php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -QUIT `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; force-quit) echo -n "Terminating php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -TERM `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $0 stop $0 start ;; reload) echo -n "Reload service php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -USR2 `cat $php_fpm_PID` echo " done" ;; *) echo "Usage: $0 {start|stop|force-quit|restart|reload}" exit 1 ;; esac chmod +x /etc/init.d/php-fpm service php-fpm startservice php-fpm stopservice php-fpm reloadservice php-fpm restart #开机启动vim /etc/rc.local添加一行/usr/local/php/sbin/php-fpm & C.Nginx整合mkdir /usr/local/nginx/conf/vhostsvim /usr/local/nginx/conf/nginx.conf user www www;worker_processes 8;error_log /var/log/www/nginx/error.log crit;#error_log /var/log/www/nginx/error.log notice;#error_log /var/log/www/nginx/error.log info; #pid logs/nginx.pid; worker_rlimit_nofile 65535;events { use epoll; worker_connections 1024;}http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; include vhosts/*.conf;} vim /usr/local/nginx/conf/vhosts/a.test.com.conf #反向代理upstream www.test { a.test.com:8080;}server { listen 80; server_name www.test.com; charset utf-8; root /home/www/www.test; location / { index index.php index.html index.htm;if (!-e $request_filename) { #rewrite ^(.*)$ /index.php?s=$1 last; #break; } } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { #開啟支持php fastcgi_pass 127.0.0.1:9000; #php fastcgi服務地址及端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }} service nginx reload D.Apache+nginx+php反向代理 vim /usr/local/nginx/conf/vhosts/a.test.com.conf #反向代理upstream www.test{ server a.test.com:8080;}server { listen 80; server_name www.test.com; charset utf-8; root /home/www/test.com; location / { index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { #fastcgi_pass 127.0.0.1:9000; #php fastcgi服務地址及端口 #fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #include fastcgi_params; proxy_pass http://www.test; #apache反向代理 proxy_pass_header User-Agent; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 512k; proxy_buffers 4 512k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 512k; }}service nginx reload再访问 http://www.test.com/phpinfo.php 系统已经用apache来解析动态请求了,反向代理配置正确完成AllowOverride None Options None Require all granted ErrorLog "/var/log/www/apache/a.test.com-error_log" CustomLog "/var/log/www/apache/a.test.com-access_log" common