LTMP-xache源码安装及nginx高级配置篇

一.安装配置tengine
软件包列表:http://yunpan.cn/cwgyc36ZW7rxq  提取码 1b8f

1.)下载安装
[root@kali ]#wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz #下载源码包
[root@kali ]#yum install pcre-devel openssl-devel -y #安装依赖组件
[root@kali tengine-2.0.1]#groupadd nginx #创建nginx组
[root@kali tengine-2.0.1]#useradd nginx -g nginx  -s /sbin/nologin -M
[root@kali tengine-2.0.1]#mkdir  /var/tmp/nginx/{proxy,fastcgi,uuwsgi,client}  -pv #创建相关目录
[root@kali tengine-2.0.1]#./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx --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 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_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/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi  #检测
[root@kali tengine-2.0.1]#make  #编译
[root@kali tengine-2.0.1]#make install #安装
[root@kali tengine-2.0.1]# /usr/local/nginx/sbin/nginx  #启动nginx

2.)
添加脚本让其开机启动:

[root@kali src]# vim /etc/rc.d/init.d/nginx

#!/bin/sh

# chkconfig: - 55 45

# description:  The nginx daemon is a web service.

# processname: nginx

. /etc/rc.d/init.d/functions

NGINX="/usr/local/nginx/sbin/nginx"

start()

{

echo -n $"Starting nginx: "

daemon $NGINX

echo

}

stop()

{

echo -n $"Shutting down nginx: "

$NGINX -s stop

echo

}

quit()

{

echo -n $"Shutting down nginx: "

$NGINX -s quit

echo

}

reload()

{

echo -n $"reload config:"

$NGINX -s reload

echo

}

[ -f $NGINX ] || exit 1

# See how we were called.

case "$1" in

start)

start

;;

stop)

quit

;;

reload)

reload

;;

restart)

stop

sleep 3

start

;;

*)

echo $"Usage: $0 {start|stop|restart|reload}"

exit 1

esac

exit 0
[root@kali src]# chmod  +x /etc/rc.d/init.d/nginx
[root@kali src]# chkconfig  --add nginx
[root@kali src]# chkconfig  --level 35 nginx on
[root@kali src]# service nginx start

3).在主配置文件中添加:

