lnmp安装配置.doc

Lnmp安装配置

1.准备php函数的rpm

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

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

wget http://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
wget http://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
wget http://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.2.14源代码包所需要的函数支持包

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

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

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.5.3-m3

cd mysql-5.5.3-m3

./configure --prefix=/usr/local/mysql --without-debug --enable-thread-safe-client --with-pthread --enable-assembler --enable-profiling --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-plugins=all --with-mysqld-user=mysql --without-embedded-server --with-server-suffix=-community --with-unix-socket-path=/tmp/mysql.sock

Make

#编译

Make install

#安装

 

复制一份配置文件到安装后的/etc目录下修改mysql配置文件

1.cp /usr/local/mysql/share/mysql/my-medium.cnf   /etc/my.cnf

1.[client]

下增加一行

default-character-set=utf8  #客户端字符集#连接字符集

2.[mysqld]

下增加两行

character-set-server = utf8  #服务器字符集#数据库字符集

collation-server = utf8_general_ci  #服务器,库,表的校验字符集

* 生成MySQL配置文件

 

 setfacl -m u:mysql:rwx -R /usr/local/mysql (如果是复制进去要注意横杠要改过)

 setfacl -m d:u:mysql:rwx -R /usr/local/mysql (如果是复制进去要注意横杠要改过)

 * acl来让mysql用户对/usr/local/mysql有所有权限

 

启动MySQL服务

1.用原本源代码的方式去使用和启动mysql

/usr/local/mysql/bin/mysqld_safe --user=mysql &

查看启动后的进程

pstree -p | grep mysql

查看mysql的端口

netstat -tunpl | grep :3306

 

2.重启以后还要设置开机自动启动:

vi /etc/rc.local

/usr/local/mysql/bin/mysqld_safe --user=mysql &>/dev/null &

 

/usr/local/mysql/bin/mysql_install_db --user=mysql

 *cd / 用上面的命令创建数据库授权表

 

4./usr/local/mysql/bin/mysqladmin -uroot password 123456 

#mysql用户root加密码.密码为123456

 #测试直接用刚刚改好的用户与密码登录test数据库看

 /usr/local/mysql/bin/mysql -u root -p123456

mysql>show databases;

*查看/usr/local/mysql/var/mysql有没有表

mysql>s

#s查看设置好的字符集是utf-8不是

 

 mysql> exit

#退出数据库管理

 

 

 

5. 编译安装PHPFastCGI模式)

tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
#
解压并打补丁,让php支持fpm来方便管理php-cgi进程

cd php-5.2.14/
./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扩展模块

tar zxvf 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 ../

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure && make && make install
cd ../

tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
cd ../

 

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

cp /lnmp/php-5.2.14/php.ini-dist /usr/local/php/etc/php.ini

vi /usr/local/php/etc/php.ini

搜索extension_dir在引号里面加入

/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

搜索extension=msql.so在下面加入

 

extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"

 

接着把下面代码加到文件最后

