Linux搭建LNMP环境
1、安装nginx并测试正常访问网站
2、安装mysql
1)确认rpm mysql删除 # rpm -qa | grep mysql 2)解压缩Mysql并配置安装参数 # ./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312 3)编译并安装 # make && make install 4)生成mysql配置文件及启动服务 # cp support-files/my-medium.cnf /etc/my.cnf # cp support-files/mysql.server /etc/init.d/mysqld # chmod a+x /etc/init.d/mysqld # chkconfig --add mysqld # chkconfig --list mysqld 5)初始化mysql数据库 # useradd -M -s /sbin/nologin mysql # /usr/local/mysql/bin/mysql_install_db --user=mysql # chown -R root:mysql /usr/local/mysql/ # chown -R mysql /usr/local/mysql/var/ 6)建立mysql相应软链接 # ln -s /usr/local/mysql/bin/* /usr/local/bin/ # ln -s /usr/local/mysql/lib/mysql/* /usr/lib # ln -s /usr/local/mysql/include/mysql/* /usr/include/ 7)启动mysqld服务,并验证 # /etc/init.d/mysqld start Starting MySQL. [确定] # netstat -antp | grep mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21727/mysqld 8)为mysql用户添加密码 # mysqladmin -u root -p password "www.1.com"
3、安装php
1)确认rpm php软件卸载 # rpm -qa | grep php 2)解压缩并配置安装参数 # ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php --enable-mbstring --enable-fpm 错误1: configure: error: xml2-config not found. Please check your libxml2 installation. 解决办法: yum install libxlm2-devel 错误2: configure: error: libpng.(a|so) not found. 解决办法: yum install -y libpng-devel 3)编译并安装 #make && make install 4)生成php.ini配置文件并创建相应的软链接 # cp php.ini-development /usr/local/php/php.ini # ln -s /usr/local/php/bin/* /usr/local/bin/ # ln -s /usr/local/php/sbin/* /usr/local/sbin/ 5)配置php使其支持ZendGuardLoader # cp ZendGuardLoader.so /usr/local/php/lib/php/ # vim /usr/local/php/php.ini zend_extension=/usr/local/php/lib/php/ZendGuardLoader.so zend_loader.enable=1
4、配置nginx使其支持php
nginx支持php有两种方式:其一,充当中介,将访问php页面的请求转交到其他的LAMP服务器进行解析;其二,通过使用php的fpm模块调用本机的php环境
这里采用第二种方式
1)生成php-fpm.conf配置文件,并启用php-fpm进程
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid //确认pid文件位置 user = nginx group = nginx pm.start_servers = 20 //启动时开启的进程数 pm.min_spare_servers = 5 //最少空闲进程数 pm.max_spare_servers = 35 //最多空闲进程数 # /usr/local/sbin/php-fpm # netstat -antp | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 25063/php-fpm
2)修改nginx服务启动脚本,使php-fpm随nginx自动启动关闭;在原有脚本基础上添加蓝色字体内容
# vim /etc/init.d/nginx
PROG_FPM="/usr/local/sbin/php-fpm" PIDF_FPM="/usr/local/php/var/run/php-fpm.pid" start) $PROG $PROG_FPM ;; stop) kill -s QUIT $(cat $PIDF) kill -s QUIT $(cat $PIDF_FPM) ;;
3)验证nginx和php-fpm共同启动
# /etc/init.d/nginx start # netstat -atnp | grep :9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 25152/php-fpm # netstat -atnp | grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25149/nginx
4)修改nginx配置文件,使其支持php
# vim /usr/local/nginx/conf/nginx.conf location ~ .php$ { root /usr/local/nginx/html/; //指定php页面根目录 fastcgi_pass 127.0.0.1:9000; //指定php-fpm的端口 fastcgi_index index.php; //指定Php首页名称 include fastcgi.conf; }
5)在/usr/local/nginx/html下创建test.php文件测试LNMP是否正常工作
# vim /usr/local/nginx/html/test.php <?php $link=mysql_connect('localhost','root','www.1.com'); if($link) echo "连接成功"; mysql_close(); ?>
6)重启nginx服务,并测试
www.bj.com/test.php