include vhosts/*.conf

[root@kali conf]# mkdir vhosts

[root@kali vhosts]# vim wp.conf

二.安装mysql

也可参考最新这篇文章安装免费的mariadb:http://www.mrliangqi.com/729.html

1).下载安装

cmake-3.2.3.tar.gz(编译mysql)

http://www.cmake.org/files/v3.2/cmake-3.2.3.tar.gz

说明:cmake指定编译选项的方式不同于make,其实现方式对比如下

./configure 对应的是 cmake .

./configure --help 对应的是 cmake . -LH 或者是 ccmake .

mysql-5.6.13.tar.gz

yum install –y openssl openssl-devel ncurses ncurses-devel 安装依赖文件

[root@mysql ~]# groupadd -r mysql

[root@mysql ~]# useradd -g mysql -r  -s /sbin/nologin mysql

[root@kali /]# chown  -R   mysql.mysql  /data/

[root@kali src]# cd cmake-2.8.7

[root@kali cmake-2.8.7]# ./configure  &&make &&make install

[root@kali mysql-5.6.13]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSAL_DATADIR=/data/

[root@mysql mysql-5.6.12]# rm  -rf CMakeCache.txt

2).为其添加脚本服务

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

[root@kali mysql-5.6.13]# chmod +x   /etc/init.d/mysqld

[root@kali mysql-5.6.13]# chkconfig  --add mysqld

[root@kali mysql-5.6.13]# chkconfig   mysqld on

3).初始化mysql
[root@kali mysql-5.6.13]# /usr/local/mysql/scripts/mysql_install_db    --basedir=/usr/local/mysql/  --datadir=/data/  --user-mysql

4).复制配置文件:

[root@kali mysql-5.6.13]# cp support-files/my-default.cnf

5).启动mysqld

[root@kali mysql-5.6.13]# service  mysqld start

6).mysql命令添加到系统环境变量

[root@kali data]# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

[root@kali data]# .  /etc/profile.d/mysql.sh

[root@kali data]# source  /etc/profile

7).输入mysql的库文件

[root@kali data]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib/

[root@kali data]# ldconfig  -v

8).输入mysql的头文件到系统头文件

[root@kali data]# ln -sv /usr/local/mysql/include/  /usr/include/mysql

`/usr/include/mysql' -> `/usr/local/mysql/include/'

[root@kali data]# cd /usr/include/mysql/

9).修改mysql密码及给用户赋予权限

#mysqladmin  -u root password "3306.com" //给mysql设置密码为3306.com

mysql> Grant all privileges on *.* to 'root'@'%' identified by ‘password’with grant option;(%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名;‘root’则是指要使用的用户名,里面的password需要自己修改成root的密码)
mysql> flush privileges; (运行为句才生效,或者重启MySQL)

三.php-fpm安装

1).下载解压安装

php-5.4.0.tar.bz2

http://cn2.php.net/distributions/php-5.6.10.tar.bz2

yum install -y libxml2-devel bzip2-devel   libcurl-devel   安装依赖库文件

[root@kali src]# tar xf  php-5.4.4.tar.bz2

./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    --with-jd && make  &&make install

报错:error: mcrypt.h not found. Please reinstall libmcrypt.

解决:源码安装即可

libmcrypt-2.5.7.tar.gz

# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz

#tar xf  libmcrypt-2.5.7.tar.gz

#cd libmcrypt-2.5.7.tar.gz

#./configure  && make && make install

2).为php提供配置文件

[root@kali php-5.4.4]# cp php.ini-production /etc/php.ini

为php-fpm提供Sysv Init 脚本,并加入系统服务

[root@kali php-5.4.4]# cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm

[root@kali php-5.4.4]# chmod +x  /etc/rc.d/init.d/php-fpm

[root@kali php-5.4.4]# chkconfig   --add php-fpm

[root@kali php-5.4.4]# chkconfig  php-fpm on

3).为php-fpm提供配置文件

[root@kali php-5.4.4]# cp /usr/local/php/etc/php-fpm.conf.default   /usr/local/php/etc/php-fpm.conf 复制配置文件

[root@kali php-5.4.4]#vim /usr/local/php/etc/php-fpm.conf  编辑配置文件添加以下内容:

pm.max_children = 150

pm.start_servers = 8

pm.min_spare_servers = 5

pm.max_spare_servers = 10

pid = /usr/local/php/var/run/php-fpm.pid

4).启动php-fpm

[root@kali php-5.4.4]# service php-fpm start

5).查看进行或者端口来验证

[root@kali php-5.4.4]# ps -aux |grep php-fpm

[root@kali php-5.4.4]# ss -ntl  9000端口启动为成功

 

四. 整合nginx和php

1).编辑nginx主配置文件,启用一下选项

location ~ \.php$ {

root           html;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

include        fastcgi_params;

}

2).备份配置文件,并编辑其配置文件将内容修改为:

[root@kali conf]# cp fastcgi_params{,.bak}

[root@kali conf]# vim /usr/local/nginx/conf/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;

3).并在所支持的页面主页格式中添加php格式的主页

例如:

location / {

root   html;

index  index.php index.html index.htm;

}

4).重载nginx之后,新建测试文件测试php

[root@kali conf]# /usr/local/nginx/sbin/nginx  -s reload

五.安装xcache为php加速

1).下载并安装

[root@kali src]# tar xf xcache-3.0.1.tar.bz2

[root@kali xcache-3.0.1]# /usr/local/php/bin/phpize

[root@kali xcache-3.0.1]# ./configure   --enable-xcache  --with-php-config=/usr/local/php/bin/php-config  && make && make install

2).安装成功会提示

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

3).编辑php.ini 整合php和xcache

将xcache提供的样例配置导入php.ini

[root@kali xcache-3.0.1]# mkdir /etc/php.d

[root@kali xcache-3.0.1]# cp  xcache.ini  /etc/php.d

[root@kali xcache-3.0.1]# vim /etc/php.d/xcache.ini

zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

4).重新启动即可。

[root@kali xcache-3.0.1]# service php-fpm  start

注意:要在ssl中使用php,需要在php的location中添加:

fastcgi_param  HTTPS  on;

 五.nginx高级配置篇

1).nginx-虚拟主机

在主配置文件的server下面添加。

[root@kali pm]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name  www.stu1.com;

root "/web/www1/";

location / {

root   "/web/www1/";

index index.php index.html index.htm;

}

access_log  logs/nginx/www1.access ;

error_log  logs/nginx/www1.error ;

 

location ~ \.php$ {

root           "/web/www1/";

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

include        fastcgi_params;

}

}

 

server {

listen 80;

server_name  www.stu2.com;

root "/web/www2/";

location / {

root   "/web/www2/";

index index.php index.html index.htm;

}

access_log  logs/nginx/www2.access ;

error_log  logs/nginx/www2.error ;

 

location ~ \.php$ {

root           "/web/www2/";

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

include        fastcgi_params;

}

}

2).基于帐号密码登录ststus页

2.1在主配置文件添加以下参数:

location /status {

stub_status on;

auth_basic "auth page";

auth_basic_user_file /usr/local/nginx/users/.htpasswd;

}

2.2创建目录,生成登录的帐号和密码(test/test)

[root@kali nginx]# mkdir /usr/local/nginx/users/ -pv

[root@kali nginx]# cd /usr/local/nginx/users/

[root@kali users]# htpasswd  -c -m /usr/local/nginx/users/.htpasswd test

New password:

Re-type new password:

Adding password for user test

2.3重载nginx之后输入帐号密码即可登录。

3).基于ip的访问控制

allow 172.16.0.0/16;

deny all;

4).基于https访问虚拟主机

证书要求:国家:ca 州:ha城市:zz组织:linux部门:ops主机名:www.stu2.com邮件:admin@stu2.com

4.1 生成私钥,生成证书签署请求,并获得证书

[root@local ~]# cd /etc/pki/CA/

[root@local CA]# (umask   077;openssl   genrsa -out private/cakey.pem 2048)

[root@local CA]# openssl req -new  -x509 -key private/cakey.pem  -out cacert.pem -days 3655

Country Name (2 letter code) [XX]:cn

State or Province Name (full name) []:ha

Locality Name (eg, city) [Default City]:zz

Organization Name (eg, company) [Default Company Ltd]:linux

Organizational Unit Name (eg, section) []:ops

Common Name (eg, your name or your server's hostname) []:www.stu2.com

Email Address []:admin@stu2.com

[root@kali CA]# touch  index.txt

[root@kali CA]# echo 01 > serial

4.2 nginx申请证书

要让用户使用https访问,需要安装mod_ssl

[root@kali CA]# yum install -y mod_ssl

[root@kali ssl]# (umask 077;openssl  genrsa -out  nginx.key 1024)

[root@kali ssl]# openssl  req -new -key  nginx.key   -out nginx.csr

[root@kali ssl]# openssl  ca -in nginx.csr   -out nginx.crt -days 3655

4.3 配置并启用https,编辑nginx主配置文件添加私钥和证书路径

listen  443;

ssl on;

ssl_certificate      /usr/local/nginx/ssl/nginx.crt;

ssl_certificate_key  /usr/local/nginx/ssl/nginx.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers   on;

===================小扩展======================

添加本地光盘为yum源

1).挂载光盘

[root@kali conf]# mount  /dev/cdrom  /media/

2).修改文件

[root@kali yum.repos.d]# vim  centos.repo

[dvdbase]

name=centos dvd

baseurl=file:///media

gpgcheck=0

enable=1

3).测试

#yum  list

 

 


发表评论

登录 后发表评论.