lnamp完整版[linux+apache2.4+php5.6.6+mysql5.6]
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.gz tar -zxvf apr-1..5.tar.gz cd apr-1.5.1 ./configure --prefix=/usr/local/apr make make install B.apr-util安装 wget -c wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.4.tar.gz tar -zxvf 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 C.pcre安装 wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz tar -zxvf pcre-8.36.tar.gz cd pcre-8.36 ./configure --prefix=/usr/local/pcre make make install D.apache安装 wget -c http://mirror.symnds.com/software/Apache//httpd/httpd-2.4.10.tar.gz tar -zxvf httpd-2.4.10.tar.gz cd httpd-2.4.10 ./configure --prefix=/usr/local/apache --enable-so-rewrite=shared --with-mpm=prefork --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre 如果提示openssl(旧版),运行 yum install openssl-devel yum update openssl make make install E.把apache加入系统service,开机自启动 cp /usr/local/apache/bin/apachectl /etc/init.d/httpd vim /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 httpd ln -s /sbin/chkconfig /usr/bin/chkconfig ln -s /sbin/service /usr/bin/service 2.MySQL安装 A.准备工作 安装一些必须的软件: yum install cmake automake autoconf libtool gcc g++ bison yum install ncurses-devel mkdir -p /data/mysql mkdir -p /var/run/mysqld groupadd 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.gz cd mysql-5.6.11 cmake -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文件 make make 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.cnf cp support-files/my-medium.cnf /etc/my.cnf vim /etc/my.cnf [client] default-character-set=utf8 port=3306 [mysqld_safe] log-error=/var/log/www/mysql/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [mysqld] datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock socket=/tmp/mysqld.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 wait-timeout = 30 max_connections = 512 max_allowed_packet=64M slow_query_log slow_query_log_file=/var/log/www/mysql/slow.log log-error = /var/log/www/mysql/error.log #数据目录的权限 chown mysql.mysql -R /data/mysql chown -R mysql:mysql /var/run/mysqld chmod +x /usr/local/mysql chown -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/bin ln -s /usr/local/mysql/bin/mysqldump /usr/bin ln -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.gz tar -zxvf zlib-1.2.8.tar.gz wget -c http://www.openssl.org/source/openssl-1.0.1e.tar.gz tar -zxvf openssl-1.0.1e.tar.gz wget 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.gz tar -zxvf tengine-2.1.0.tar.gz cd 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.3 make 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 contributors set -e PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME SCRIPTNAME=/etc/init.d/$NAME # If the daemon file is not found, terminate the script. test -x $DAEMON || exit 0 start() { $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 ;; esac exit 0 chmod +x /etc/init.d/nginx service nginx start service nginx reload service nginx restart service nginx stop vim /etc/rc.local 添加一行 /usr/local/nginx/sbin/nginx & 4.PHP编译安装 A.yum安装相关库包 yum install gd-devel php-gd yum install zlib zlib-devel yum install freetype-devel freetype-demos freetype-utils yum install libpng libpng-devel libpng10 libpng10-devel yum install libjpeg libjpeg-devel yum install ImageMagick yum install flex yum install ImageMagick-devel yum install libxml2 libxml2-devel yum install libxml2 curl curl-devel yum -y install libtool libtool-ltdl-devel yum install mhash-devel yum install patch B.可能需要源码编译的包 wget -c ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz tar -zxvf libmcrypt-2.5.7.tar.gz cd libmcrypt-2.5.7 ./configure --prefix=/usr/local/libmcrypt make && make install wget -c http://iweb.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz tar -zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure --prefix=/usr/local/libmhash make && make install C.编译安装 wget -c http://cn2.php.net/get/php-5.6.6.tar.gz/from/this/mirror tar -zxvf php-5.6.6.tar.gz cd 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.h yum install openldap yum install openldap-devel Configure: error: Please reinstall the BZip2 distribution centos: 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再make make make install 修改apache的配置文件httpd.conf vim /usr/local/apache/conf/httpd.conf
然后在文本最后面添加 LoadModule php5_module modules/libphp5.so
(注意,在apache安装目录下,modules下有libphp5.so,这是php安装时添加进去的,如果没有,php,你需要重装下) AddType application/x-httpd-php .php AddType application/x-httpd-php .html cp php.ini-production /usr/local/php/etc/php.ini 5.环境整合 A.整合apache mkdir -p /home/www/test.com chown -R www.www /home/www apache将被nginx代理,因此将apache的默认监听端口改为8080 vim /usr/local/apache/conf/httpd.conf 将hosts定向到 Include conf/extra/httpd-vhosts.conf Listen 8080 User www Group www <IfModule dir_module> DirectoryIndex index.php index.html index.htm </IfModule> vim /usr/local/apache/conf/extra/httpd-vhosts.conf 将里面的*:80全部改成yourdomain.name:8080 并添加虚拟hosts <VirtualHost *:8080> ServerAdmin webmaster@a.test.com DocumentRoot "/home/www/test.com" DirectoryIndex index.php index.html ServerName a.test.com <Directory "/home/www/test.com"> AllowOverride None Options None Require all granted </Directory> ErrorLog "/var/log/www/apache/a.test.com-error_log" CustomLog "/var/log/www/apache/a.test.com-access_log" common </VirtualHost> vim /home/www/test.com/phpinfo.php <?php echo phpinfo(); ?> service httpd start
访问http://a.test.com:8080/phpinfo.php B.配置php-fpm cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #开启php-fpm慢脚本日志 request_slowlog_timeout = 30 slowlog = /var/log/www/php/php-fpm.log.slow #脚本等待时间 request_terminate_timeout = 120 pid = /usr/local/php/var/run/php-fpm.pid error_log = /var/log/www/php/php-fpm.log.error log_level = notice user = www group = www listen = 127.0.0.1:9000 pm = dynamic pm.max_children = 100 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 100 pm.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 INFO prefix=/usr/local/php exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_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 start service php-fpm stop service php-fpm reload service php-fpm restart #开机启动 vim /etc/rc.local 添加一行 /usr/local/php/sbin/php-fpm & C.Nginx整合 mkdir /usr/local/nginx/conf/vhosts vim /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来解析动态请求了,反向代理配置正确 完成