用源码包搭建LNMP架构

一、MYSQL安装
1'yum install -y make cmake gcc gcc-c++ zlib-devel openssl-devel bison ncurses-devel boost-thread
2'tar zxf mysql-5.5.12.tar.gz
3'cd mysql-5.5.12
4'cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装目录
  -DMYSQL_DATADIR=/usr/local/mysql/data \         #数据库存放目录
  -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \   #Unix socket文件路径
  -DWITH_MYISQM_STORAGE_ENGINE=1 \                #安装myisam存储引擎
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \              #安装innodb存储引擎
  -DWITH_ARCHIVE_STORAGE_ENGINE=1 \               #安装archive存储引擎
  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \             #安装blackhole存储引擎
  -DWITH_PARTITION_STORAGE_ENGINE=1 \             #安装数据库分区
  -DENABLED_LOCAL_INFILE=1 \                      #允许从本地导入数据
  -DWITH_READLINE=1 \                             #快捷键功能
  -DWITH_SSL=yes \                                #支持SSL
  -DDEFAULT_CHARSET=utf8 \                        #使用utf8字符
  -DDEFAULT_COLLATION=utf8_general_ci \           #校验字符
  -DEXTRA_CHARSETS=all \                          #安装所有扩展字符集
  -DMYSQL_TCP_PORT=3306 \                         #MySQL监听端口
5‘make (如果还出错就看错误提示,可能是少安装了某些包)
6‘make install
7'重新编译时需要清除旧的对象文件和缓存信息
  make clean
  rm -f CmakeCache.txt
8'useradd -M -s /sbin/nologin mysql
9'cd /usr/local/mysql/support-files
10'cp my-medium.cnf /etc/my.cnf                   #根据你的主机内存复制mysql文件
11'cd ..
12'chown -R mysql.mysql .
13'cd scripts
14'./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
15'cd ..
16'chown -R root .
17'chown -R mysql data
18'cd support-files
19'cp mysql.server /etc/init.d/mysqld(mysqld需要有执行权限)
20'cd ../bin
21'vim ~/.bash_profile(将/usr/local/mysql/bin添加到该文件中)

   PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
22'source ~/.bash_profile
23'echo $PATH(查看是否添加好)
24'/etc/init.d/mysqld start
现在就可以用mysql命令登录了,你最好执行mysql_secure_installation,按照提示完成mysql安全设置,生产环境推荐使用

二、PHP安装
1'ln -s /usr/local/mysql/lib /usr/local/mysql/lib64     #不然php编译的时候找不到mysql的库文件
2'tar zxf libiconv-1.13.1.tar.gz                        #加强系统对支持字符编码转换的功能
  cd libiconv-1.13.1/
  ./configure --libdir=/usr/local/lib64
  make
  make install
3'tar jxf libmcrypt-2.5.8.tar.bz2                       #mcrypt mhash是php加密算法扩展库
  cd libmcrypt-2.5.8
  ./configure --libdir=/usr/local/lib64
  make
  make install
  cd libltdl
  ./configure --libdir=/usr/local/lib64 --enable-ltdl-install
  make
  make install
4'tar zxf mhash-0.9.9.9.tar.gz
  cd mhash-0.9.9.9
  ./configure --libdir=/usr/local/lib64
  make
  make install
5'ldconfig /usr/local/lib64                             #ldconfig是一个动态链接库管理命令
6'tar zxf mcrypt-2.6.8.tar.gz
  cd mcrypt-2.6.8/
  ./configure --libdir=/usr/local/lib64
  make
  make install
#./configure时可能会报这个错:/bin/rm:cannot remove 'libtoolT':No such file or directory 这个可以忽略
7'yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel freetype-devel gmp-devel openldap-devel -y
8'useradd -M -s /sbin/nologin nginx
9'tar jxf php-5.3.6.tar.bz2
  cd php-5.3.6
  ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=usr/    local/mysql/ --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --without-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-libdir=lib64 --with-ldap --with-ldap-sasl --with-mcrypt --with-mhash
  make ZEND_EXTRA_LIBS='-liconv'(编译好后会提示你make test,这个无所谓了,make test花费的时间很长)
  make install
10'cp php.ini-production /usr/local/php/etc/
   mv php.ini-production php.ini
11'cd scai/fpm/
   cp init.d.php-fpm /etc/init.d/php-fpm
12'vim /usr/local/php/etc/php.ini
   cgi.fix_pathinfo=0                         #防止Nginx文件类型错误解析漏洞
