一.安装配置tengine
软件包列表:http://yunpan.cn/cwgyc36ZW7rxq 提取码 1b8f
1.)下载安装
[[email protected] ]#wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz #下载源码包
[[email protected] ]#yum install pcre-devel openssl-devel -y #安装依赖组件
[[email protected] tengine-2.0.1]#groupadd nginx #创建nginx组
[[email protected] tengine-2.0.1]#useradd nginx -g nginx -s /sbin/nologin -M
[[email protected] tengine-2.0.1]#mkdir /var/tmp/nginx/{proxy,fastcgi,uuwsgi,client} -pv #创建相关目录
[[email protected] 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 #检测
[[email protected] tengine-2.0.1]#make #编译
[[email protected] tengine-2.0.1]#make install #安装
[[email protected] tengine-2.0.1]# /usr/local/nginx/sbin/nginx #启动nginx
2.)添加脚本让其开机启动:
[[email protected] 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
[[email protected] src]# chmod +x /etc/rc.d/init.d/nginx
[[email protected] src]# chkconfig --add nginx
[[email protected] src]# chkconfig --level 35 nginx on
[[email protected] src]# service nginx start
3).在主配置文件中添加:
include vhosts/*.conf
[[email protected] conf]# mkdir vhosts
[[email protected] 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 安装依赖文件
[[email protected] ~]# groupadd -r mysql
[[email protected] ~]# useradd -g mysql -r -s /sbin/nologin mysql
[[email protected] /]# chown -R mysql.mysql /data/
[[email protected] src]# cd cmake-2.8.7
[[email protected] cmake-2.8.7]# ./configure &&make &&make install
[[email protected] mysql-5.6.13]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSAL_DATADIR=/data/
[[email protected] mysql-5.6.12]# rm -rf CMakeCache.txt
2).为其添加脚本服务
[[email protected] mysql-5.6.13]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.13]# chmod +x /etc/init.d/mysqld
[[email protected] mysql-5.6.13]# chkconfig --add mysqld
[[email protected] mysql-5.6.13]# chkconfig mysqld on
3).初始化mysql
[[email protected] mysql-5.6.13]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/ --user-mysql
4).复制配置文件:
[[email protected] mysql-5.6.13]# cp support-files/my-default.cnf
5).启动mysqld
[[email protected] mysql-5.6.13]# service mysqld start
6).mysql命令添加到系统环境变量
[[email protected] data]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[[email protected] data]# . /etc/profile.d/mysql.sh
[[email protected] data]# source /etc/profile
7).输入mysql的库文件
[[email protected] data]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[[email protected] data]# ldconfig -v
8).输入mysql的头文件到系统头文件
[[email protected] data]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include/'
[[email protected] 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).下载解压安装
http://cn2.php.net/distributions/php-5.6.10.tar.bz2
yum install -y libxml2-devel bzip2-devel libcurl-devel 安装依赖库文件
[[email protected] 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提供配置文件
[[email protected] php-5.4.4]# cp php.ini-production /etc/php.ini
为php-fpm提供Sysv Init 脚本,并加入系统服务
[[email protected] php-5.4.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[[email protected] php-5.4.4]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-5.4.4]# chkconfig --add php-fpm
[[email protected] php-5.4.4]# chkconfig php-fpm on
3).为php-fpm提供配置文件
[[email protected] php-5.4.4]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 复制配置文件
[[email protected] 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
[[email protected] php-5.4.4]# service php-fpm start
5).查看进行或者端口来验证
[[email protected] php-5.4.4]# ps -aux |grep php-fpm
[[email protected] 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).备份配置文件,并编辑其配置文件将内容修改为:
[[email protected] conf]# cp fastcgi_params{,.bak}
[[email protected] 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。
[[email protected] conf]# /usr/local/nginx/sbin/nginx -s reload
五.安装xcache为php加速
1).下载并安装
[[email protected] src]# tar xf xcache-3.0.1.tar.bz2
[[email protected] xcache-3.0.1]# /usr/local/php/bin/phpize
[[email protected] 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
[[email protected] xcache-3.0.1]# mkdir /etc/php.d
[[email protected] xcache-3.0.1]# cp xcache.ini /etc/php.d
[[email protected] 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).重新启动即可。
[[email protected] xcache-3.0.1]# service php-fpm start
注意:要在ssl中使用php,需要在php的location中添加:
fastcgi_param HTTPS on;
五.nginx高级配置篇
1).nginx-虚拟主机
在主配置文件的server下面添加。
[[email protected] 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)
[[email protected] nginx]# mkdir /usr/local/nginx/users/ -pv
[[email protected] nginx]# cd /usr/local/nginx/users/
[[email protected] 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邮件:[email protected]
4.1 生成私钥,生成证书签署请求,并获得证书
[[email protected] ~]# cd /etc/pki/CA/
[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
[[email protected] 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 []:[email protected]
[[email protected] CA]# touch index.txt
[[email protected] CA]# echo 01 > serial
4.2 nginx申请证书
要让用户使用https访问,需要安装mod_ssl
[[email protected] CA]# yum install -y mod_ssl
[[email protected] ssl]# (umask 077;openssl genrsa -out nginx.key 1024)
[[email protected] ssl]# openssl req -new -key nginx.key -out nginx.csr
[[email protected] 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).挂载光盘
[[email protected] conf]# mount /dev/cdrom /media/
2).修改文件
[[email protected] yum.repos.d]# vim centos.repo
[dvdbase]
name=centos dvd
baseurl=file:///media
gpgcheck=0
enable=1
3).测试
#yum list