一.LNMP介紹
php
自PHP-5.3.3起,PHP-FPM加入到了PHP核心,編譯時加上--enable-fpm便可提供支持。 PHP-FPM以守護進程在後臺運行,Nginx響應請求後,自行處理靜態請求,PHP請求則通過fastcgi_pass交由PHP-FPM處理,處理完畢後返回。 Nginx和PHP-FPM的組合,是一種穩定、高效的PHP運行方式,效率要比傳統的Apache和mod_php高出很多。html
二.安裝依賴環境
mysql
shell> yum -y install gcc gcc-c++ make cmake automake autoconf cpp ncurses ncurses-devel libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel pcre-devel libtool libtool-libs freetype-devel gd zlib-devel bison readline-devel glibc-devel glib2-devel bzip2-devel gettext-devel libcap-devel libmcrypt-devel libxml2-devel curl-devel libpng-devel openldap-devel
#下載軟件包nginx
shell> wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz shell> wget http://nginx.org/download/nginx-1.4.5.tar.gz shell> wget http://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.30.tar.gz shell> wget http://cn2.php.net/distributions/php-5.4.28.tar.gz
三.系統說明
c++
centos6.4最小化安裝
正則表達式
軟件版本:nginx1.4.5+mysql5.5+php5.4sql
四.編譯安裝nginx1.4.5
shell> tar xvf nginx-1.4.5.tar.gzshell
shell> ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.34 --lock-path=/var/run/nginx.lock --pid-path=/var/run/nginx.pid --user=apache --group=apache shell> make && make install //nginx參數解釋 --with-pcre=/usr/local/src/pcre-8.34 若是要在」location」指令中使用正則表達式,一樣須要PCRE庫,等號後面是pcre源碼的路徑,不是pcre安裝路徑 --with-http_gzip_static_module 容許ngx_http_gzip_static_module模塊(mod_dflate) --with-http_ssl_module 開啓HTTP SSL模塊,使NGINX能夠支持HTTPS請求。這個模塊須要系統已經安裝了OPENSSL,在DEBIAN上是libssl-dev --with-http_stub_status_module 啓用 "server status" 頁 --pid-path=PATH 在nginx.conf中沒有指定pid指令的狀況下,默認的nginx.pid的路徑。若是沒有指定,默認爲 PATH/logs/nginx.pid --lock-path=PATH nginx.lock文件的路徑。
啓動nginx
數據庫
shell> /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#打開瀏覽器測試是否能夠訪問
apache
五.編譯安裝mysql5.5
1.編譯安裝
shell> groupadd mysql && useradd-g mysql mysql -s /sbin/nologin
shell> mkdir-p /data/mysql
shell> cd/home/softs/mysql-5.5.30
shell> cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DMYSQL_DATADIR=/data/dbdata-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_SSL=yes
shell> make&& makeinstall
2.複製配置文件
shell> cp -f /home/softs/mysql-5.5.30/support-files/my-large.cnf /etc/my.cnf
shell> cp -f /home/softs/mysql-5.5.30/support-files/mysql.server /etc/init.d/mysqld
shell> chmod 755 /etc/init.d/mysqld
shell> vim /etc/my.cnf
---------------------------
[mysqld
datadir=/data/mysql
socket=/usr/local/mysql/tmp/mysql.sock
---------------------------
shell> chown mysql.mysql /usr/local/mysql -R
3.初始化數據庫
shell> /usr/local/mysql/scripts/mysql_install_db --user=root --datadir=/data/mysql --basedir=/usr/local/mysql
shell> chown mysql.mysql /data/mysql -R
shell> /etc/init.d/mysqld restart
4.添加環境變量
shell> echo "export PATH=${PATH}:/usr/local/mysql/bin" >> /etc/profile
六.編譯安裝php5.4
1.編譯前說明
#yum安裝運行環境
shell> yum –y install libxml2-devel curl-devel libpng-devel openldap-devel
#安裝PHP前須要編譯安裝這兩個源碼包:libmcrypt mhash
#nginx使用fpm的方式調用php,php5.4之後版本中已經支持php-fpm,因此只須要在php編譯參數裏指定--enable-fpm便可
2.首先安裝libmcrypt-2.5.8
wget http://pkgs.fedoraproject.org/repo/pkgs/libmcrypt/libmcrypt-2.5.7.tar.gz/b1be163143f8e8ed0474beeb642b3bad/libmcrypt-2.5.7.tar.gz
tar xvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install
3.安裝mhash-0.9.9.9
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make && make install
4.創建軟鏈接到/usr/lib
ln -s /usr/local/lib/libmcrypt* /usr/lib
ln -s /usr/local/lib/libmhash* /usr/lib
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/lib64/libldap* /usr/lib/
ln -s /usr/local/mysql/lib/libmysqlclient* /usr/lib/
ldconfig
5.安裝php5.4
tar xvf php-5.4.25.tar.gz
cd php-5.4.25
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-gd --enable-gd-native-ttf --with libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear
【error1】
configure: error: Cannot find ldap libraries in /usr/lib.
【solution1】
shell> ln -s /usr/lib64/libldap* /usr/lib/
shell> ldconfig
【error2】
Generating phar.php
/home/softs/php-5.4.25/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
【solution2】
shell> ln -s /usr/local/mysql/lib/libmysqlclient* /usr/lib/
shell> ldconfig
shell> make && make install
PHP安裝涉及的軟件包:
php5 - 服務器端PHP解釋器
php5-cgi - 服務器端PHP-CGI解釋器
php5-cli - PHP5命令行工具
iphp5-common - PHP5一些基本文件
php5-fpm - 服務器端PHP-FPM程序 這個程序對Nginx處理PHP很重要
php5-gd - PHP5的GD模塊 GD是一套開源圖像處理庫,通常dz生成縮略圖或者加水印須要他
php5-imagick - PHP5的ImageMagick模塊 DZ支持調用其用以提供比GD跟快以及更高效的圖像處理
php5-imap - PHP5的IMAP模塊 論壇的郵件發送功能可能須要
php5-ldap - PHP5的LDAP模塊 LDAP是一個輕量級目錄服務
php5-mcrypt - PHP5的MCrypt模塊 主要用途是數據加密,好比phpmyadmin就會要求提供此模塊來提供更高的安全性
php5-mysql - PHP5的MySQL模塊 若是想讓你的網站能夠訪問數據庫,此模塊必備
php5-snmp - PHP5的SNMP模塊 SNMP是簡單網絡管理協議的縮寫,主要用於從一些設備以及服務器讀取信息。部分監控軟件可能會用到
php5-sqlite - PHP5的SQLite模塊 SQLite是一個輕量級的數據庫,某些軟件可能須要
php5-xmlrpc - PHP5的XML-RPC
七.安裝php擴展
1.安裝php擴展庫:memcache
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
make && make install
2.安裝php加速:eaccelerator
tar-zxvf eaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator-42067ac
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
3.修改php.ini增長memcache,ea等軟件信息
添加memcache庫的位置:
vim /usr/local/php/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525" extension = "memcache.so" extension = "pdo_mysql.so" extension = "imagick.so"
添加eaccelerator庫的位置信息:
[eaccelerator] zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so" eaccelerator.shm_size="1" eaccelerator.cache_dir="/usr/local/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" eaccelerator.keys = "disk_only" eaccelerator.sessions = "disk_only" eaccelerator.content = "disk_only"
mkdir -p /usr/local/eaccelerator_cache
chmod 0777 /usr/local/eaccelerator_cache
七.配置nginx支持php
1.首先爲php建立配置文件:
shell> cp php.ini-production /usr/local/php/php.ini
shell> mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
shell> ln -s /usr/local/php/bin/php /usr/bin/
2.啓動php-fpm
shell> /usr/local/php/sbin/php-fpm
shell> ps aux | grep php-fpm #查看php-fpm是否成功
root 3412 0.0 0.0 103236 892 pts/3 S+ 13:56 0:00 grep php-fpm
root 29811 0.0 0.5 281608 5280 ? Ss Mar12 0:05 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nobody 29812 0.0 1.5 290576 15832 ? S Mar12 0:00 php-fpm: pool www
nobody 29813 0.0 1.8 293496 19064 ? S Mar12 0:01 php-fpm: pool www
nobody 30839 0.0 1.1 286492 11300 ? S Mar12 0:00 php-fpm: pool www
3.配置nginx,添加支持php
vi /usr/local/nginx/conf/nginx.conf
-------------------------------------------------------------
http {
server {
listen 80;
server_name localhost;
charset uft-8;
access_log logs/host.access.log;
location / {
root /var/www/bbs;
index index.php index.html index.htm;
}
#添加下面的內容,注意$document_root在默認配置文件中沒有
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcg i_params;
include fastcgi.conf;
}
}
}
------------------------------------------------------------
#修改完以後重啓nginx
//支持ssl加密傳輸
--------------------------------------------------------------user apache apache;worker_processes 2;error_log logs/error_nginx.log;pid logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access_nginx.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on;#虛擬主機配置文件# include virtual/www.zijian.com.conf; server {#https加密協議默認端口 listen 443; server_name bbs.zijian.com; charset uft-8; access_log logs/bbs.access.log main;#網站根目錄 root /var/www/bbs; index index.php index.html; location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; }#配置ssl,證書用openssl工具生成,下面的路徑是證書的存放目錄,證書須要用系統自帶的openssl工具生成#訪問這個server時候,用https://xxx.com格式 ssl on; ssl_certificate /usr/local/nginx/conf/ssl/client.pem; ssl_certificate_key /usr/local/nginx/conf/ssl/client.key; }}-------------------------------------------------------------------