Nginx(三)构建LEMP平台,发布WEB站点

1.编译安装Nginx(略)

2.安装MySQL数据库

(1)编译安装mysql

[root@server ~]#cd /usr/src/

[root@server src]#tar zxf mysql-5.1.55.tar.gz

[root@server src]#cd mysql-5.1.55/

[root@server mysql-5.1.55]#./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312

[root@server mysql-5.1.55]#make && make install

(2)优化调整

[root@server mysql-5.1.55]#cp support-files/my-medium.cnf /etc/my.cnf

[root@server mysql-5.1.55]#cp support-files/mysql.server /etc/init.d/mysqld

[root@server mysql-5.1.55]#chmod a+x /etc/rc.d/init.d/mysqld

[root@server mysql-5.1.55]#chkconfig --add mysqld

[root@server mysql-5.1.55]#ln -s /usr/local/mysql/bin/* /usr/local/bin/

[root@server mysql-5.1.55]#ln -s /usr/local/mysql/lib/mysql/* /usr/lib/

[root@server mysql-5.1.55]#ln -s /usr/local/mysql/include/mysql/* /usr/include/

(3)初始化数据库

[root@server ~]#useradd -M -u 27 -s /sbin/nologin mysql

[root@server ~]#cd /usr/local/mysql/bin/

[root@server bin]#./mysql_install_db --user=mysql

[root@server bin]#chown -R root:mysql /usr/local/mysql/

[root@server bin]#chown -R mysql /usr/local/mysql/var/

(4)启动mysql服务

[root@server bin]#service mysqld start

[root@server bin]#mysqladmin -u root password 'pwd123'    //为root设置密码

3.安装PHP解析环境

(1)编译安装PHP

[root@server src]#tar zxf php-5.3.6.tar.gz

[root@server src]#cd php-5.3.6/

[root@server php-5.3.6]#./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql/ --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm

[root@server php-5.3.6]#make && make install

(2)安装后调整

[root@server php-5.3.6]#cp php.ini-development /usr/local/php5/php.ini

[root@server php-5.3.6]#ln -s /usr/local/php5/bin/* /usr/local/bin/

[root@server php-5.3.6]#ln -s /usr/local/php5/sbin/* /usr/local/sbin/

(3)安装ZendGuardLoader(提高php解析效率)

[root@server src]#tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz

[root@server src]#cd ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/

[root@server php-5.3.x]#cp ZendGuardLoader.so /usr/local/php5/lib/php/

[root@server php-5.3.x]#vim /usr/local/php5/php.ini

zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so

zend_loader.enable=1

short_open_tag=on            //添加到配置文件底部

4.配置Nginx支持PHP环境

(1)启用php-fpm进程

[root@server ~]#cd /usr/local/php5/etc/

[root@server etc]#cp php-fpm.conf.default php-fpm.conf

[root@server etc]#vim php-fpm.conf

pid = run/php-fpm.pid                //确认pid文件位置

user = nginx                         //运行用户

group = nginx                        //运行组

pm.start_servers = 20                //启动时开启的进程数

pm.min_spare_servers = 5             //最少空闲进程数

pm.man_spare_servers = 35            //最多空闲进程数

[root@server etc]#/usr/local/sbin/php-fpm

[root@server etc]#netstat -anpt | grep php-fpm       //端口默认9000

(2)修改脚本使Nginx和php-fpm同步

[root@server ~]#vim /etc/init.d/nginx

PROG_FPM="/usr/local/sbin/php-fpm"

PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"

case "$1" in

 start)

  $PROG -t &> /dev/null

  if [ $? -eq 0 ] ; then

   $PROG

$PROG_FPM

  else

   $PROG -t

  fi

   ;;

 stop)

   kill -s QUIT $(cat $PIDF)

kill -s QUIT $(cat $PIDF_FPM)

   ;;

(3)配置Nginx支持PHP解析

第一种方法:

server {

   ... ...

   location ~ \.php$ {                       //访问.php页面的配置段

proxy_pass http://apache:80;       //转交给其他服务器

   }    

}

第二种方法:

server {

   ... ...

   location ~ \.php$ {                      

 root /var/www/xxxx;                //PHP网页文档根目录

       fastcgi_pass 127.0.0.1:9000;      //php-fpm监听地址

       fastcgi_index index.php;          //php首页文件

       include fastcgi.conf;             //包括fastcgi.conf样本配置

   }

}

[root@server ~]#service nginx restart

(4)PHP页面访问测试

[root@server ~]#vim /var/www/xxxx/test.php

<?php

$link=mysql_connect('localhost','root','pwd123');

if($link) echo "Congratullations!!!";

mysql_close();

?>

http://server/test.php

5.在LEMP平台中部署web应用

(1)下载并部署程序代码

[root@server src]#tar ... ...

[root@server src]#mv .../wwwroot/ /var/www/xxxx/name

[root@server src]#cd /var/www/xxxx/name/

[root@server name]#chown -R nginx:nginx admincp/ data/ templates/ upload/

(2)创建数据库

建议设置专用数据库及授权用户以降低风险。

[root@server ~]#mysql -u root -p

mysql>create database xxxxdb;

mysql>grant all on xxxxdb.* to runxxxx@localhost identified by 'password';

(4)安装web应用

访问http://server/xxxx/install/index.php

完成安装以后,将install/目录转移,以避免重复安装,降低安全风险。例如:

[root@server name]#mv install install.lock

[root@server name]#chmod 600 install.lock/



DONE!


你可能感兴趣的:(数据库,mysql)