Centos 6.3 64bit 编译LNMP环境

OS: Centos 6.3 64bit

一、目录规划:
#应用安装目录
/data/apps/
#web 网站目录
/data/webapp/
#数据库存放目录
/data/db/
#应用库
/data/lib/
#Shell 执行目录
/data/shell/
# 软件包
/data/src

二、安装必要的组件
# yum 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 libtool  libtool-libs libevent-devel libevent openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool-ltdl libtool-ltdl-devel bison

三、准备必要的软件源码包
# cd /data/src
# wget http://nginx.org/download/nginx-1.5.8.tar.gz
# wget http://cn2.php.net/distributions/php-5.4.23.tar.bz2
# wget http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.32.tar.gz
# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
# wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
# wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
# wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
# wget http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz

四、安装CMake
MySQL5.5之后的版本安装需要用CMake编译,因此首先要先安装CMake:

# cd /data/src
# tar zxf cmake-2.8.12.1.tar.gz
# cd cmake-2.8.5
# ./configure --prefix=/usr
# make
# make install
# cd ..

五、安装MySQL
tar xzf mysql-5.5.32.tar.gz

a.首先建立mysql用户组及用户:

# groupadd mysql
# useradd -g mysql mysql

b.编译安装MySQL:

# cmake -DCMAKE_INSTALL_PREFIX=/data/apps/mysql/ -DMYSQL_DATADIR=/data/db/mysql/data -DMYSQL_UNIX_ADDR=/tmp/mysql/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DWITH_EXTRA_CHARSETS:STRING=ALL -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/tmp/mysql/mysql.sock -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=yes -DSYSCONFDIR=/data/apps/mysql
# make
# make install

c.赋予MySQl权限:

# chmod +w /data/apps/mysql
# chown -R mysql:mysql /data/apps/mysql

d.创建一系列的MySQL需要目录:

# mkdir -p /data/db/mysql/data/
# mkdir -p /data/db/mysql/binlog/
# mkdir -p /data/db/mysql/relaylog/
# chown -R mysql:mysql /data/db/mysql/

e.以mysql账号建立数据库:

# /data/apps/mysql/scripts/mysql_install_db --user=mysql --basedir=/data/apps/mysql --datadir=/data/db/mysql/data &

f.复制mysql的启动脚本到/etc/init.d/目录方便启动:

# cd support-files
# cp mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld

配置数据库启动目录:

# vim /etc/init.d/mysqld

basedir=/data/apps/mysql
datadir=/data/db/mysql/data

g.复制配置文件

# cp my-medium.cnf /data/db/mysql/my.cnf
# cd ../../

h.配置文件
# vim /data/db/mysql/my.cnf

i.查看有否默认的配置my.cnf在etc目录下,如果有默认的my.cnf文件在etc下启动mysql里会优先引用etc下的配置文件,所以要把/etc/my.cnf文件删除或修个名字
# ll /etc/my.cnf
# mv /etc/my.cnf /etc/my.cnf_bak


[client]
character-set-server = utf8
port    = 3306
socket  = /tmp/mysql.sock

