Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强,它的主要功用中还有一项是作为缓存服务器,在某些场景下,如后端的服务器是数据库服务器时,Nginx与其他就需要配合其他专门用于缓存数据库中数据的软件结合起来可以更好的完成缓存数据的功能,比如memcached,此时的Nginx作为代理服务器使用。
实验:完成LNNMP平台的搭建模拟
实验用主机:
172.16.103.1 (安装Nginx、php、MariaDB)
172.16.103.2 (安装memcached,用于缓存php服务器的响应给Nginx服务器的结果)
简要说明:172.16.103.1主机上安装的nginx作为反向代理使用,也作为web服务器使用,只不过在处理php等动态页面时交给了php应用程序来处理,php程序是使用fastcgi的方式工作的,监听在TCP的9000端口,php是使用编译的方式安装的,安装时都运行在了同一台主机上,包括MariaDB。
实验过程:
一、安装MariaDB
由于在编译php时对数据库有依赖关系,所以先安装好数据库。步骤如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# useradd -r mysql
# mkdir /mydata/data
# chown -R mysql.mysql /mydata/data
# tar xf mariadb-5.5.39-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.39-linux-x86_64/ mysql
# cd mysql
# chown -R root.mysql ./*
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf 添加如下几行内容:
datadir = /mydata/data
innodb_file_per_table = ON
二、编译安装php(版本是php-5.4.31)
编译前解决依赖关系:
1
2
# yum groupinstall -y "Development tools" "Server Platform Development"
# yum install mhash-devel libcurl-devel bzip2-devel gd-devel libxml2-devel
执行编译安装的过程:
1
2
3
4
# tar xf php-5.4.31.tar.bz2
# cd php-5.4.31
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
# make && make install
为php提供配置文件,服务启动脚本及php-fpm的配置文件:
1
2
3
4
5
6
7
8
9
# cp php.ini-production /etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# vim php-fpm.conf #在php-fpm的配置文件中需要修改的内容为,主要是需要设定这两个文件的具体路径,配置文件中的默认设置与实际安装完成后的文件所在的位置不一致,所以需要改变。
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
为php程序安装memcached的扩展模块,该功能模块使用的是memcache-2.2.7.tgz的版本,以实现缓存数据的存储。
1
2
3
4
5
6
7
# tar xf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php/bin/phpize #在编译memcache之前使用phpize获取当前系统信息,以便后续完成编译工作
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
# vim /etc/php.ini #将编译安装生成的php扩展模块memcache.so的路径写入php配置文件中
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
三、编译安装Nginx
1
2
3
4
Nginx的安装依赖于pcre-devel包,安装前需要先安装这个rpm包,以保证编译顺利完成。
# yum -y install pcre-devel
# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
# make && make install
为nginx服务提供脚本文件,方便启动停止服务
1
2
3
4
# vim /etc/rc.d/init.d/nginx
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# service nginx start #尝试启动服务
由于安装的php应用程序是运行于fastcgi模式,所以需要编辑Nginx的配置使得其可以向php服务器传递必要的参数,以保证请求的数据可以正常的显示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# vim /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# vim /etc/nginx/nginx.conf #编辑配置文件,开启fastcgi功能,以完成当用户请求动态页面内容时Nginx服务器可以向后端的php服务器转发用户的请求:
在配置文件中需要添加的内容如下说明,配置文件中server段包括全局段其他部分使用的是默认配置,未列出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80; #指定默认监听的端口
server_name www.a.com ; #指定服务器名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /www/a.com; #定义站点的根目录
index index.php index.html index.htm; #指定支持的默认首页类型,注意需要添加上index.php
}
location ~ .php$ { #这部分内容默认是注销的,需要开启,第一行是使用正则表达式匹配用户请求的url以.php结尾时,将请求转发给后端的php服务器来响应
root /www/a.com;
fastcgi_pass 127.0.0.1:9000; #安装的php应用程序也在本机上,所以转发时使用的地址是127.0.0.1:9000,其中php程序监听的端口是9000
fastcgi_index index.php; #设定支持的首页类型
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #用于指定向后端php服务器发送数据请求时传递的fastcgi参数及文件路径。
include fastcgi_params;
}
}
四、在虚拟机172.16.103.2上安装memcache程序并启动。
1
2
# yum install -y memcached
# service memcached start
五、测试缓存数据的效果。
前端用户的请求发往nginx如果是请求动态内容时才会发往后端的php服务器,所以在站点的根目录下创建一个.php结尾的测试文件memcache,添加如下内容为:
1
2
3
4
5
6
7
8
9
10
$mem = new Memcache;
$mem->connect("172.16.103.2",11211) or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."
";
$mem->set('hellokey','Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)
";
$get_result = $mem->get('hellokey');
echo "$get_result is from memcached server.";
?>
这是一个测试连接memcached的php程序,连接成功时,会在memcached中插入hellokey这个键值及其数据。在浏览器内输入www.a.com/memcache.php
另外可以直接在安装memcached主机端使用命令来查看memcached的运行状态及统计的数据信息:
1
2
3
[iyunv@node2 ~]# memcached-tool 127.0.0.1
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
1 96B 1240293s 1 1 no 0 0 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[iyunv@node2 ~]# memcached-tool 127.0.0.1 stats
#127.0.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 85
bytes_read 1233
bytes_written 1870
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 21
cmd_set 20
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 1
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evictions 0
get_hits 21
get_misses 0
incr_hits 0
incr_misses 0
limit_maxbytes 67108864
listen_disabled_num 0
pid 3787
pointer_size 64
rusage_system 0.391940
rusage_user 0.239963
threads 4
time 1411664449
total_connections 33
total_items 20
uptime 1240664
version 1.4.4
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com