LNMP 源代码安装攻略
CentOS 5.6下安装PHP5.3.8,nginx 1.0.11, Mysql 5.5
Install Mysql 5.5
1) 删除已安装的mysql
yum remove mysql
yum remove php
2) 安装依赖包
yum install gcc gcc-c++ ncurses-devel bison
3) 下载mysql5.5.15和cmake
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.15.tar.gz/from/http://mysql.he.net/
wget http://www.cmake.org/files/v2.8/cmake-2.8.5.tar.gz
4) 安装cmake
tar -xvf cmake-2.8.5.tar.gz
cd cmake-2.8.5
./configure --prefix=/usr/local/cmake
make && make install
5) 创建用户和目录
groupadd mysql
useradd -g mysql mysql
mkdir -p /data/mysql
chown mysql.mysql -R /data/mysql/
6) 编译安装 MySQL 5.5.15
cd /usr/local/src
tar xvf mysql-5.5.15.tar.gz
cd mysql-5.5.15
/usr/local/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1
make && make install
// 更改权限
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
// 创建mysql 数据存放目录
mkdir -p /data/mysql/
mkdir -p /data/mysql/log/
chown -R mysql:mysql /data/mysql/
7) mysql初始化数据库
// copy配置文件
路径:/usr/local/mysql/support-files
cp my-large.cnf /usr/local/mysql/my.cnf
vi /usr/local/mysql/my.cnf
解注释语句 innodb_buffer_pool_size = 256M
//加上以下内容
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
log-error = /data/mysql/log/mysql_error.log
pid-file = /data/mysql/mysql.pid
user = mysql
tmpdir = /tmp
初始化数据库
转化到路径mysql下
scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql
8) 配置启动MySQL 5.5.15
cd support-files/
cp mysql.server /etc/init.d/mysqld
将 mysql 加入开机启动
chmod +x /etc/init.d/mysqld
(编辑此文件,查找并修改以下变量内容:)
vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
执行命令语句
chkconfig --add mysqld
chkconfig --level 345 mysqld on
启动 mysql
service mysqld start
在此需要在etc/profile下进行添加路径path
例:
PATH=$PATH:/usr/local/mysql/bin
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
设置mysql数据库root帐号密码。
mysqladmin -u root password ‘新密码’ [引号内填密码]
让mysql数据库更安全
复制内容到剪贴板
代码:
mysql -u root -p [此时会要求你输入刚刚设置的密码,输入后回车即可
mysql> DROP DATABASE test; [删除test数据库]
mysql> DELETE FROM mysql.user WHERE user = “”; [删除匿名帐户]
mysql> FLUSH PRIVILEGES; [重载权限]
9) 防火墙配置
a.添加.允许访问端口{21: ftp, 80: http}.
iptables -I RH-Firewall-1-INPUT -m state -state NEW -m tcp -p tcp –dport 21 -j ACCEPT
iptables -I RH-Firewall-1-INPUT -m state -state NEW -m tcp -p tcp –dport 80 -j ACCEPT
郁闷.请把上面复制到记事本里,然后把–state这段–改成小写的,再操作.否则粘贴时会出现乱码.
b.关闭防火墙{不推荐}.
service iptables stop
c.重置加载防火墙
service iptables restart
9) 参考资料:
http://hi.baidu.com/mucunzhishu/blog/item/d05574636268e2ca8cb10d59.html
http://www.enet.com.cn/article/2007/0330/A20070330515622.shtml
http://www.txstudy.com/show.aspx?id=7898&cid=11
http://space.itpub.net/7607759/viewspace-695654
http://www.site-school.com/wangyebiancheng/PHP/43580.html
http://blog.51yip.com/mysql/1312.html
http://www.hackbase.com/tech/2011-04-27/63478.html
Install PHP 5.3.8
1) 利用CentOS Linux系统自带的yum命令安装、升级所需的程序
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) 编译安装PHP 5.3.8支持库
libiconv (加强系统对字符编码转换的支持)
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../
libmcrypt(加密算法库,PHP扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库)
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 ../../
mhash(hash加密算法库)
From - http://www.ligaofeng.com/?cat=3 //参考文章
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../
建立libmcrypt ,libmhash相关库的软连接,为编译mcrypt作准备。mcrypt也依赖libmcrypt ,libmhash
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
编译安装mcrypt
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
3) 增加mysql 库的软连接
ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
4) 编译PHP
tar zxvf php-5.3.8.tar.gz
cd php-5.3.8
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib --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
编译开关的解释:
--enable-fpm: needed to enable the FastCGI Process Manager
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-development /usr/local/webserver/php/etc/php.ini
注意:release时需要改为 php.ini-production
5) 编译安装PHP5扩展模块
tar zxvf memcache-2.2.6.gz
cd memcache-2.2.6
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
6) 改php.ini文件
手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
7) 配置eAccelerator加速PHP
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/webserver/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"
8) 创建www用户和组
groupadd www
useradd -g www www
mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www
9) PHP-FPM configuration
Now, copy some config files you’ll need.
cd /usr/local/src/php5.3.6
cp sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
cp sapi/fpm/php-fpm.conf.in /etc/php-fpm.conf
Edit your /etc/init.d/php-fpm and adjust the following values for your system.
php_fpm_BIN=/usr/local/webserver/php/sbin/php-fpm
php_fpm_CONF=/etc/php-fpm.conf
php_fpm_PID=/var/run/php-fpm/php-fpm.pid
Make sure the path to the PID file is in a directory where the PHP-FPM user can write to, otherwise you’ll see your php-fpm children start but the init.d script still return failures.
mkdir -p /var/run/php-fpm
chown www.www /var/run/php-fpm
Now edit your /etc/php-fpm.conf the enable at least the following.
对下列字符段先查找替换,没有的话就进行添加 (注:逐行查找)
[global]
pid = /var/run/php-fpm/php-fpm.pid
listen = 127.0.0.1:9000
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
把php-fpm加入服务列表
/sbin/chkconfig --add php-fpm (添加到Service列表)
/sbin/chkconfig php-fpm on
启动php-fpm
service php-fpm start
查看启动后进程
# lsof -i tcp:9000
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
php-fpm 5140 root 6u IPv4 123964005 TCP localhost.localdomain:cslistener (LISTEN)
php-fpm 5142 php-fpm 0u IPv4 123964005 TCP localhost.localdomain:cslistener (LISTEN)
php-fpm 5143 php-fpm 0u IPv4 123964005 TCP localhost.localdomain:cslistener (LISTEN)
php-fpm 5144 php-fpm 0u IPv4 123964005 TCP localhost.localdomain:cslistener (LISTEN)
php-fpm 5145 php-fpm 0u IPv4 123964005 TCP localhost.localdomain:cslistener (LISTEN)
10) 参考资料:
http://blog.s135.com/nginx_php_v6/
http://mattiasgeniar.be/2011/04/21/guide-running-nginx-1-0-with-php-fpm-5-3-on-centos-5/
http://www.loilee.com/archives/7
安装nginx 1.0.11
1) 安装Nginx所需的pcre库:
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../
2) 安装Nginx
tar zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
3) 创建Nginx日志目录
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs
创建Nginx配置文件
在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf
内容见附件
4) 在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:
vi /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;
5) 修改系统limits
解除 Linux 系统的最大进程数和最大文件打开数限制:
vi /etc/security/limits.conf
# 添加如下的行
* soft noproc 11000
* hard noproc 11000
* soft nofile 65535
* hard nofile 65535
说明:* 代表针对所有用户
noproc 是代表最大进程数
nofile 是代表最大文件打开数
6) 启动Nginx
/usr/local/webserver/nginx/sbin/nginx
7) 配置开机自动启动Nginx
vi /etc/rc.local
在末尾增加以下内容:
/usr/local/webserver/nginx/sbin/nginx
8) 参考:
改变nginx 配置后的平滑启动
/usr/local/webserver/nginx/sbin/nginx -s reload
优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
引用
# Add
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_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
使配置立即生效:
/sbin/sysctl -p
编写每天定时切割Nginx日志的脚本
1) 创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容:
引用
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/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/webserver/nginx/nginx.pid`
2) 设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容:
引用
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
Cd etc/sysconfig/
Vi iptables
修改
Service iptables restart
查看php extension
php -m
php –i
解决mysqlnd报错问题
SET SESSION old_passwords=FALSE;
SET PASSWORD = PASSWORD('[your password]');