LNMP部署

  1. mkdir –p /data0/software
  2. cd /data0/software
  3. yum install gcc gcc-c++ autoconf automake make 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 kernel-headers kernel-devel pcre pcre-devel openssl openssl-devel cmake ncurses-devel bison libtool-ltdl libtool-ltdl-devel libevent-devel unixODBC unixODBC-devel libmcrypt-devel mhash-devel libxslt-devel libidn libidn-devel pcre pcre-devel krb5-libs krb5-devel
  4. 为安装PHP做准备:
  5. 安装libiconv1.4

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

tar zxvf libiconv-1.14.tar.gz

cd libiconv-1.14/

./configure –prefix=/usr/local

make && make install

cd ../

2.安装libmcrypt

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

tar -zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

/sbin/ldconfig

Cd libltdl

./configure –enable-ltdl-install

make && make install

cd ../../

3.安装支持库mhash

 

tar –zxvf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9

./configure

make && make install

cd ../

4.建立链接文件:

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

5.安装mcrypt-2.6.8

   tar -zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8

/sbin/ldconfig

./configure

make && make install

cd ../

6.安装MySQL:

useradd mysql

tar -zxvf mysql-5.1.38.tar.gz

cd mysql-5.1.38

./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase

make && make install

chmod +w /usr/local/mysql

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

su – mysql

/usr/local/mysql/bin/mysql_install_db –basedir=/usr/local/mysql –datadir=/data0/mysql/3306/data –user=mysql

Cd /usr/local/mysql/share/mysql

Cp my-huge.cnf /data0/mysql/3306/my.cnf

Cd /data0/mysql/3306/

Cp my.cnf my.cnf.bak

Vim my.cnf,修改文件,内容如下:

more /data0/mysql/3306/my.cnf|grep -v ‘^$’|grep -v ‘#’

[client]

default-character-set = utf8

port           = 3306

socket         = /tmp/mysql.sock

[mysqld]

user = mysql

port           = 3306

socket         = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir = /data0/mysql/3306/data

skip-locking

key_buffer_size = 384M

max_allowed_packet = 1M

table_open_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 8M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size = 32M

thread_concurrency = 8

log-bin=/data0/mysql/3306/binlog

server-id       = 1

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

prompt=”(\u:linxi.com:)[\d]> ”

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

编辑mysql的启动文件:

Vim /data0/mysql/3306/mysql,文件内容如下:

   more /data0/mysql/3306/mysql |grep -v ‘^$’|grep -v ‘#’

mysql_port=3306

mysql_username=”admin”

mysql_password=”12345678″

function_start_mysql()

{

printf “Starting MySQL…\n”

/bin/sh /usr/local/mysql/bin/mysqld_safe –defaults-file=/data0/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &

}

function_stop_mysql()

{

printf “Stoping MySQL…\n”

/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown

}

function_restart_mysql()

{

printf “Restarting MySQL…\n”

function_stop_mysql

sleep 5

function_start_mysql

}

function_kill_mysql()

{

kill -9 $(ps -ef|grep ‘bin/mysqld_safe’|grep ${mysql_port}|awk ‘{printf

$2}’)

kill -9 $(ps -ef|grep ‘libexec/mysqld’|grep ${mysql_port}|awk ‘{printf $

2}’)

}

if [ “$1” = “start” ]; then

function_start_mysql

elif [ “$1” = “stop” ]; then

function_stop_mysql

elif [ “$1” = “restart” ]; then

function_restart_mysql

elif [ “$1” = “kill” ]; then

function_kill_mysql

else

printf “Usage: /data0/mysql/${mysql_port}/mysql {start|stop|restart|kill

}\n”

fi

chmod +x /data0/mysql/3306/mysql

/data0/mysql/3306/mysql start

/usr/local/mysql/bin/mysql –uroot –p –S /tmp/mysql.sock

Grant all privileges on *.* to ‘admin’@’localhost’ identified by ‘12345678’;

