LNMP

编译安装LNMP Centos 6.5 x64 + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19

环境:
系统硬件:Oracle VM VirtualBox (CPU:2*4核,内存2G)
系统版本:CentOS-6.5-x86_64-bin-DVD1.iso
软件下载:http://pan.baidu.com/s/1nt2zcr3
安装步骤:


1.系统环境
1.1 更新系统 [按需求执行]
[root@centos ~]# yum update -y

1.2 查看环境
[root@centos ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@centos ~]# uname -a
Linux zabbix 2.6.32-431.17.1.el6.x86_64 #1 SMP Wed May 7 23:32:49 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@centos ~]# yum -y install lsof gcc gcc-c++

2.安装Google-perftools (使用tcmalloc 加速 mysql 和 nginx)

2.1下载需要的文件
下载 libunwind-1.1.tar.gz 到 /usr/local/src
下载 gperftools-2.2.tar.gz 到 /usr/local/src

2.2 安装libunwind
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf libunwind-1.1.tar.gz
[root@centos ~]# cd libunwind-1.1
[root@centos ~]# ./configure --enable-shared
[root@centos ~]# make && make install

2.3 安装google-perftools
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf gperftools-2.2.tar.gz
[root@centos ~]# cd gperftools-2.2
[root@centos ~]# ./configure --enable-shared --enable-frame-pointers
[root@centos ~]# make && make install

2.4 更新,使动态链接库能够被系统共享
[root@centos ~]# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
[root@centos ~]# ldconfig


3.安装mysql

3.1 下载文件
下载 ncurses-5.9.tar.gz到/usr/local/src
下载 bison-3.0.tar.gz到/usr/local/src
下载 cmake-3.0.0.tar.gz到/usr/local/src
下载 mysql-5.6.19.tar.gz到/usr/local/src

3.2 安装ncurses
[root@centos ~]# yum install ncurses-devel openssl* -y
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf ncurses-5.9
[root@centos ~]# ./configure
[root@centos ~]# make && make install

3.3 安装cmake
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf cmake-3.0.0.tar.gz
[root@centos ~]# cd cmake-3.0.0
[root@centos ~]# ./bootstrap
[root@centos ~]# make && make install

3.4 安装bison
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf bison-3.0.tar.gz
[root@centos ~]# cd bison-3.0
[root@centos ~]# ./configure
[root@centos ~]# make && make install

3.5 创建mysql需要的目录、配置用户和用户组
cat /etc/passwd 查看用户列表
cat /etc/group  查看用户组列表
如果不存在则创建用户和组
[root@centos ~]# groupadd mysql
[root@centos ~]# useradd -g mysql mysql

