nginx默认是无法处理动态请求的,一般要配合动态语言或脚本来出来动态请求,比如nginx和php、java、python等来处理用户的动态请求。
比如nginx+php配合使用来解析动态请求流程:
流程说明:nginx通过fastcgi的接口来连接php通过php动态解析后再返回给nginx客户端,最后由nginx交于用户。这个fastcgi是一个C/S模式,nginx是客户端,php是服务器端。
图中的wrapper不起解析作用,而是把需要解析的内容转交给php解析器来解析,这个wrapper类似于python中的装饰器作用。
1、安装php前要安装一些依赖包:
yum install zlib-devel libxml2-devel libjpeg-devel libiconv-devel -y yum install freetype-devel libpng-devel gd-devel curl-devel libxslt-devel -y
这里的libiconv-devel可能安装不成功,这时候就要另外下载编译安装
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar xf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv make && make install
安装libmcrypt-devel,这个模块是动态加载的功能。但redhat官方的yum源是没有这个包的,先要下载一个epel.repo第三方yum源,然后再用yum安装就可以了
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum install libmcrypt-devel -y
安装mhash加密扩展库,因为有了第三方的epel.repo的yum源了,直接安装就行
yum install mhash mhash-devel -y
安装mcrypt加密扩展库
yum install mcrypt -y
2、安装php
切换到tools工具目录,解压php压缩文件,进行预编译参数设置
./configure --prefix=/application/php5.3.27 --with-mysql=/application/mysql --with-iconv-dir=/usr/local/libiconv \ --with-freetype-dir -with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml \ --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \ --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd \ --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip \ --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx \ --with-fpm-group=nginx --enable-ftp
如果是php5.5.26版本的话,安装前的参数设置略有不同
./configure --prefix=/application/php5.5.26 --with-mysql=/application/mysql --with-iconv-dir=/usr/local/libiconv \ --with-freetype-dir -with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml \ --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \ --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd \ --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc \ --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=nginx \ --with-fpm-group=nginx --enable-ftp --with-pdo-mysql=mysqlnd --enable-opcache=no
configure参数--with-mysql指定mysql的安装路径,如果服务器没有安装mysql,可以使用--with-mysql=mysqlnd让php自己生成mysql库就行了
编译前准备:对mysql库做软连接,在php安装包中创建phar.phar文件
ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/ touch ext/phar/phar.phar
如果不做上面的两步操作,php编译的时候即make的时候就会报错,接下来编译和安装
make && make install
生成php解析文件,拷贝php解压包中的php.ini-production到/application/php/lib/下
[root@lnmp01 php-5.3.27]# cp php.ini-production /application/php/lib/php.ini
生成php进程配置文件
[root@lnmp01 php-5.3.27]# cp /application/php/etc/php-fpm.conf.default /application/php/etc/php-fpm.conf
对生成的php-fpm.conf做如些修改,php-fpm.conf和php-fpm.conf.default不同如下图差异:
3、启动php服务
[root@lnmp01 etc]# /application/php/sbin/php-fpm
检查php进程是否启动
[root@lnmp01 etc]# netstat -lntup|grep php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19664/php-fpm
[root@lnmp01 etc]# ps -ef|grep php-fpm root 19664 1 0 21:45 ? 00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf) nginx 19665 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19666 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19667 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19668 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19669 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19670 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19671 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19672 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19673 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19674 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19675 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19676 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19677 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19678 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19679 19664 0 21:45 ? 00:00:00 php-fpm: pool www nginx 19680 19664 0 21:45 ? 00:00:00 php-fpm: pool www root 19684 13879 0 21:46 pts/0 00:00:00 grep php-fpm
4、配合nginx服务提供服务
比如对blog.goser.com应用php功能,修改blog.conf配置文件
[root@lnmp01 nginx]# cat conf/extra/blog.conf
server {
listen 80;
server_name blog.goser.org;
location / {
root html/blog;
index index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/blog_access.log main;
}
在html/blog/下创建一个php文件
[root@lnmp01 nginx]# cat html/blog/phpinfo.php <?php phpinfo(); ?>
然后重启nginx服务,用浏览器打开这个phpinfo.php文件测试是否能够访问:blog.goser.com/phpinfo.php
5、测试nginx、php、mysql整合是否正常
在html/blog下创建一个测试mysql数据库的php文件
[root@lnmp01 nginx]# vim html/blog/test_mysql.php
<?php
$link_id=mysql_connect(\'localhost\',\'root\',\'123456\') or mysql_error();
if($link_id){
echo "mysql successful by goser!";
}else{
echo mysql_error();
}
?>
打开浏览器访问blog.goser.com/test_mysql.php,页面显示mysql successful by goser! 说明连接数据库正常。
到此lnmp架构搭建完成!!!
6、网站目录权限优化,防木马攻击
对网站的上传目录设置权限为服务账户的755,对其他目录或文件设置拥有者为root,目录755 ,文件644
比如对blog站点的权限设置为:
[root@lnmp01 html]# chown -R root.root blog/ [root@lnmp01 html]# find ./blog/ -type f|xargs chmod 644 [root@lnmp01 html]# find ./blog/ -type d|xargs chmod 755 [root@lnmp01 html]# chown -R nginx.nginx ./blog/uploads/
7、部署一个wordpress博客站点
切换到tools工具目录,下载wordpress:wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.tar.gz 下载好解压
将解压好的wordpress拷贝到/application/nginx/html/blog/目录下
[root@lnmp01 tools]# cp -a wordpress/* /application/nginx/html/blog/
创建数据库wordpress和管理这个数据库的账户wordpress
mysql> create database wordpress; mysql> grant all on wordpress.* to wordpress@\'localhost\' identified by \'123456\'; mysql> flush privileges;
临时对blog/目录授予nginx属主权限,这样在安装wordpress的时候不会因为没有写的权限而无法自动创建wp-config.php的连接数据库的配置文件。
[root@lnmp01 html]# chown -R nginx.nginx /application/nginx/html/blog/
最后重启nginx是配置生效,打开浏览器访问站点网址进行wordpress安装。。。。
安装好wordpress后对blog/目录的权限重新规划,尽可能的防治木马入侵:
[root@lnmp01 html]# chown -R root.root /application/nginx/html/blog/ [root@lnmp01 html]# find /application/nginx/html/blog/ -type f|xargs chmod 644 [root@lnmp01 html]# find /application/nginx/html/blog/ -type d|xargs chmod 755 [root@lnmp01 html]# chown -R nginx.nginx /application/nginx/html/blog/wp-content/uploads/
8、将lnmp中的数据库分离成一个独立的数据
1、导出wordpress数据库,为接下来独立数据库服务器的数据导入
[root@lnmp01 tools]# mysqldump -uroot -p123456 wordpress -B|gzip>bak.sql.gz
2、拷贝这个数据库备份文件到独立数据库服务器上
[root@lnmp01 tools]# scp bak.sql.gz root@192.168.182.144:/tmp
3、在独立数据库服务器上将这个拷贝好的备份数据库导入到独立数据库服务器中
[root@mysql-server tmp]# gzip -d bak.sql.gz [root@mysql-server tmp]# mysql -uroot -p123456 <bak.sql
查看数据库是否导入了
[root@mysql-server tmp]# mysql -uroot -p123456 -e "show databases like \'wordpress\'" [root@mysql-server tmp]# mysql -uroot -p123456 -e "use wordpress;show tables;"
为导入的wordpress数据库创建管理账号
mysql> grant all on wordpress.* to wordpress@\'192.168.182.%\' identified by \'123456\';
4、在lnmp服务器上将数据库连接配置文件wp-config.php文件修改如下:
[root@lnmp01 tools]# vim /application/nginx/html/blog/wp-config.php /** MySQL主机 */ define(\'DB_HOST\', \'192.168.182.144\');
5、关掉lnmp服务器上的mysqld服务
/etc/init.d/mysqld stop chkconfig mysqld off
6、浏览器打开wordpress博客,添加一篇文章(标题为1111...内容为222....),检查内容是否添加到独立的数据库服务器上了
mysql> select * from goser_posts\G
*************************** 11. row ***************************
ID: 11
post_author: 1
post_date: 2017-09-21 13:18:18
post_date_gmt: 2017-09-21 05:18:18
post_content: 22222222222222222222222222222222222
post_title: 11111111111111111111111111111111111111111111111
post_excerpt:
post_status: inherit
comment_status: closed
ping_status: closed
post_password:
post_name: 10-revision-v1
to_ping:
pinged:
post_modified: 2017-09-21 13:18:18
post_modified_gmt: 2017-09-21 05:18:18
到此数据库分离成独立服务器配置成功!!!
9、将文件的上传目录同步到nfs服务器上
在nfs服务器上创建一个和web服务的用户的id相同的用户。
比如web服务上的nginx的id为503,那么在nfs服务上的nginx用户的id也为503
[root@nfs-server ~]# useradd -u 503 nginx
创建站点服务的目录,并对这个目录设置权限
[root@nfs-server ~]# mkdir /data/nfs-blog [root@nfs-server ~]# chown -R nginx.nginx /data/
nfs挂载配置
[root@nfs-server ~]# vim /etc/exports /data 192.168.182.0/24(rw,sync,all_squash,anonuid=503,anongid=503) [root@nfs-server ~]# exportfs -rv
这样就可以在web段对上传的目录做nfs挂载
[root@lnmp01 uploads]# /bin/mount -t nfs -o noatime,nodiratime 192.168.182.141:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/
最后将此挂载放到/etc/rc.local中,下次重启后自动挂载
关于伪静态的设置
在nginx的blog.conf中的server块中添加
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
然后wordpress博客后台设置中自定义为:/archives/%post_id%.html
这样再添加文章,文章的url就是一种伪静态的形式。。。。