[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"



再查找output_buffering = Off

修改为output_buffering = On

再查找; cgi.fix_pathinfo=0

把分号注释去了,防止Nginx文件类型错误解析漏洞

 

8. php.ini中配置eAccelerator加速PHP

mkdir -p /usr/local/eaccelerator_cache

#准备eaccelerator缓存目录

 

 

9.准备php-cginginx进程执行者用户

useradd nginx

 

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

vi /usr/local/php/etc/php-fpm.conf

1(改为1两边的注释去掉)

#0改成1,页面上会输出错误日志

Unix user of processes
      nginx (
改为nginx两边的注释去掉)

      Unix group of processes
      nginx  (
改为nginx两边的注释去掉)

 128
#
最大子进程数128,如果内存小于2G,则64个最佳

65535
# Set open file desc rlimit,
同时打开的文件数,linux系统允许同时打开的文件数为1024,修改linux系统中允许同时打开的文件,用命令修改ulimit -SHn 65535,而且这个参数重启后还能生效,加到 /etc/profile全局配置文件的最后,开机就会生效,ulimit -a查看是否open files 65535

1024
#
最大请求数, How much requests each process should execute before respawn.一个子进程能够回应1042个请求

 

11.启动php-cgi(fastcgi)进程,监听127.0.0.19000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为nginx

 

/usr/local/php/sbin/php-fpm start

 

#启动php-cgi  (pstree -p | grep php-cgi查看进程ps -eo user,command | grep php-cgi查看进程执行者)

/usr/local/php/sbin/php-fpm reload

#重新加载配置文件

/usr/local/php/sbin/php-fpm stop

#关闭php-fpm,此时nginx肯定不上php

 

12. 安装Nginx所需的pcre

tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure

 make

 make install (这个软件安装必须一步步走)
cd ../

 

13. 安装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
make

make install  (这个软件安装必须一步步走)

cd ../

 

 

 

 

 

 

14. 修改Nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

user  nginx nginx;

worker_processes 8;

#相当于cpu个数

error_log  logs/nginx_error.log 

pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process. 

黏贴到pid下面
worker_rlimit_nofile 65535;

替换events里面的两条内容
events 
{
  
use epoll;
  worker_connections 65535;
}

接着下面的http区间
http 
{

把下面3行的注释去掉

log_formatmain  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

 

 

  tcp_nopush     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;

 

 

 

接下来到servser区间(一个大括号的servser代表一个虚拟主机)


   server {

listen80;

server_name  192.168.100.2;  (这里写上主机的ip)

 

#charset koi8-r;

 

access_loglogs/host.access.log  main; (这里注释去掉)

 

location / {

roothtml;

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 {

roothtml;

   }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ .php$ {

#proxy_passhttp://127.0.0.1;

#}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ .php$ {

#    roothtml;

#fastcgi_pass127.0.0.1:9000;

#fastcgi_indexindex.php;

#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

#    includefastcgi_params;

#}

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

#

#location ~ /.ht {

#    deny  all;

#}

}

保存退出回来到/usr/local/nginx/sbin里用

 

./nginx –t看看日志是否正确

 

接着启动./nginx

 

ps -ef |grep nginx查看进程

 

netstat -tunpl | grep :80查看端口是否是80

 

浏览器输入测试

http://192.168.100.2/

 

 

在打开配置文件

vi /usr/local/nginx/conf/nginx.conf

搜索9000

把这几行注释去掉

location ~ .php$ {

roothtml;

fastcgi_pass127.0.0.1:9000;

fastcgi_indexindex.php;

fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

includefastcgi_params;

}

 

 

接着打开

vi/usr/local/nginx/conf/fastcgi_params

任意地方加入

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

 

平滑重启

1)pkill -HUP nginx  (只推荐这种重启的命令)

测试是否解析php

http://192.168.100.2/index.php

 

如果不解析!重装phpphp相关的模块等

 

 

16.配置开机启动nginx,php-fpm,ulimit

1)nginx

vi /etc/rc.local

/usr/local/nginx/sbin/nginx

2)php-fpm

vi /etc/rc.local

/usr/local/php/sbin/php-fpm start

3)ulimit

Vi /etc/profile

Ulimit -SHn 65535

 

17.检查nginx配置文件语句错误

/usr/local/nginx/sbin/nginx –t

 

 

 

 

18.平滑重启nginx进程

1)pkill -HUP nginx  (只推荐这种重启的命令)

2)kill -HUP `pgrep -uroot nginx`

3)/usr/local/nginx/sbin/nginx -s reload

 

 

 

19.编写每天定时切割Nginx日志的脚本
1
、创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh

vi /usr/local/nginx/sbin/cut_nginx_log.sh

 

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`



2
、设置crontab,每天凌晨00:00切割nginx访问日志

crontab -e

00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh


20.配置nginx虚拟主机

vi /usr/local/nginx/conf/nginx.conf

==èwww.baidu.com公司网站

server
  {
    listen       80;
    server_name  www.baidu.com;
    index index.html index.php index.htm;
    root  /web/baidu;

    location ~ .*.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;

   fastcgi_param  SCRIPT_FILENAME/scripts$fastcgi_script_name;
      
include fastcgi_params;
    }
    
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }
#
设置图片在客户端缓存时间为30days
    location ~ .*.(js|css)?$
    {
      expires      1h;
    }
#
设置jscss文件在客户端的缓存时间为1hour
    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;

#自定义日志区域
   }

==èwww.sina.com公司网站

 

server
  {
    listen       80;
    server_name  www.sina.com;
    index index.html index.php index.htm;
    root  /web/baidu;

    location ~ .*.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;

   fastcgi_param  SCRIPT_FILENAME/scripts$fastcgi_script_name;
      
include fastcgi_params;
    }
    
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }
#
设置图片在客户端缓存时间为30days
    location ~ .*.(js|css)?$
    {
      expires      1h;
    }
#
设置jscss文件在客户端的缓存时间为1hour
    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;

#自定义日志区域
   }

最后在客户端测试虚拟主机www.baidu.comwww.sina.com两家公司网站