[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /data/apps/mysql
datadir = /data/db/mysql/data
log-error = /data/db/mysql/mysql_error.log
pid-file = /data/db/mysql/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
log-slave-updates
log-bin = /data/db/mysql/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /data/db/mysql/relaylog/relaylog
relay-log-info-file = /data/db/mysql/relaylog/relaylog
relay-log = /data/db/mysql/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

interactive_timeout = 120
wait_timeout = 120

skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

server-id = 1

innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0

log-slow-queries = /data/db/mysql/slow.log
long_query_time = 10

lower_case_table_name = 1

[mysqldump]
quick


j.配置启动MySQL

# /etc/init.d/mysqld start

k.设置MySQL系统自动启动,删除空密码账号:

# chkconfig --add mysqld
# chkconfig --level 345 mysqld on
# service mysqld start
# /data/apps/mysql/bin/mysqladmin -u root password
mysql_password

l.删除空root密码

# /data/apps/mysql/bin/mysql -u root -p
# mysql> use mysql;
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'mysql_password';
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'mysql_password';
# mysql> delete from user where host='127.0.0.1' or host='::1' or host='localhost.localdomain';
# mysql>delete from user where password="";
# mysql>flush privileges;
# mysql>quit;

n.创建libmysqlclient软链接

# ln -s /data/apps/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 (64位系统)
# ln -s /data/apps/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18 (32位系统)

m.查看数据库状态

# /data/apps/mysql/binmysql -u root -p 'mysql_password'
# show full  processlist;
# show status;

至此MySQL编译安装完成。

六、安装PHP所需要的一些库

a.libiconv库的编译安装:

# cd /data/src
# tar xzf libiconv-1.14.tar.gz
# cd libiconv-1.14/
# ./configure --prefix=/usr/local
# make
# make install
# cd ..

b.libmcrypt库的编译安装:

# cd /data/src
# tar xzf 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 ../../

c.建立libmcrypt库软链接:

#64位系统
# ln -sf /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
# ln -sf /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
# ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
# ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
# ln -sf /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
# ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
# ldconfig

#32位系统
# ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
# ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
# ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
# ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
# ln -sf /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
# ln -sf /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
# ldconfig

d. 编译安装mhash

# cd /data/src
# tar xzf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9
# ./configure
# make
# make install
# cd ..

e.添加libmhash相关软链接

#64位系统使用
# ln -sf /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
# ln -sf /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
# ln -sf /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
# ln -sf /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
# ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
# ldconfig

#32位系统使用
# ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
# ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
# ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
# ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
# ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

f.编译安装mcrypt:
# tar xzf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8/
# /sbin/ldconfig
# ./configure
# make
# make install
# cd ..

七、安装PHP

a.添加PHP依赖的软链接(64位系统)

# ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
# ln -s /usr/lib64/libldap.so /usr/lib/libldap.so
# ln -s /usr/lib64/libpng.so /usr/lib/libpng.so

b.(PHP-FPM)编译安装:

# tar xjf php-5.4.23.tar.bz2
# cd php-5.4.16
# ./configure -prefix=/data/apps/php --with-config-file-path=/data/apps/php/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --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-safe-mode --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-xmlrpc --enable-zip --enable-soap --without-pear
# make ZEND_EXTRA_LIBS='-liconv'
# make install

c. 创建PHP的配置文件

# cp -f php.ini-production /data/apps/php/etc/php.ini
# cd ..

d.由于刚才安装的时候试用了without-pear参数没有安装PEAR(因为直接安装会报错),现在我们再来补上:

# wget http://pear.php.net/go-pear.phar
# /data/apps/php/bin/php go-pear.phar

至此PHP的编译安装完毕。

八、安装PHP的扩展模块及配置PHP

接下来我们安装一些PHP常用的模块来优化系统:

a. 用pecl安装memcache扩展库:

# cd /data/apps/php/bin/
# ./pecl install memcache


b.用pecl安装opcache扩展库,目前还是bate版本:

# cd /data/apps/php/bin/
# ./pecl install zendopcache-beta

c.编辑PHP.ini配置文件添加扩展库:

# vim /data/apps/php/etc/php.ini

#大约在870行添加扩展库目录

extension_dir="/data/apps/php/lib/php/extensions/no-debug-non-zts-20100525/"

#添加扩展memcache库

extension=memcache.so

#在文件末尾添加opcache库扩展与配置,注册opcache 要用 zend_extension 而且要全路径

[opcache]
zend_extension=/data/apps/php/lib/php/extensions/no-debug-non-zts-20100525/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

#修改时间配置,找到  ;date.timezone = 修改为:

date.timezone = PRC

#修改PHP的错误日志,找到;error_log =

error_log = /data/apps/php/logs/error.log

d.建立PHP存放日志的目录:

# mkdir -p /data/apps/php/logs

e.建立php-fpm的配置文件:

# cd /data/apps/php/etc
# cp php-fpm.conf.default php-fpm.conf
# mkdir /data/apps/php/run
# vim /data/apps/php/etc/php-fpm.conf

#然后在里面写入下面的内容:

[global]
pid = /data/apps/php/run/php-fpm.pid
error_log = /data/apps/php/logs/php-fpm.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
daemonize = yes
[www]
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
user = www
group = www
listen.mode=0666
pm = static
pm.max_children = 32
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024

request_terminate_timeout = 0s
request_slowlog_timeout = 0s
slowlog = logs/slow.log
rlimit_files = 65535
rlimit_core = 0
chroot =
chdir =
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

php_flag[display_errors] = off


f.然后我们添加www用户组和帐号信息,以便后面的php-fpm和nginx能够正常工作:

# groupadd www
# useradd -g www www

g.接下来,让我们测试下php-fpm配置文件是否正确:

# /data/apps/php/sbin/php-fpm -t

如果出现"NOTICE: configuration file /data/apps/php/etc/php-fpm.conf test is successful"字样则表示配置正确。否则需要检查配置文件。

九、安装Nginx

a.首先我们创建网站存放的根目录:

# mkdir -p /data/web/wwwroot

这里可以根据需要建立你自己喜欢的Web根目录地址,不过注意后面的配置文件相应地方需要做修改。第一次进行编译操作或者初学者建议保持这里的配置不改动,以便顺利完成编译安装工作。

b.然后创建Nginx所需的日志目录并分配权限:

# mkdir -p /var/log/nginx
# chmod +w /var/log/nginx
# chown -R www:www /var/log/nginx

c.然后编译安装Nginx所需的pcre库:

# cd /data/src
# tar xjf pcre-8.21.tar.bz2
# cd pcre-8.21/
# ./configure
# make
# make install
# cd ..
# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1

d.安装所需的GD库:
# yum install gd gd-devel

e.接下来,编译安装Nginx:
# cd /data/src
# tar xzf nginx-1.5.8.tar.gz
# cd nginx-1.5.2/
# ./configure --user=www --group=www --prefix=/data/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_image_filter_module
# make
# make install
# cd ..

f.然后编辑Nginx的配置文件:

# vim /data/apps/nginx/conf/nginx.conf
# mkdir /data/apps/nginx/conf/vhosts

#将对应的内容改为如下:

user www www;
worker_processes  1;

error_log  /data/apps/nginx/logs/error.log crit;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /data/apps/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections  51200;
}


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 20m;

    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;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";

    server_tokens off;

    log_format  access  '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer"'
    '"$http_user_agent" $http_x_forwarded_for';

    server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /data/web/wwwroot;

    location ~ .*\.(php|php5)?$
    {
        try_files $uri = 404;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location /status {
        stub_status on;
        access_log off;
    }


    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires 1h;
    }

    error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/wwwroot;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    }

    include /data/apps/nginx/conf/vhosts/*.conf;
}

注意根据服务器的配置选择不同的值。512M以下内存为1~2;1G内存4;2G内存8;4G内存16,以此类推。

g.运行下面的命令测试Nginx的配置文件是否正确:

/data/apps/nginx/sbin/nginx -t

如果出现下面提示的一种则表示配置文件无误,否则需要做进一步修改:

nginx: the configuration file /data/apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/apps/nginx/conf/nginx.conf test is successf

h.这样Nginx的安装与配置也就完成。

十、设置开机启动

编译好了的软件默认是不会开机自动启动的,需要处理下以便实现开机自启动。

a.首先是MySQL(在编译MySQL已经添加):

# chkconfig –add mysqld
# chkconfig –level 345 mysqld on

b.然后php-fpm和Nginx的启动放到/etc/rc.local启动文件中:

# vim /etc/rc.local

#在文件的末尾增加下面的内容:

# ulimit -SHn 65535
# /data/apps/php/sbin/php-fpm
# /data/apps/nginx/sbin/nginx

保存退出后即可。

至此,所有的编译安装工作都结束。让我们启动php-fpm和Nginx来做测试吧!(MySQL在上面已经启动了)在控制台里面输入下面命令:

# ulimit -SHn 65535
# /data/apps/php/sbin/php-fpm
# /data/apps/nginx/sbin/nginx

即可启动php-fpm和nginx。

十一、各项服务的启动与停止

1、MySQL:

启动:# /etc/init.d/mysqld start
停止:# /etc/init.d/mysqld stop
重启:# /etc/init.d/mysqld restart

2、php-fpm:

启动:# /data/apps/php/sbin/php-fpm
停止:# killall php-fpm
php-fpm 关闭:
kill -INT `cat /data/apps/php/run/php-fpm.pid`
php-fpm 重启:
kill -USR2 `cat /data/apps/php/run/php-fpm.pid`

查看php-fpm进程数:

ps aux | grep -c php-fpm

3、Nginx:

启动:# /data/apps/nginx/sbin/nginx
停止:# killall nginx
平滑重启: # /data/apps/nginx/sbin/nginx -s reload

如果你有更多的Linux知识,可以考虑将php-fpm和Nginx的启动做成启动脚本放在/etc/init.d/下。

十二、遇到的问题

在我编译安装的时候,Nginx在启动之后报错:

[emerg] 1552#2780: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32

网上查了一下资料,这个错误表示无法构建server_names_hash,应该增加server_names_hash_bucket_size的值。

解决办法很简单,在Nginx的配置文件的"http {"这一节加上下面的内容即可:

server_names_hash_bucket_size 64;

至此,全部的安装和配置工作结束。

你可能感兴趣的:(PHP技术,Linux,MySQL)