LNMP 部署(Linux+Nginx+Mysql+Php)

1. 安装所需各种依赖包

yum –y install

gcc gcc-c++autoconf automakebison flex freetype freetype-devel fontconfig-develgettext-devel libjpeglibjpeg-devel libpng libpng-devel libxml2 libxml2-devellibtool libtool-ltdllibtool-ltdl-devel libtiff-devel libXpm-devel libiculibicu-devel libidn libidn-devellibxslt libxslt-devel zlib zlib-devel glibcglibc-devel glib2 glib2-devel bzip2bzip2-devel ncurses ncurses-devel curlcurl-devel e2fsprogs e2fsprogs-devel krb5krb5-devel openssl openssl-developenldap openldap-devel nss_ldapopenldap-clients openldap-servers pcrepcre-devel pam-devel vim-enhancedmysql-devel php-devel libevent libevent-devel


2.准备lnmp其他的源代码包

wgethttp://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wgethttp://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wgethttp://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz

 

3.安装php-5.3.28源代码包所需要的函数支持包

tar zxvflibiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

(libiconv库为需要做转换的应用提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换)

tar zxvflibmcrypt-2.5.8.tar.gz 
cd libmcrypt-2.5.8/
./configure
make
make install
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

(libmcrypt是加密算法扩展库。支持DES,3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256,ARCFOUR, SERPENT, SAFER+等算法。)

tar zxvf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9/
./configure
make &&make install
cd ../
(加密算法支持)
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make
make install
cd ../


4. 编译安装MySQL 5.1.73-m3

groupadd mysql
useradd -g mysql mysql

 mkdir -p /data/mysql

#创建数据存放目录

chown mysql.mysql -R/data/mysql/

mkdir -p /usr/local/mysql/

chown mysql.mysql -R/usr/local/mysql/

tar zxvfmysql-5.1.73-m3.tar.gz
cd mysql-5.1.73-m3
./configure --prefix=/usr/local/mysql--datadir=/data/mysql--with-charset=utf8--with-extra-charsets=complex--enable-thread-safe-client--with-big-tables --with-ssl --with-embedded-serve--enable-local-infile --enable-assembler--with-plugins=innobase--with-plugins=partition

Make &&make install        #编译安装                                     

 #############################################################################

--prefix=/usr/local/mysql//MySQL安装目录 
--datadir=/mydata //数据库存放目录 
--with-charset=utf8 //
使用UTF8格式 
--with-extra-charsets=complex //
安装所有的扩展字符集 
--enable-thread-safe-client //
启用客户端安全线程 
--with-big-tables //
启用大表 
--with-ssl //
使用SSL加密 
--with-embedded-server //
编译成embeddedMySQL library (libmysqld.a), 
--enable-local-infile //
允许从本地导入数据 
--enable-assembler //
汇编x86的普通操作符,可以提高性能 
--with-plugins=innobase //
数据库插件 
--with-plugins=partition //
分表功能,将一个大表分割成多个小表

chown-R mysql:mysql/usr/local/mysql/

#MySQL目录的权限给MySQL用户和组 
/usr/local/mysql/bin/mysql_install_db--user=mysql
#
MySQL来初始化数据库,安装mysqltest数据库

chown -R mysql:mysql/usr/local/mysql/var/

#把初始化的数据库目录给MySQL所有者
/usr/local/mysql/bin/mysqld_safe--user=mysql &
#
启动mysql服务
/usr/local/mysql/bin/mysqladmin-uroot password  123
#
修改mysql登录密码为123
/usr/local/mysql/bin/mysql-uroot -p123 
#
mysql登录

其他:
#cp /usr/local/src/software/mysql-5.1.38/support-files/mysql.server/etc/init.d/mysqld 
#chmod 755 /etc/init.d/mysqld 
#chkconfig --add mysqld 
#chkconfig mysqld on 
#service mysqld restart 

登陆测试: 

#cd /usr/local/mysql/bin 
#mysql 
>show databases; 

# MySQL安装结束

 

5.编译安装PHP(FastCGI模式。使用fastCGI管理php,加快php解析速度)