Grant all privileges on *.* to ‘admin’@’127.0.0.1’ identified by ‘12345678’;

Grant all privileges on *.* to ‘admin’@’%’ identified by ‘12345678’;

Flush privileges;

Use mysql;

Select host,user,password from user;

Lsof –n –i:3306

Ps –ef|grep mysqld|grep –v grep

/data0/mysql/3306/mysql stop

  1. 编译安装PHP(FastCGI模式):

tar -zxvf php-5.4.22.tar.gz

cd php-5.4.22

cp -frp /usr/lib64/libldap* /usr/lib/

./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-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fpm –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-openssl –with-mhash –enable-pcntl –enable-sockets –with-ldap –with-ldap-sasl –with-xm\lrpc –enable-zip –enable-soap –with-pear

make ZEND_EXTRA_LIBS=’-liconv’

make install

cp php.ini-production /usr/local/php/etc/php.ini

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

vim /etc/profile,添加:

PATH=/usr/local/php/bin:$PATH

PATH=/usr/local/php/sbin:$PATH

chmod 755 /etc/init.d/php-fpm

chkconfig –add php-fpm

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

ps -ef|grep php

  1. 编译安装PHP的扩展:

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 –zxvf eaccelerator-eaccelerator-42067ac.tar.gz

cd eaccelerator-eaccelerator-42067ac

/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

