为公司搭建一个测试架构,要求跟真是环境贴近需要安装Nginx、PHP、mysql、memcached、redis!
但是只给了一台R720的服务器,没办法,只能全跑在一台上了
服务器型号:戴尔R710
内存:4G
操作系统:CentOS 6.4
CPU: E5520
各个包版本:
Nginx-1.2.1
Php-5.4.14
Mysql-5.5.29
Memcached-1.2.8
Redis-2.6.14
安装Nginx
一些依赖包
yum install -y gcc gcc-c++ makezlib zlib-devel pcre* libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
1、安装
tar zxf nginx-1.2.1.tar.gz ./configure --prefix=/usr/local/webserver/nginx/ make && make install
2、启动Nginx
#/usr/local/webserver/nginx/sbin/nginx
安装Mysql-5.5
1、准备
groupadd mysql useradd -r -g mysql mysql mkdir /usr/local/webserver/mysql mkdir -pv /usr/local/webserver/mysql/data chown mysql.mysql /usr/local/webserver/mysql/ -R yum -y install gcc gcc-c++ ncurses-devel libtool openssl-devel bison-devel ncurses-devel libaio-devel tar zxf mysql-5.5.29.tar.gz cd mysql-5.5.29
2、首先安装cmake(Mysql-5.5安装不同于以往版本,需要cmake编译)
tar zxf cmake-2.8.7.tar.gz cd cmake-2.8.7 ./configure make && make install
3、编译安装
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DENABLED_PROFILING=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci make make install
注:编译的时候如果没有utf8_general_ci,会出现COLLATION'latin1_swedish_ci' is not valid for CHARACTER SET 'utf8'
4、配置
在源码目录下support-files目录里有很多配置文件的模版
#拷贝主配置文件 cp /root/mysql-5.5.29/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf #创建启动脚本,cp后可以使用/etc/init.d/mysqld start启动 cp /root/mysql-5.5.29/support-files/mysql.server /etc/init.d/mysqld #修改权限 chmod 755 /etc/init.d/mysqld #初始化mysql sh scripts/mysql_install_db --user=mysql --basedir=/usr/local/webserver/mysql/ --datadir=/usr/local/webserver/mysql/data/ #创建链接到标准路径下(修改环境变量指定,效果一样) ln -s /usr/local/webserver/mysql/bin/* /usr/bin/ ln -s /usr/local/webserver/mysql/lib/* /usr/lib/ ln -s /usr/local/webserver/mysql/libexec/* /usr/local/libexec ln -s /usr/local/webserver/mysql/share/man/man1/* /usr/share/man/man1 ln -s /usr/local/webserver/mysql/share/man/man8/* /usr/share/man/man8
注:vim /etc/my.cnf (在[mysqld]里添加一行skip-name-resolve = 1跳过域名解析,加快启动速度)
5、启动mysql
/etc/init.d/mysqld start
进入mysql,以下语句设置root密码删除没用的用户
/usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd use mysql; update user set password=password('newpassword') where user='root'; delete from user where not (user='root') ; delete from user where user='root' and password=''; drop database test; DROP USER ''@'%'; flush privileges;
#mysql的一些安全设置
mysql_secure_installation
#mysql错误文件的位置
/usr/local/webserver/mysql/data/localhost.err
启动时的两个问题:
l启动报错:File'./mysql-bin.index' not found (Errcode: 13)
解决办法:
一般就是权限问题,mysql用户是否对数据库目录内的所有文件具有写的权限
chown mysql.mysql -R data
l启动报错:0911029:10:22 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade tocreate it.
解决办法:
出现这种问题是因为没有指定mysql安装目录和data目录导致的,运行如下命令:
# /usr/local/mysql/scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
安装PHP
1、安装 jpeg
groupadd www useradd -r -g www www yum install bzip2-devel net-snmp-devel #wget http://www.ijg.org/files/jpegsrc.v8d.tar.gz # tar zxvf jpegsrc.v8d.tar.gz # cd jpeg-8d # ./configure -enable-shared -enable-static # make && make install clean
2、安装libmcrypt库
wget http://lcmp.googlecode.com/files/libmcrypt-2.5.8.tar.gz tar fvxz libmcrypt-2.5.8.tar.gz ./configure && make && make install
说明:libmcrypt默认安装在/usr/local
3、安装mhash
tar-zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure make && makeinstall
4、安装mcrypt
tar zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 export LD_LIBRARY_PATH=/usr/local/lib ./configure make && make install
说明:由于在配置Mcrypt时,会找不到libmcrypt的链接库,导致无法编译,因为Libmcrypt的链接库默认在/usr/local/lib文件夹下。因些在配置mcrypt时要加入LD_LIBRARY_PATH=/usr/local/lib 导入键接库
5、开始安装PHP
tar zxf php-5.4.14.tar.gz ./configure --prefix=/usr/local/webserver/php \ --with-libxml-dir=/usr/include/libxml2 \ --with-config-file-path=/usr/local/webserver/php/etc/ \ --with-mysql=/usr/local/webserver/mysql \ --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config \ --with-gd \ --enable-gd-native-ttf \ --with-zlib \ --with-mcrypt \ --with-pdo-mysql=/usr/local/webserver/mysql \ --enable-shmop \ --enable-soap \ --enable-sockets \ --enable-wddx \ --enable-zip \ --with-xmlrpc \ --enable-fpm \ --enable-mbstring \ --with-zlib-dir \ --with-bz2 \ --with-curl \ --enable-exif \ --enable-ftp \ --with-jpeg-dir=/usr/lib64 \ --with-png-dir=/usr/lib64 \ --with-freetype-dir=/usr/lib64 \ --with-iconv \ --enable-xml \ --enable-bcmath \ --enable-sysvsem \ --enable-inline-optimization \ --with-curlwrappers \ --enable-mbregex \ --with-openssl \ --enable-pcntl \ --with-gettext \ --enable-session make make install
Nginx+PHP整合,在安装时必须启用--enable-fpm
6、修改配置
cp php.ini-production /usr/local/webserver/php/etc/php.ini rm -rf /etc/php.ini ln -s /usr/local/webserver/php/etc/php.ini /etc/php.ini cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf
7、修改 /usr/local/etc/php-fpm.conf 文件
pid = run/php-fpm.pid 却掉注释
让Nginx支持PHP需要在定义server主机的时候添加
location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }
在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件,输入以下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; 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_FILENAME $document_root$fastcgi_script_name; 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 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; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
启动:#/usr/local/sbin/php-fpm
安装redis
1、获取安装文件
#wget http://download.redis.io/redis-stable.tar.gz
2、编译安装
tar xzvf redis-stable.tar.gz cd redis-stable make make install
3、配置
mkdir -p /etc/redis cp redis.conf /etc/redis
修改配置文件
#vi /etc/redis/redis.conf
仅修改: daemonize yes
4、启动
/usr/local/bin/redis-server /etc/redis/redis.conf
查看启动
ps -ef | grep redis
安Redis的PHP扩展
1、获取
wget --no-check-certificate http://github.com/owlient/phpredis/tarball/master
2、安装
tar zxf owlient-phpredis-2.1.1-1-g90ecd17.tar && cd owlient-phpredis-90ecd17/ /usr/local/webserver/php/bin/phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config make && make install
3、配置
vim /usr/local/webserver/php/etc/php.ini
添加 extension = "redis.so"
安装memcached
1、下载libevent1.4.11及memcached稳定版本(libevent 是安装 memcached 的唯一前提条件。它是 memcached 所依赖的异步事件通知库)
2、安装libevent1.4.11
wget -c http://cloud.github.com/downloads/libevent/libevent/libevent-1.4.11-stable.tar.gz
./configure -prefix=/usr/local/libevent make && make install
3、安装memcached
./configure -prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make && make install export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/libevent/lib
注:安装memcached时需要指定libevent的安装位置,最后一条命令是将libevent的lib目录加入LD_LIBRARY_PATH里
检查,/usr/local/memcached,有2个目录,安装成功!
4、memcached运行参数
/usr/local/memcached/bin/memcached -d -m 128 -u root -l 192.168.0.97 -c 256 -P /tmp/memcached.pid
5、运行一下报错原因及处理方法:
/usr/local/memcached/bin/memcached: error while loading shared libraries:libevent-1.4.so.2: cannot open shared object file: No such file or directory这样的错误,原因是,memcached默认是到/usr/lib/里找libevent-1.4.so.2,而我们安装的是在/usr/local/lib里面
执行:
ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2
安装Memcache的PHP扩展
1、解压编译安装
cd memcache-2.2.5 /usr/local/webserver/php/bin/phpize #安装PHP扩展都需要执行此脚本 ./configure -enable-memcache -with-php-config=/usr/local/webserver/php/bin/php-config -with-zlib-dir make && make install
安装完成会有类似下面的提示:
Installing shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/
2、修改PHP的配置文件
#vim /usr/local/webserver/php/etc/php.ini 做一下修改
extension_dir = “/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/“ extension = "memcache.so" #(如果有分号,把分号去掉!)
l如果启动memcached时提示未找到eaccelerator.so
需要安装eAccelerator加速器
下载安装eAccelerator
#wget https://github.com/eaccelerator/eaccelerator/tarball/master
最新的版本是eaccelerator-42067ac.tar.gz
#tar zxvf eaccelerator-42067ac.tar.gz
需要phpize安装php扩展,如果不明白phpize可以看这个:http://blog.csdn.net/21aspnet/article/details/8193482
需要注意的是需要写php的安装路径对应的phpize下面的php-config也是
# /usr/local/webserver/php/bin/phpize
#./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
#make
需要注意的是要安装高版本的,低了编译不过去。
#makeinstall
需要记住生成的提示信息,要与php配置文件php.ini的extension_dir处填写的一致
/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/