如今nginx運用愈來愈普遍。這是由於NGINX在某些方面優於apache。而且NGINX在web服務當中佔有必定的比例,咱們有必要了解和學習。下面步入正題,看LNMP架構怎麼搭建。
首先,分析下LAMP架構的安裝。在LAMP架構中mysql,apache是能夠單獨安裝不須要關聯其餘兩個,而PHP即要關聯MYSQL,又要關聯apache,因此,裝PHP是關鍵。那麼,在LNMP架構中,也同樣。關鍵的地方是讓NGINX來支持PHP。那怎麼讓NGINX來支持PHP呢?這就用到了,PHP中的fastcgi跟fpm這兩個東東,在正常狀況下Nginx和PHP他倆之間是一點感受沒有的。在以前,不少朋友都搭建過Apache+PHP,Apache+PHP編譯後生成的是模塊文件,而Nginx+PHP須要PHP生成可執行文件才能夠,因此要利用fastcgi技術來實現Nginx與PHP的整合,這個只要咱們安裝是啓用FastCGI便可。這次咱們安裝PHP不只使用了FastCGI,並且還使用了PHP-FPM這麼一個東東,PHP-FPM說白了是一個管理FastCGI的一個管理器。
好了,如今明白了。那麼,就動手吧。首先,安裝MYSQL和NGINX,php
[root@localhost mysql-5.5.22]# useradd mysql -s /sbin/nologin [root@localhost mysql-5.5.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 [root@localhost mysql-5.5.22]# gmake && make install [root@localhost mysql-5.5.22]# chown mysql. -R /usr/local/mysql [root@localhost mysql-5.5.22]# cd support-files/ [root@localhost support-files]# cp my-huge.cnf /etc/my.cnf [root@localhost mysql-5.5.22]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ [root@localhost mysql-5.5.22]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld [root@localhost mysql-5.5.22]# chkconfig --add mysqld [root@localhost mysql-5.5.22]# service mysqld start
最後添加環境變量到系統html
vim /etc/profile PATH=$PATH:/usr/local/mysql/bin/ export PATH
而後安裝nginx,安裝nginx比較簡單了,直接運行mysql
[root@shiyan1 nginx-1.2.6]#./configure --prefix=/usr/local/nginx [root@shiyan1 nginx-1.2.6]# make && make install
nginx完了,就裝PHP,這個是關鍵nginx
[root@localhost libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt [root@localhost libmcrypt-2.5.7]# make && make install [root@localhost php-5.3.10]# ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-curl --with-libxml-dir --with-config-file-path=/usr/local/php5/etc --enable-ftp --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-zip --with-mcrypt=/usr/local/libmcrypt --with-gd --enable-soap --enable-fpm --enable-fastcgi
運行完會提示,找不到參數enable-fastcgi.這個不用理會。直接make && make installweb
[root@localhost php-5.3.10]# cp php.ini-production /usr/local/php5/etc/php.ini [root@localhost php-5.3.10]# cd /usr/local/php5/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# vim php-fpm.conf [global] ; Pid file ; Note: the default prefix is /usr/local/php5/var ; Default Value: none pid = run/php-fpm.pid ; Error log file ; If it's set to "syslog", log is sent to syslogd instead of being written ; in a local file. ; Note: the default prefix is /usr/local/php5/var ; Default Value: log/php-fpm.log ;error_log = log/php-fpm.log [root@localhost etc]# cd /usr/local/src/php-5.3.10/sapi/fpm/ [root@localhost fpm]# cp init.d.php /etc/rc.d/init.d/php-fpm [root@localhost fpm]# service php-fpm start [root@localhost fpm]# cd /usr/local/nginx/conf/nginx.conf # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 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; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one [root@localhost fpm]# /usr/local/nginx/sbin/nginx
把fpm跟nginx配置文件修改好啓動,lnmp就完成了。值得注意的是:php-fpm.conf這個配置文件修改的時候,只需修改一處就是,把pid = run/php-fpm.pid這個前面的分號去掉就能夠了,而nginx配置文件把支持PHP打開就OK!sql