ln -s /usr/local/mysql/include/mysql/* /usr/local/include/

./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql

make && make install

tar -zxvf ImageMagick-6.8.7-10.tar.gz

cd ImageMagick-6.8.7-10

./configure

make && make install

cd ../

tar -zxvf imagick-3.1.2.tgz

cd imagick-3.1.2

/usr/local/php/bin/phpize

./configure –with-php-config=/usr/local/php/bin/php-config

make

make install

8.修改php.ini文件:

手工修改,查找php.ini中的extension_dir=”./”修改为:

Extension_dir=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/”

并在此行后增加几行:

Extension=” memcache.so”

Extension=”pdo_mysql.so”

Extension=”imagick.so”

查找output_buffering = Off改为:

output_buffering = On

修改php.ini时区:

date.timezone = Asia/Shanghai

  1. 配置eAccelerator加速PHP:

mkdir -p /usr/local/eaccelerator_cache

vim php.ini:

按shift+g,跳转到配置文件的最末尾,加上配置信息:

[eaccelerator]

zend_extension=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/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″

  1. 创建www用户和组,创建虚拟机的目录:

Useradd www

mkdir -p /data0/htdocs/blog

chmod +w /data0/htdocs/blog/

chown -R www:www /data0/htdocs/blog/

mkdir -p /data0/htdocs/www

chmod +w /data0/htdocs/www

chown -R www:www /data0/htdocs/www

  1. 创建php-fpm配置文件:

Cp /usr/local/php/etc/php-fpm.conf /usr/local/php/etc/php-fpm.conf.bak

Vim /usr/local/php/etc/php-fpm.conf:

修改成下面的结果:

more php-fpm.conf|grep -v ‘^$’|grep -v ‘;’

[global]

pid = run/php-fpm.pid

error_log = log/php-fpm.log

log_level = notice

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 5s

daemonize = yes

events.mechanism = epoll

[www]

user = www

group = www

listen = 127.0.0.1:9000

listen.mode = 0666

listen.allowed_clients = 127.0.0.1

pm = static

pm.max_children = 128

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 102400

slowlog = log/$pool.log.slow

request_slowlog_timeout = 0s

request_terminate_timeout = 0

rlimit_files = 65535

rlimit_core = 0

chroot =

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

env[OSTYPE] = $OSTYPE

env[MACHTYPE] = $MACHTYPE

env[MALLOC_CHECK_] = 2

php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com

php_flag[display_errors] = on

启动php-fpm:

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

Ps –ef|grep php-fpm|grep –v ‘grep’

  1. 安装nginx所需的pcre库:

tar -zxvf pcre-8.31.tar.gz

cd pcre-8.31

./configure

make && make install

cd ../

  1. 安装Nginx:

tar -zxvf nginx-1.5.8.tar.gz

cd nginx-1.5.8

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

make && make install

cd ../

  1. 创建nginx日志文件:

mkdir -p /data1/logs

chmod +w /data1/logs/

chmod +w /data1/logs

chown -R www:www /data1/logs

  1. 创建及编辑nginx配置文件:

Cd /usr/local/nginx/conf/

Cp nginx.conf nginx.conf.bak

Vim nginx.conf:

more nginx.conf|grep -v ‘^$’|grep -v ‘#’

user www www;

worker_processes 8;

error_log /data1/logs/nginx_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;

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 /data1/logs/access.log access;

sendfile       on;

keepalive_timeout 60;

server {

listen       80;

server_name 192.168.1.109;

location / {

root   html;

index index.html index.htm index.php;

}

error_page   500 502 503 504 /50x.html;

location = /50x.html {

root   html;

}

location ~ .*\.(php|php5)?$ {

fastcgi_pass   127.0.0.1:9000;

fastcgi_index index.php;

include       fcgi.conf;

}

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

{

expires 30d;

}

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

{

expires 1h;

}

}

}

  1. 在/usr/local/nginx/conf/目录中创建fcgi.conf文件:

Cp fastcgi.conf fcgi.conf

Cp fcgi.conf fcgi.conf.bak

Vim fcgi.conf,内容如下:

more fcgi.conf|grep -v ‘^$’|grep -v ‘#’

fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;

fastcgi_param QUERY_STRING      $query_string;

fastcgi_param REQUEST_METHOD     $request_method;

fastcgi_param CONTENT_TYPE       $content_type;

fastcgi_param CONTENT_LENGTH     $content_length;

fastcgi_param SCRIPT_NAME       $fastcgi_script_name;

fastcgi_param REQUEST_URI        $request_uri;

fastcgi_param DOCUMENT_URI       $document_uri;

fastcgi_param DOCUMENT_ROOT     $document_root;

fastcgi_param SERVER_PROTOCOL   $server_protocol;

fastcgi_param HTTPS             $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version;

fastcgi_param REMOTE_ADDR       $remote_addr;

fastcgi_param REMOTE_PORT       $remote_port;

fastcgi_param SERVER_ADDR       $server_addr;

fastcgi_param SERVER_PORT       $server_port;

fastcgi_param SERVER_NAME       $server_name;

fastcgi_param REDIRECT_STATUS   200;

  1. 启动nginx:

/usr/local/nginx/sbin/nginx –t –c /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx

Ps –ef|grep nginx|grep –v ‘grep’

  1. 配置开机自动启动Nginx+PHP:

Vim /etc/rc.local,末尾增加:

more /etc/rc.local |grep -v ‘^$’|grep -v ‘#’

touch /var/lock/subsys/local

/usr/local/nginx/sbin/nginx

/data0/mysql/3306/mysql start

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

  1. 优化Linux内核参数:

Vim /etc/sysctl.conf,在末尾增加内容:

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog = 32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.ip_local_port_range = 1024 65535

/sbin/sysctl -p

  1. 检查nginx配置文件:

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

  1. 创建index.php查看nginx解析PHP文件情况:

<?php

Phpinfo();

?>

  1. 检查nginx和php与mysql的连接情况:

vim mysql_test.php,内容如下:

<?php

$link=mysql_connect(“localhost”,”admin”,”12345678″);

if(!$link)

{

die(‘Could not connect: ‘ .mysql_error());

}

else echo “Mysql test is successful!”;

mysql_close($link);

?>

chmod 777 mysql_test.php

chown -R www:www mysql_test.php

23.nginx日志切割:

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

#! /bin/bash

#This script run at 00:00

#The Nginx logs path

logs_path=”/data1/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/nginx.pid

crontab –e

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

/etc/init.d/crond restart

 

Tagged on:         

发表评论