13'cd /usr/local/php/etc
   cp php-fpm.conf.default php-fpm.conf
   vim php-fpm.conf                           #去掉一下几行注释
       pin=run/php-fpm.pid
       pm.max_children = 50
       pm.start_servers = 20                  #在生产环境中一定要做压力测试,找到最合适的进程数组合
       pm.min_spare_servers = 5
       pm.max_spare_servers = 35
       pm.max_requests = 500
14'/etc/init.d/php-fpm start
   vim ~/.bash_profile                        #将/usr/local/php/bin添加进去
   source ~/.bash_profile

三‘nginx安装
1'yum install -y pcre-devel
2'tar zxf nginx-1.0.8.tar.gz
  cd nginx-1.0.8
3'vim auto/cc/gcc
  #CFLAGS=”$CFLAGS -g” (注释掉这行,去掉debug模式编译,编译以后程序只有几百k)
4'vim src/core/nginx.h
  #define NGINX_VERSION "1.0.2”
  #define NGINX_VER "nginx" (修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号)
5'./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
6'make && make install
7'vim ~/.bash_profile(将/usr/local/nginx/sbin添加进去)
  source ~/.bash_profile
  (你也可以在/usr/sbin下做一个nginx的链接)
8'vim /usr/local/nginx/conf/nginx.conf
  user nginx nginx;
  worker_processes 8;
  error_log logs/error.log crit;
  pid  logs/nginx.pid;
  worker_rlimit_nofile 65535;
  events {
    use epoll;
    worker_connections 65535;
  }
  http {
    include
    mime.types;
    default_type application/octet-stream;
    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;
    keepalive_timeout 65;
    tcp_nodelay on;
    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;
    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;
    server {
        listen 80;
        server_name station25.example.com;
        location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include
fastcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
    }
  }
9'nginx -t          #检查配置文件的语法
  nginx    #启动nginx
  nginx -s reload   #重载主配置文件
  nginx -s stop     #关闭nginx
10'Nginx支持的信号
  1) TERM,INT 快速关闭
  2) QUIT 从容关闭
  3) HUP 平滑重启,重新加载配置文件
  4) USR1 重新打开日志文件,在切割日志时用处比较大
  5) USR2 平滑升级可执行程序
  6) WINCH 从容关闭工作进程
四‘加入memcache
Memcache是danga.com的一个开源项目,它是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的Hash表,能够用来存储各种格式的数据。可以类比于MySQL这样的服务,而PHP扩展的Memcache实际上是连接Memcache的方式。 
1'yum install memcached -y
2'yum install autoconf -y
3'tar zxf memcache-2.2.5.tar.gz
  cd memcache-2.2.5
  phpize
  ./configure --with-php-config=/usr/local/php/bin/php-config
  make && make install
5'vi /usr/local/php/etc/php.ini
  extension=memcache.so
  service php-fpm reload
6'vi /usr/local/nginx/html/test.php
  <?php
  $memcache = new Memcache;
  $memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
  $version = $memcache->getVersion();
  echo "Server's version: ".$version."\n";
  $tmp_object = new stdClass;
  $tmp_object->str_attr = 'test';
  $tmp_object->int_attr = 123;
  $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
  echo "Store data in the cache (data will expire in 10 seconds)\n";
  $get_result = $memcache->get('key');
  echo "Data from the cache:\n";
  var_dump($get_result);
  ?>
7'visit website: http://192.168.0.25/test.php
8'cp memcache.php /usr/local/nginx/html
9'vi /usr/local/nginx/html/memcache.php
  define('ADMIN_USERNAME','sjx');   // Admin Username
  define('ADMIN_PASSWORD','sjx');  // Admin Password
  $MEMCACHE_SERVERS[] = '192.168.0.25:11211'; // add more as an array
10'visit website: http://192.168.0.25/memcache.php
五‘使用tomcat发布动态页面
1'sh jdk-6u26-linux-x64.bin
2'mv jdk1.6.0_26/ /usr/local/jdk
3'vi /etc/profile
  export JAVA_HOME=/usr/local/jdk
  export CLASSPATH=:$JAVA_HOME/lib
  export PATH=$PATH:$JAVA_HOME/bin
4'source /etc/profile(可以echo $JAVA_HOME,echo $CLASSPATH查看一下)
5‘tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local
6'mv /usr/local/apache-tomcat-7.0.8 /usr/local/tomcat
7'cd /usr/local/tomcat/bin
8'./startup.sh
这时你可以看到机子上有个8080端口,以上web访问时需加上端口号8080
7‘在nginx的配置文件中加入
  location ~ \.jsp$ {
            proxy_pass  http://127.0.0.1:8080;
        }
  这几行即可不需输入8080,这样当遇到动态页面时会自动交给tomcat处理。
8'可以写个简单的jsp文件测试下
  vim test.jsp
  THE TIME IS: <%=new java.util.Date() %>

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