说明:
php在编译安装时,nginx要想能够调用php提供动态php格式的网页,必须用FastCGI来实现,但 FastCGI只是一个框架,实现FastCGI框架的有PHP-FPM,但对于5.2.x版本的php来说,默认是不支持PHP-FPM的,需要打上php-fpm的补丁,对于5.3.2之前版本的也是需要打补丁的,而且打补丁的过程比较麻烦。好在5.3.3版 本的PHP-FPM被直接做进了源代码包中,在编译安装时只需启用PHP-FPM功能即可。但如果要使用PHP-FPM的话,还需要提供以下几个功能:- 需要提供可以解析xml格式的文档,需要安装libxml2 和libxml2-devel这两个包,好在这两个包在安装完开发环境后这两个包是默认安装过的。
- 需要安装libevent并且在1.4.12之后的版本,不幸的是rhel5.4版本中这个包是是在1.4.12之前的,需要从新手动编译安装该包。
- libiconv 用来提供网络连接方式的功能组件,可以实现更快速的网络访问,这个组件系统上是没有装的,需要手动编译安装。
构建编译环境:
yum -y install gcc openssl-devel zlib-devel pcre-develyum groupinstall "Developement Tools" "Development Libraries" -yt
首先安装Nginx:
wget http://nginx.org/download/nginx-1.0.14.tar.gztar zxvf nginx-1.0.14.tar.gz #useradd -s /sbin/nologin -M nginxcd nginx-1.0.14./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/make && make install
创建nginx的启动脚本:
vim /etc/init.d/nginxd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | #!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid # Source function library.. /etc/rc.d/init.d/functions # Source networking configuration.. /etc/sysconfig/network # Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx"prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done} start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval} stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval} restart() { configtest || return $? stop sleep 1 start} reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo} force_reload() { restart} configtest() { $nginx -t -c $NGINX_CONF_FILE} rh_status() { status $prog} rh_status_q() { rh_status >/dev/null 2>&1} case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac |
chmod +x /etc/init.d/nginxd #给予脚本执行权限chkconfig --add nginxd #加入开机启动选项中chkconfig nginxd on #设置开机自动启动service nginxd start #启动nginx 服务
测试访问:
安装MySQL:
下载:wget http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.5/mysql-5.5.22.tar.gztar zxvf mysql-5.5.22.tar.gzcd mysql-5.5.22-linux2.6-i686/usr/sbin/groupadd mysql #添加mysql用户/usr/sbin/useradd -g mysql mysql #添加mysql组
编译:
./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=partition,innobase,myisammrgmake&&make install
编译时出现错误:
../depcomp: line 571: exec: g++: not foundmake[1]: *** [my_new.o] Error 127make[1]: Leaving directory `/root/lnmpsrc/mysql-5.1.62/mysys'make: *** [all-recursive] Error 1
在其他安装g++的服务器上查看g++属于哪个包:
[root@vps ~]# find / -name g++/usr/bin/g++[root@vps ~]# rpm -qf /usr/bin/g++gcc-c++-4.4.6-3.el6.i686
可以看出g++属于gcc-c++包
安装gcc-c++:[root@vps ~]#yum install gcc-c++ -y
重新编译:
./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=partition,innobase,myisammrgmake&&make install
改变/usr/local/mysql目录用户和属组:
chown -R mysql.mysql /usr/local/mysql
创建mysql数据库、日志存放目录
mkdir /mysql/{data,binlog,relaylog} -pchown -R mysql.mysql /mysql
以mysql帐号建立数据表:
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/mysql/data --user=mysql
创建mysql配置文件:
vim /mysql/my.cnf
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | [client] character-set-server = utf8port = 3306socket = /tmp/mysql.sock [mysqld]character-set-server = utf8replicate-ignore-db = mysqlreplicate-ignore-db = testreplicate-ignore-db = information_schemauser = mysqlport = 3306socket = /tmp/mysql.sockbasedir = /usr/local/mysqldatadir = /mysql/datalog-error = /mysql/mysql_error.logpid-file = /mysql/mysql.pidopen_files_limit = 10240back_log = 600max_connections = 5000max_connect_errors = 6000table_cache = 614external-locking = FALSEmax_allowed_packet = 32Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 300#thread_concurrency = 8query_cache_size = 512Mquery_cache_limit = 2Mquery_cache_min_res_unit = 2kdefault-storage-engine = MyISAMthread_stack = 192Ktransaction_isolation = READ-COMMITTEDtmp_table_size = 246Mmax_heap_table_size = 246Mlong_query_time = 3log-slave-updateslog-bin = /mysql/data/binlogbinlog_cache_size = 4Mbinlog_format = MIXEDmax_binlog_cache_size = 8Mmax_binlog_size = 1Grelay-log-index = /mysql/relaylog/relaylogrelay-log-info-file = /mysql/relaylog/relaylogrelay-log = /mysql/relaylog/relaylogexpire_logs_days = 30key_buffer_size = 256Mread_buffer_size = 1Mread_rnd_buffer_size = 16Mbulk_insert_buffer_size = 64Mmyisam_sort_buffer_size = 128Mmyisam_max_sort_file_size = 10Gmyisam_repair_threads = 1myisam_recover interactive_timeout = 120wait_timeout = 120 skip-name-resolve#master-connect-retry = 10slave-skip-errors = 1032,1062,126,1114,1146,1048,1396 #master-host = 192.168.1.1#master-user = username#master-password = password#master-port = 3306 server-id = 1 innodb_additional_mem_pool_size = 16Minnodb_buffer_pool_size = 512Minnodb_data_file_path = ibdata1:256M:autoextendinnodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 16Minnodb_log_file_size = 128Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0 #log-slow-queries = /mysql/slow.log#long_query_time = 10 [mysqldump]quickmax_allowed_packet = 32M |
管理mysql脚本:
vim /mysql/mysqld
123456789101112131415161718192021222324252627282930313233343536373839404142 | #!/bin/shmysql_port=3306mysql_username="admin" #帐号密码可以自行创建mysql_password="rootisnosafe" function_start_mysql(){ printf "Starting MySQL...\n" /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/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_mysqlelif [ "$1" = "stop" ]; then function_stop_mysqlelif [ "$1" = "restart" ]; thenfunction_restart_mysqlelif [ "$1" = "kill" ]; thenfunction_kill_mysqlelse printf "Usage: /mysql/mysqld {start|stop|restart|kill}\n"fi |
赋予脚本执行权限:
chmod +x /mysql/mysqld
启动mysql
/mysql/mysqld start
命令行管理mysql:
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
创建一个具有root权限的用户:admin,密码为rootisnosafe
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'rootisnosafe';GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY 'rootisnosafe';
安装php:
先安装libevent和libiconv:wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gztar zxvf libevent-1.4.14b-stable.tar.gzcd libevent-1.4.14b-stable./configure&&make&&make installwget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gztar zxvf libiconv-1.14.tar.gzcd libiconv-1.14./configuremakemake installln -sf /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
现在安装php:
wget http://cn.php.net/distributions/php-5.4.0.tar.gztar zxvf php-5.4.0.tar.gzcd php-5.4.0./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/localmake ZEND_EXTRA_LIBS='-liconv' #因为-liconv的目录不是在/usr/local下所以安装时需要手动指定make install
其中:
–with-mysql和–with-mysqli的路径是你mysql的具体所在的目录–enable-fpm 启动fpm。其他都是些基本选项,简单易懂cp php.ini-production /usr/local/php/etc/php.ini
修改配置文件:
vim /usr/local/php/etc/php-fpm.conf
1234 | pm.max_children = 50pm.start_servers = 10pm.min_spare_servers = 5pm.max_spare_servers = 35 |
启动:
/usr/local/php/sbin/php-fpm &
检查是否正常启动:
netstat -tunlp|grep 9000tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7826/php-fpm
将 /usr/local/php/sbin/php-fpm &加入到rc.local:
echo '/usr/local/php/sbin/php-fpm &' >>/etc/rc.local
配置fastcgi_params 文件:
vim /etc/nginx/fastcgi_params
将内容替换为:
12345678910111213141516171819 | 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-redirectfastcgi_param REDIRECT_STATUS 200; |
最后修改nginx.conf配置文件:
12345678910111213 | vim /etc/nginx/nginx.conflocation ~ \.php$ { root /www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } location / { root /www; index index.php index.html index.htm; } |
保存退出。
启动nginx :service nginxd start
编辑/www/index.php:
123 | |
访问测试:
测试数据库连接:
编辑:/www/index.php 12345 | |
刷新访问。
如果出现OK字样,表示连接正常。
除非注明,文章皆由()原创,转载请标明本文地址
本文地址: