Lnmp安装与配置笔记

——————————————安装与配置——————————————
1.相关软件:
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
 
nginx   http://nginx.org/download/nginx-1.2.0.tar.gz
pcre     ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
php      http://cn.php.net/get/php-5.3.13.tar.bz2/from/this/mirror
libiconv  http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
mhash   http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
mcrypt   http://ncu.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
libmcrypt  http://ncu.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
mysql     ftp://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-5.5/mysql-5.5.24.tar.gz
cmake   http://www.cmake.org/files/v2.8/cmake-2.8.5.tar.gz
 
2.MySQL
(1).安装cmake
[root@study ~]# tar zxf cmake-2.8.5.tar.gz
[root@study ~]# cd cmake-2.8.5
[root@study cmake-2.8.5]# ./configure
[root@study cmake-2.8.5]# gmake && gmake install && cd ../
 
(2).安装配置mysql
[root@study ~]# useradd mysql
[root@study ~]# mkdir -p /data/mysql   
[root@study ~]# chown mysql:mysql /data/mysql
[root@study ~]# tar zxf mysql-5.5.24.tar.gz
[root@study ~]#cd mysql-5.5.24
[root@study mysql-5.5.24]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc/
[root@study mysql-5.5.24]# make && make install
[root@study mysql-5.5.24]# cp support-files/my-medium.cnf /etc/my.cnf
[root@study mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld
[root@study mysql-5.5.24]# chmod 755 /etc/init.d/mysqld 
 
[root@study ~]# vim /etc/my.cnf
#在[mysqld]增加
datadir          = /data/mysql
wait-timeout = 30
max_connections = 512
default-storage-engine = MyISAM   
#修改
max_allowed_packet = 16M
 
[root@study ~]# cd /usr/local/mysql/
[root@study mysql]# ./scripts/mysql_install_db --user=mysql                 #生成授权表
[root@study ~]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
[root@study ~]# chkconfig mysqld on
[root@study ~]# /etc/init.d/mysqld start
 
修改密码:
[root@study ~]# /usr/local/mysql/bin/mysqladmin -u root password 123456
 
3.PHP
(1).安装相关的库
[root@study ~]# tar zxf libiconv-1.13.1.tar.gz
[root@study ~]# cd libiconv-1.13.1
[root@study libiconv-1.13.1]# ./configure --prefix=/usr/local/
[root@study libiconv-1.13.1]# make && make install && cd
 
[root@study ~]# tar zxf libmcrypt-2.5.8.tar.gz
[root@study ~]# cd libmcrypt-2.5.8
[root@study libmcrypt-2.5.8]# ./configure && make && make install
[root@study libltdl]# /sbin/ldconfig && cd libltdl/ && ./configure --enable-ltdl-install
[root@study libltdl]# make && make install && cd
 
[root@study ~]# tar zxf mhash-0.9.9.9.tar.gz
[root@study ~]# cd mhash-0.9.9.9
[root@study mhash-0.9.9.9]# ./configure && make && make install && cd
 
[root@study ~]# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
[root@study ~]# ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
[root@study ~]# ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
[root@study ~]# ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
[root@study ~]# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
[root@study ~]# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
[root@study ~]# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
[root@study ~]# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
[root@study ~]# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
[root@study ~]# ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
 
[root@study ~]# tar zxf mcrypt-2.6.8.tar.gz
[root@study ~]# cd mcrypt-2.6.8
[root@study mcrypt-2.6.8]# /sbin/ldconfig
[root@study mcrypt-2.6.8]# ./configure && make && make install && cd                     
 
(2).php安装
[root@study ~]# tar jxf php-5.3.13.tar.bz2
[root@study ~]# cd php-5.3.13
[root@study ~]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql -enable-fastcgi -enable-fpm \
--with-mysqli=/usr/local/mysql/bin/mysql_config --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-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl \
--with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --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
[root@study php-5.3.13]# make ZEND_EXTRA_LIBS='-liconv'
[root@study php-5.3.13]# make install
[root@study php-5.3.13]# cp php.ini-production /usr/local/php/etc/php.ini
 
(3).配置PHP-FPM(PHP5.3.3已经集成php-fpm了,不再是第三方的包了)
[root@study ~]# useradd www
[root@study ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@study ~]# /usr/local/php/sbin/php-fpm -t    #检查语法
[09-May-2012 22:03:32] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
 
[root@study ~]# echo "/usr/local/php/sbin/php-fpm" >> /etc/rc.local          #开机自启动
[root@study ~]# /usr/local/php/sbin/php-fpm                  #启动服务
 
4.Nginx
(1).安装pcre
[root@study ~]# tar zxf pcre-8.21.tar.gz
[root@study ~]# cd pcre-8.21
[root@study pcre-8.21]# ./configure && make && make install
 
(2).nginx安装
[root@study ~]# tar zxf nginx-1.2.0.tar.gz
[root@study ~]# cd nginx-1.2.0
[root@study nginx-1.2.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx  \
--with-http_stub_status_module --with-http_ssl_module \
--with-pcre --lock-path=/var/run/nginx.lock --pid-path=/var/run/nginx.pid
[root@study nginx-1.2.0]# make && make install
[root@study ~]# /usr/local/nginx/sbin/nginx         #启动服务
[root@study ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local  #开机自启动
 
(3).配置nginx
编辑/usr/local/nginx/conf/nginx.conf
#user nobody;
events {
    worker_connections 1024;
}
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index index.php;
        #    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
修改成:
 
   
   
   
   
  1. user www;  
  2. events {  
  3.     use epoll;  
  4.     worker_connections 1024;  
  5. }  
  6.         location ~ \.php$ {  
  7.             root           html;  
  8.             fastcgi_pass   127.0.0.1:9000;  
  9.             fastcgi_index index.php;  
  10.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
  11.             include        fastcgi_params;  
  12.         } 

 (4).重启服务测试

[root@study ~]# /usr/local/nginx/sbin/nginx -t        #语法检查
[root@study ~]# /usr/local/nginx/sbin/nginx -s reload    #重新加载配置
[root@study ~]# cd /usr/local/nginx/html/
[root@study html]# cat index.php
<?php
phpinfo();
?>
 
问题:
1.make: [ext/phar/phar.phar] error 1
解决:在编译的时候加--without-pear参数,如果还不行,make的时候不添加 ZEND_EXTRA_LIBS='-liconv' 参数。
 
————————————————使用————————————————
一、Nginx的启动、关闭、重启及版本
1.启动
/usr/local/nginx/sbin/nginx
 
2.关闭
kill -9 `cat /var/run/nginx.pid`
 
3.平滑重启
kill -HUP `cat /var/run/nginx.pid`
 
4.查看版本及编译信息
/usr/local/nginx/sbin/nginx -V
 
二、Nginx配置
1.虚拟主机配置
   
   
   
   
  1. server {  
  2.         listen          80;                #端口  
  3.         server_name     web1.example.com;         #域名或IP  
  4.         access_log      logs/web1.example.com main; #日记  
  5.    
  6.         location /{  
  7.           index index.php;   
  8.           root /data/web1;         #目录(可用绝对路径或相对路径)  
  9.         }  
  10.    
  11.         location ~ \.php$ {   #php  
  12.           root html;  
  13.           fastcgi_pass 127.0.0.1:9000;  
  14.           fastcgi_index index.php;  
  15.           fastcgi_param SCRIPT_FILENAME /data/web1$fastcgi_script_name;  
  16.           include       fastcgi_params;  
  17.         }  
  18.      } 
2.负载均衡
www.example.com域名指向Nginx主机的ip,当用户访问www.example.com时,Nginx将请求负载均衡到后端的两台web主机。
 

配置文件:
[root@master conf]# egrep -v "(#|^$)" nginx.conf
   
   
   
   
  1. user www;  
  2. worker_processes 2;  
  3. events {  
  4.     use epoll;  
  5.     worker_connections 1024;  
  6. }  
  7. http {  
  8.     include       mime.types;  
  9.     default_type application/octet-stream;  
  10.     log_format example.com '$remote_addr - $remote_user [$time_local] "$request" '  
  11.                       '$status $body_bytes_sent "$http_referer" '  
  12.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  13.     sendfile        on;  
  14.     keepalive_timeout 65;  
  15.     upstream web {  
  16.         server 192.168.209.128:80;  
  17.         server 192.168.209.129:80;  
  18.     }  
  19.     server {  
  20.         listen       80;  
  21.         server_name www.example.com;  
  22.         location / {  
  23.             proxy_pass http://web;  
  24.             proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;  
  25.             proxy_set_header    Host    $host;  
  26.             proxy_set_header    X-Real-IP       $remote_addr;  
  27.             proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  
  28.         }  
  29.      access_log /data/logs/example.com.log example.com;  
  30.     }  
  31. }  
轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。
Weight:指定轮询权值,Weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。
ip_hash:每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。
fair:这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。
url_hash:此方法按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包。
 
3.日志轮询,通过Nginx的信号控制功能的脚本来实现日志的自动切割。
[root@master ~]# cat nginxlog.sh
   
   
   
   
  1. #!/bin/bash  
  2. log_dir="/data/logs" 
  3. data_dir=`date +%Y%m%d%H`  
  4. mkdir -p $log_dir/$data_dir >/dev/null 2>&1  
  5. mv $log_dir/access.log $log_dir/$data_dir/access.log  
  6. mv $log_dir/error.log $log_dir/$data_dir/error.log  
  7. kill -USR1 `cat /var/run/nginx.pid` 
配合crontab每天凌晨00:05自动切割
[root@master ~]# crontab -l
5 0 * * * sh /root/nginxlog.sh
 
4.启动脚本
[root@master ~]# vim /etc/init.d/nginx
   
   
   
   
  1. #!/bin/bash  
  2. #date: 2012-05-25  
  3. #chkconfig: 345 85 15  
  4. #description: nginx [engine x] is light http web/proxy server   
  5.    
  6. . /etc/rc.d/init.d/functions  
  7. nginx="/usr/local/nginx/sbin/nginx" 
  8. prog="nginx" 
  9. RETVAL=0 
  10.    
  11.    
  12. start() {  
  13.         echo -n $"Starting $prog:"  
  14.         daemon $nginx  
  15.         RETVAL=$?  
  16.         echo  
  17.         return $RETVAL  
  18.         }  
  19. stop() {  
  20.         echo -n $"Stoping $prog:"  
  21.         daemon pkill -9 nginx  
  22.         echo  
  23.         return $RETVAL  
  24.         }  
  25. reload() {  
  26.         echo -n $"Reloading $prog:"  
  27.         daemon kill -HUP `cat /var/run/nginx.pid`  
  28.         echo  
  29.         return $RETVAL  
  30.         }  
  31. check () {  
  32.         if [ `$nginx -t` ]; then  
  33.                 echo "error"  
  34.         fi  
  35.         }  
  36.    
  37. case "$1" in  
  38.         start)  
  39.                 start  
  40.                 ;;  
  41.         stop)  
  42.                 stop  
  43.                 ;;  
  44.         reload)  
  45.                 reload  
  46.                 ;;  
  47.         check)  
  48.                 check  
  49.                 ;;  
  50.         *)  
  51.         echo $"Usage: $0 {start|stop|restart|check}"  
  52.         RETVAL=1 
  53. esac  
  54.    
  55. exit $RETVAL  
[root@master ~]# chmod a+x /etc/init.d/nginx
[root@master ~]# chkconfig --add nginx
 

 

你可能感兴趣的:(nginx,mysql,PHP,LNMP)