现在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,
- [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
最后添加环境变量到系统
- vim /etc/profile
- PATH=$PATH:/usr/local/mysql/bin/
- export PATH
然后安装nginx,安装nginx比较简单了,直接运行
- [root@shiyan1 nginx-1.2.6]#./configure --prefix=/usr/local/nginx
- [root@shiyan1 nginx-1.2.6]# make && make install
nginx完了,就装PHP,这个是关键
- [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 install
- [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!