tar zxvf php-5.3.28.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.3.28 -p1
#-p  1   
是数字
#
解压并打补丁,让php支持fpm来方便管理php-cgi进程(使用php-fpm管理fastCGI

#gzip   -c  保留源文件  -d  解压

cd php-5.3.28/

./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-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-inline-optimization--with-curl --with-curlwrappers--enable-mbregex --enable-fastcgi --enable-fpm--enable-force-cgi-redirect--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
make ZEND_EXTRA_LIBS=‘-liconv‘
#
编译过程设定变量(编译过程需要)
make install

cp/php.ini-dist /usr/local/php/etc/php.ini
cd ../

 

6.准备编译安装PHP5扩展模块

tarzxvf memcache-2.2.5.tgz

cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
make &&make install
cd ../

 

cp /usr/local/php/php-5.3.28/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm
这时候还不能使用/etc/init.d/php-fpm start启动,需修改一下php-fpm配置文件才可以,配置文件位置php安装目录 etc 目录下,默认有个参考文件.
cp php-fpm.conf.default php-fpm.conf
nano php-fpm.conf
,pm.start_servers = 20
,pm.min_spare_servers = 5
,pm.max_spare_servers = 35

 

 

7.修改php.ini文件,让php模块生效

cd/usr/local/php/php-5.3.28/ext/ftp

/usr/local/php/bin/phpize来执行

查找extension_dir= "./"
修改为:
vi /usr/local/php/etc/php.ini
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
手工添加
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找output_buffering= Off
修改为output_buffering= On
再查找 ; cgi.fix_pathinfo=0
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞

 

8.配置eAccelerator加速PHP:

mkdir -p/usr/local/eaccelerator_cache
vi /usr/local/php/etc/php.ini

/usr/local/php/bin/phpize

到配置文件的最末尾,粘上以下内容:
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

 

9.准备php-cgi和nginx进程执行者用户

useraddnginx


10.创建php-fpm配置文件-php-fpm.conf

vi/usr/local/php/etc/php-fpm.conf
<value name="display_errors">0</value>
#0
改成1,页面上会输出错误日志.  取消注释
unix user of processes
     <valuename="user">nginx</value>
      Unix group of processes
      <value name="group">nginx</value>                         
取消注释

     <valuename="max_children">128</value>
#
最大子进程数128,如果内存小于2G,则64个最佳
<value name="rlimit_files">65535</value>
# Set open file desc rlimit,
同时打开的文件数,linux系统允许同时打开的文件数为1024,修改linux系统中允许同时打开的文件,ulimit-SHn 65535,而且这个参数重启后还能生效,加到/etc/profile全局配置文件的最后,开机就会生效,ulimit-a查看openfiles 65535
       ulimit 
用户控制shell启动进程所占用的资源
             -H  
设定硬性资源限制,也就是管理员设定的限制
             -S   
设定软性资源限制,弹性限制
             -n   
设定可同时打开的最大文件个数
             -f    
设定单个文件最大大小
             -a   
查看目前的限制
<value name="max_requests">1024</value>
#
最大请求数, Howmuch requests each process should executebefore respawn.一个子进程能够回应1042个请求11. 启动php-cgi(fastcgi)进程,监听127.0.0.19000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为nginx

/usr/local/php/sbin/php-fpmstart
#
启动php-cgi
/usr/local/php/sbin/php-fpm reload
#
重新加载配置文件
/usr/local/php/sbin/php-fpm stop
#
关闭php-fpm,此时nginx肯定连不上php

##Php-fpm启动:

/etc/init.d/php-fpm start

 

11.安装Nginx所需的pcre库

tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

yum installpcre* //如过你已经装了,请跳过这一步

#为了支持rewrite功能,我们需要安装pcre

yum installopenssl*

#安装openssl

#需要ssl的支持,如果不需要ssl支持,请跳过这一步

yum installzlib zlib-devel

# gzip 类库安装


12.安装Nginx

tar zxvf nginx-0.8.46.tar.gz

cd nginx-0.8.46/

./configure --user=nginx--group=nginx--prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module--with-http_gzip_static_module


Nginx添加proxy_cache模块,对视频进行缓存

wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz

tar zxf tar zxvfngx_cache_purge-2.0.tar.gz
nginx
ngx_cache_purge必须在同一级目录

cd nginx-1.6.1
/usr/local/nginx/sbin/nginx –V

#查看编译参数

 

./configure --user=nginx--group=nginx--prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module--with-http_gzip_static_module --add-module=../ngx_cache_purge-2.0

make &&make install


user  nginxnginx;               #定义Nginx运行的用户和用户组

worker_processes  1;

error_log logs/error.log;

pid       logs/nginx.pid;       #文件描述符数量

worker_rlimit_nofile 65535;

events                           #工作模式与连接数上限

{

    use epoll;

   worker_connections 65535;

}

 

 

http                             #设定http服务器

 {

   include       mime.types;

   default_type  application/octet-stream;

    #charsetutf-8;

 

   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;

 

   server_names_hash_bucket_size128;

   client_header_buffer_size32k;

   large_client_header_buffers4 64k;

    sendfile       on;

   tcp_nopush     on;

 

   keepalive_timeout 120;

 

  #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。

   fastcgi_connect_timeout300;

   fastcgi_send_timeout300;

   fastcgi_read_timeout300;

   fastcgi_buffer_size64k;

    fastcgi_buffers464k;

   fastcgi_busy_buffers_size128k;

   fastcgi_temp_file_write_size128k;

 

  #gzip模块设置

    gzip on;

   gzip_min_length 1k;

   gzip_buffers 4 16k;

   gzip_http_version1.0;

   gzip_comp_level 2;

   gzip_typestext/plain application/x-javascript text/cssapplication/xml;   #压缩类型

    gzip_varyon;

#limit_zonecrawler$binary_remote_addr 10m;        #开启限制IP连接数的时候需要使用

 

    server

     {

       listen       80;                                             #监听端口

       server_name 122.112.94.100;                       #主机名,或IP。如果是主机名,要能够DNS解析

 

       location / {

           root   html;                                             #网站主目录。/usr/local/nginx/html/

           index  index.html index.htmindex.php;         #默认网页顺序

                   }

       error_page   500 502 503 504  /50x.html;

        location =/50x.html {

           root   html;

                             }

 

    #limit_conn   crawler 20;

   location~.*\.(php|php5)?$            #正则表达式:碰到网页文件名是.php.php5结尾

  {

   root           html;

   fastcgi_pass  127.0.0.1:9000;          #连接fastcgi,用来解析php语句

   fastcgi_index index.php;              #首页为index.php

   fastcgi_param  SCRIPT_FILENAME /usr/loal/nginx/html$fastcgi_script_name;  #启动fast-cgi

   include       fastcgi_params;          #包括fastcgi_params中参数

   }

 

    location~.*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

     expires     30d;                   #图片格式缓存30

    }

    location~.*\.(js|css)?$

    {

     expires     1h;                     #js/css缓存2小时

    }

   log_format  access ‘$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 access;

     }

}

 

    #server {

   #    listen      8000;

   #    listen      somename:8080;

   #    server_name somename  alias  another.alias;

 

   #    location / {

   #        root  html;

   #        index index.html index.htm;

   #    }

    #}

 

 

    # HTTPS server

    #

    #server {

   #    listen      443 ssl;

   #    server_name localhost;

 

   #    ssl_certificate      cert.pem;

   #    ssl_certificate_key  cert.key;

 

   #    ssl_session_cache    shared:SSL:1m;

   #    ssl_session_timeout  5m;

 

   #    ssl_ciphers HIGH:!aNULL:!MD5;

   #    ssl_prefer_server_ciphers  on;

 

   #    location / {

   #        root  html;

   #        index index.html index.htm;

   #    }

    #}

 


本文出自 “璀璨·人生” 博客,谢绝转载!

LNMP 部署(Linux+Nginx+Mysql+Php)

上一篇:postgresql常用命令


下一篇:用SQL语句给一个表的增加自增主键或删除主键