3.6.安装mysql (需要 cmake ncurses-devel bison 库)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf mysql-5.6.19.tar.gz
[root@centos ~]# cd mysql-5.6.19
[root@centos ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/usr/local/mysql/data/ -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/usr/local/mysql/conf/ -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on -DWITH_SSL=yes  
[root@centos ~]# make && make install
3.6.1修改/usr/local/mysql权限
[root@centos ~]# chown -R mysql:mysql /usr/local/mysql/
3.6.2[配置mysql]
[root@centos ~]# cd /usr/local/mysql
[root@centos ~]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
注意:在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf",在本例中就是 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!在CentOS 6.4版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,将它重命名成别的。否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题
启动数据库  &后台运行
[root@centos ~]/usr/local/mysql/bin/mysqld_safe --user=mysql &
查看3306端口是否开启
[root@centos ~]netstat -tnl|grep 3306
简单的测试
[root@centos ~]bin/mysqladmin version
进入数据库
[root@centos ~]bin/mysql -u root
开机自启动
[root@centos ~]echo "/usr/local/mysql/bin/mysqld_safe   --user=mysql  &" >> /etc/rc.d/rc.local 
[root@centos ~]cat /etc/rc.d/rc.local

3.17 使用tcmalloc优化mysql ( 需要安装google-perftools)
[root@centos ~]# vi /usr/local/mysql/bin/mysqld_safe
在# executing mysqld_safe的下一行,加上: 【12行】
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
3.18 重启服务,查看tcmalloc是否生效 (第二条命令显示即生效)
[root@centos ~]#  /usr/local/mysql/bin/mysqladmin -u root -p reload
[root@centos ~]# lsof -n | grep tcmalloc
如果显示以下类似的信息,即表示tcmalloc生效
mysqld 30347 mysql mem REG 253,0 2177570 544322 /usr/local/lib/libtcmalloc.so.0.2.2


4.安装Nginx
4.1.准备安装
下载 pcre-8.35.tar.gz到/usr/local/src
下载 nginx-1.6.0.tar.gz到/usr/local/src
4.2 更新包
[root@centos ~]# yum install zlib* openssl* -y

4.3 安装Pcre
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf pcre-8.35.tar.gz
[root@centos ~]# cd pcre-8.35
[root@centos ~]# ./configure
[root@centos ~]# make && make install

4.4 创建www用户和组
[root@centos ~]# groupadd www
[root@centos ~]# useradd -g www www
为tcmalloc添加目录,并且赋予适当权限
[root@centos ~]# mkdir -p /tmp/tcmalloc/
[root@centos ~]# chown -R www:www /tmp/tcmalloc/

4.5 安装Nginx (需要 pcre google-perftools 库)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf nginx-1.6.0.tar.gz
伪装服务器信息 [按需求执行]
[root@centos ~]# cd nginx-1.6.0/src/core
[root@centos ~]# vim ./src/core/nginx.h
修改NGINX_VERSION为你希望显示的版号
修改NGINX_VER为你希望显示的名称
修改NGINX_VAR 为你希望显示的名称
安装NGINX
[root@centos ~]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-google_perftools_module
[root@centos ~]# make && make install

4.6 修改 nginx.conf ,令nginx可以 google-perftools实现加速
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
修改前面几行为:
user www www;
worker_processes 8;
error_log logs/error.log crit;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc/;
events{
  use epoll;
  worker_connections 65535;
}

4.7 测试和运行
[root@centos ~]# cd /usr/local/nginx
[root@centos ~]# ldconfig
[root@centos ~]# ./sbin/nginx -t
如果显示下面信息,即表示配置没问题
nginx: the configuration file /usr/local/nginxconf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginxconf/nginx.conf test is successful
输入代码运行nginx服务
[root@centos ~]# ./sbin/nginx
[root@centos ~]# ps au|grep nginx
如果显以类似下面的信息,即表示nginx已经启动
root 2013 0.0 0.0 103156 856 pts/0 S+ 03:22 0:00 grep nginx
启动时如果报异常error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory 
这说明我们的环境还不是和启动需要小小的配置一下 
解决方法(直接运行):
32位系统 [root@sever lib]# ln -s /usr/local/lib/libpcre.so.1 /lib
64位系统 [root@sever lib]# ln -s /usr/local/lib/libpcre.so.1 /lib64
输入代码检测是否支持加速
[root@centos ~]# lsof -n | grep tcmalloc
如果显示类似下面的信息,即表示支持tcmalloc加速 (mysqld和nginx两个线程都支持)
mysqld 20818 mysql mem REG 253,0 2177570 281050/usr/local/lib/libtcmalloc.so.0.2.2
nginx 29454 www 25w REG 253,0 0 288399 /tmp/tcmalloc/.29454
nginx 29455 www 27w REG 253,0 0 288403 /tmp/tcmalloc/.29455

5 安装PHP
5.1 准备安装
下载php-5.5.13.tar.gz到/usr/local/src
下载 libiconv-1.14.tar.gz到/usr/local/src
下载 libmcrypt-2.5.8.tar.gz到/usr/local/src
下载mcrypt-2.6.8.tar.gz到/usr/local/src
下载mhash-0.9.9.9.tar.gz到/usr/local/src
更新依赖
yum -y install 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 libXpm* gcc gcc-c++

5.2 安装libiconv (加强系统对支持字符编码转换的功能)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf libiconv-1.14.tar.gz
[root@centos ~]# cd libiconv-1.14
[root@centos ~]# ./configure --prefix=/usr/local/libiconv
[root@centos ~]# make && make install

5.3 安装libmcrypt(加密算法库,PHP扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库)
5.3.1 安装libmcrypt
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf libmcrypt-2.5.8.tar.gz
[root@centos ~]# cd libmcrypt-2.5.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install

5.3.2安装libmcrypt---> 下的 libltdl
[root@centos ~]# cd libltdl/
[root@centos ~]# ./configure --enable-ltdl-install
[root@centos ~]# make && make install

5.3.3 更新共享
[root@centos ~]# ln -sf /usr/local/lib/libmcrypt.* /usr/lib64/
[root@centos ~]# ln -sf /usr/local/bin/libmcrypt-config /usr/lib64/
[root@centos ~]# ln -sf /usr/local/libiconv/lib/libiconv.so.2 /usr/lib64/
[root@centos ~]# ldconfig

5.4 安装mhash(hash加密算法库)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf mhash-0.9.9.9.tar.gz
[root@centos ~]# cd mhash-0.9.9.9
[root@centos ~]# ./configure
[root@centos ~]# make && make install

更新共享
[root@centos ~]# ln -sf /usr/local/lib/libmhash.* /usr/lib64/
[root@centos ~]# ldconfig

5.5 安装mcrypt
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf mcrypt-2.6.8.tar.gz
[root@centos ~]# cd mcrypt-2.6.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install

5.6.2 安装
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf php-5.5.13.tar.gz
[root@centos ~]# cd php-5.5.13
[root@centos ~]# ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-mysql=/usr/local/mysql/ --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv=/usr/local/libiconv --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 --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 --disable-safe-mode --with-curlwrappers --enable-opcache=no
环境不同错误信息不同,如果编译出错,根据错误提示,去掉指定扩展即可
[root@centos ~]# make && make install
/root/dev/php-5.3.6/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] Error 127
解决办法是
ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib/
出现了cp: cannot stat `ext/phar/phar.phar': No such file or directory
cd  ext/phar/
ls -l
结果发现没有phar.phar 这个文件!!于是我怀疑是phar.php ,修改
cp ./phar.php  ./phar.phar
好了 

5.6.3 复制配置文件【安装目录copy】
[root@centos ~]# cp php.ini-production /usr/local/php/etc/php.ini

5.6.4 安装memcache扩展(已经安装PHP)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf memcache-3.0.6.tar.gz
[root@centos ~]# cd memcache-3.0.6
[root@centos ~]# /usr/local/php/bin/phpize
[root@centos ~]# ldconfig
[root@centos ~]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@centos ~]# make && make install
修改php配置文件,支持memcache
[root@centos ~]# vi /usr/local/php/etc/php.ini
在文件中搜索extension_dir、extension ,在相应位置添加下面两行
extension = "memcache.so"
redis安装同上即可

5.6.4.1 安装memcached扩展(已经安装PHP)
安装libmemcached
tar zxvf libmemcached-0.42.tar.gz
cd libmemcached-0.42
./configure --prefix=/usr/local/libmemcached  --with-memcached
make && make install
安装要注意的问题:
1,  安装过程中不要忘了,--with-memcached,不然会提示你
checking for memcached... no
configure: error: "could not find memcached binary"
2,你的memcached是不是1.2.4以上的,如果不是会提示你
clients/ms_thread.o: In function `ms_setup_thread':
/home/zhangy/libmemcached-0.42/clients/ms_thread.c:225: undefined reference to `__sync_fetch_and_add_4'
clients/ms_thread.o:/home/zhangy/libmemcached-0.42/clients/ms_thread.c:196: more undefined references to `__sync_fetch_and_add_4' follow
collect2: ld returned 1 exit status
make[2]: *** [clients/memslap] Error 1
make[2]: Leaving directory `/home/zhangy/libmemcached-0.42'
解决办法是--disable-64bit CFLAGS="-O3 -march=i686",如果不用这个64位的long型数据,我想php扩展memcached,memcache也就没什么区别了,装memcached也就没什么意思了。
php的扩展memcached的安装
tar zxvf memcached-1.0.2.tar.gz
cd memcached-1.0.2
/usr/local/php/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached
./configure --prefix=/usr/local/phpmemcached  --with-memcached
make && make install
vi /usr/local/php/lib/php.ini
加上extension=memcached.so重起一下服务就可以了。

5.6.5 安装php-fpm
[root@centos ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@centos ~]# vim /usr/local/php/etc/php-fpm.conf
修改以下地方
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
pm.max_children = 35
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
[www]
user = www
group = www

5.6.6 修改nginx,支持php
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
找到并修改以下代码
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}

5.6.7将php-fpm 作为服务运行
[root@centos ~]# cd /usr/local/src/php-5.5.13
[root@centos ~]# cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos ~]# chmod 700 /etc/init.d/php-fpm
[root@centos ~]# chkconfig --add php-fpm
[root@centos ~]# chkconfig --level 345 php-fpm on
服务方式启动php-fpm
[root@centos ~]# service php-fpm restart


你可能感兴趣的:(LNMP)