CentOS6.8搭建LNMP

来源:互联网 发布:免费虚拟专用网络 编辑:程序博客网 时间:2024/06/01 18:43

CentOS-6.8 安装 Nginx

安装

安装Nginx需要完成以下依赖的安装1.gcc 安装:yum install gcc-c++2.PCRE pcre-devel 安装:yum install -y pcre pcre-devel3.zlib 安装: yum install -y zlib zlib-devel4.OpenSSL 安装:yum install -y openssl openssl-devel

安装Nginx

1.下载Nginx:wget https://nginx.org/download/nginx-1.6.2.tar.gz2. 解压:tar -zxvf nginx-1.6.2.tar.gz3. 配置安装首先添加用户nginx,实现以之运行nginx服务进程:# groupadd -r nginx# useradd -r -g nginx nginx接着开始编译和安装:# ./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-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说明:(1)、Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。(2)、如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。(3)、为nginx提供SysV init脚本:4. 查找安装路径:whereis nginx

创建启动脚本:

#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig:   - 85 15 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \#               proxy and IMAP/POP3 proxy server# processname: nginx# config:      /etc/nginx/nginx.conf# config:      /etc/sysconfig/nginx# pidfile:     /var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {   # make required directories   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`   options=`$nginx -V 2>&1 | grep 'configure arguments:'`   for opt in $options; do       if [ `echo $opt | grep '.*-temp-path'` ]; then           value=`echo $opt | cut -d "=" -f 2`           if [ ! -d "$value" ]; then               # echo "creating" $value               mkdir -p $value && chown -R $user $value           fi       fi   done}start() {    [ -x $nginx ] || exit 5    [ -f $NGINX_CONF_FILE ] || exit 6    make_dirs    echo -n $"Starting $prog: "    daemon $nginx -c $NGINX_CONF_FILE    retval=$?    echo    [ $retval -eq 0 ] && touch $lockfile    return $retval}stop() {    echo -n $"Stopping $prog: "    killproc $prog -QUIT    retval=$?    echo    [ $retval -eq 0 ] && rm -f $lockfile    return $retval}restart() {    configtest || return $?    stop    sleep 1    start}reload() {    configtest || return $?    echo -n $"Reloading $prog: "    killproc $nginx -HUP    RETVAL=$?    echo}force_reload() {    restart}configtest() {  $nginx -t -c $NGINX_CONF_FILE}rh_status() {    status $prog}rh_status_q() {    rh_status >/dev/null 2>&1}case "$1" in    start)        rh_status_q && exit 0        $1        ;;    stop)        rh_status_q || exit 0        $1        ;;    restart|configtest)        $1        ;;    reload)        rh_status_q || exit 7        $1        ;;    force-reload)        force_reload        ;;    status)        rh_status        ;;    condrestart|try-restart)        rh_status_q || exit 0            ;;    *)        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"        exit 2esac

改变nginx脚本权限
chmod 755 /etc/init.d/nginx
添加进service管理服务并设置开机启动
chkconfig –add nginx
service nginx reload
chkconfig nginx on
关闭防火墙
service iptables stop
服务启动测试
service nginx start

启动时出现错误:
这里写图片描述
运行sudo fuser -k 80/tcp 解决
浏览器输入http://192.168.15.132
这里写图片描述

二,配置

1、 访问状态统计及虚拟主机应用

(1)、 nginx 的访问状态统计

vim /etc/nginx/nginx.conf
修改如下:

#访问状态连接的配置文件        location /status {            stub_status   on;            access_log    off;        }

修改完配置文件,要重载配置文件

service nginx reload

然后访问网站,看到如下内容表示成功。
这里写图片描述

注释: Active connections :表示当前的活动连接数( 1 )

server accepts handled requests :表示已经处理的连接信息

三个数字分别代表: 已处理的连接数( 1 )

成功的 TCP 握手次数( 1 )

已处理的请求数( 1 )
1、 搭建一个基于域名的虚拟主机
例如:在 nginx 服务器上搭建两个网站, www.google.com 和
准备网站目录和网页
这里写图片描述
为了测试在本地搭建DNS
IP地址:192.168.15.131
测试域名:www.google.com

1.安装bindyum -y install bind bind-libs bind-utils bind-chroot //安装bind相关软件2.编辑主要配置文件:/etc/named.conf

这里写图片描述
cd /var/named
vim google.com.zone
这里写图片描述
vim 192.168.15.zone
这里写图片描述
3.修改dns指向本机
vim /etc/resolv.conf
这里写图片描述
缓存dns搭建完成
编辑nginx配置文件
vim /etc/nginx/nginx.conf
这里写图片描述
将dns指向192.168.15.131
vim /etc/resolv.conf
这里写图片描述
访问网站
这里写图片描述

编译安装MySQL-5.6.16

一、安装cmake
跨平台编译器

获取CMake源码包wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz# tar zvxf cmake-2.8.8.tar.gz# cd cmake-2.8.8# ./configure && gmake && gmake install

二、编译安装

tar zvxf mysql-5.6.16.tar.gz -C /usr/localcd /usr/localln -sv mysql-5.6.16 mysqlgroupadd -r -g 306 mysqluseradd -g 306 -r -u 306 mysqlid mysqlchown -R mysql:mysql /usr/local/mysql/*fdisk /dev/sdbne+20Gt58ewyum install -y partedpartprobe /dev/sdbpvcreate /dev/sda6vgcreate myvg /dev/sda6lvcreate -n mydata -L 1000M myvgmke2fs -j /dev/myvg/mydatamkdir /mydatavim /etc/fstab/dev/myvg/mydata  /mydata   ext3  defaults  0 0mount -amountmkdir /mydata/datachown -R mysql:mysql /mydata/data/ls /mydata/chmod o-rx /mydata/data/# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \          -DMYSQL_DATADIR=/mydata/data \          -DSYSCONFDIR=/etc \          -DWITH_INNOBASE_STORAGE_ENGINE=1 \          -DWITH_ARCHIVE_STORAGE_ENGINE=1 \          -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \          -DWITH_READLINE=1 \          -DWITH_SSL=system \          -DWITH_ZLIB=system \          -DWITH_LIBWRAP=0 \          -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \          -DDEFAULT_CHARSET=utf8 \          -DDEFAULT_COLLATION=utf8_general_ci# make # make install
cp support-files/my-defaults.cnf /etc/my.cnfcp support-files/mysql.server /etc/rc.d/init.d/mysqldchmod +x /etc/rc.d/init.d/mysqldchkconfig --add mysqldecho "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profilesource /etc/profilechmod +x scripts/mysql_install_db /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ --basedir=/usr/local/mysql

编译mysql出现错误
这里写图片描述

该报错原因是未安装ncurses-devel,运行下面命令第一步:安装#yum -y install ncurses-devel第二步:删除CMakeCache.txt通过find命令找到所有CMakeCache.txt文档的位置#find / -name CMakeCache.txt然后全部删除:

初始化数据库时出现错误
这里写图片描述

删除数据库目录下的ib_logfile0,ib_logfile1cd /mydata/datarm -f ib_logfile0 ib_logfile1

安装过程出现错误:
使用partprobe命令出现警告http://www.cnblogs.com/kerrycode/p/5657468.html
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法 http://blog.csdn.net/zhaokuner/article/details/7276228
MySQL数据库出现The server quit without updating PID file.http://www.cnblogs.com/lyongde/p/3725502.html
这里写图片描述
http://www.jb51.net/article/48625.htm
这里写图片描述
这里写图片描述

如果依然出现错误,那就重新编译安装一次,或者换一个mysql版本,本人开始用mysq5.7,出现各种错误,后来用mysql5.6版本,成功安装。

为mysql添加密码
mysqladmin -u root password ‘root’
mysql -u root -p

编译安装php

下载

wget http://cn2.php.NET/distributions/php-5.6.2.tar.gz

安装

tar zvxf php-5.6.2.tar.gz -C /usr/src/yum -y install gd libxml2-devel libjpeg-devel libpng-develcd /usr/src/php-5.6.2/./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 --with-jpeg-dir=/usr/libmake && make install
安装后调整cp php.ini-development /usr/local/php5/php.ini[root@localhost php-5.6.2]# ln -s /usr/local/php5/bin/* /usr/local/bin/[root@localhost php-5.6.2]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
配置 nginx 支持 php 环境1)启用 php-fpm 进程 cd /usr/local/php5/etc/cp php-fpm.conf.default php-fpm.confvim php-fpm.conf

这里写图片描述
这里写图片描述
这里写图片描述

groupadd phpuseradd -g php php/usr/local/sbin/php-fpmnetstat -anpt | grep php-fpm
2)修改 nginx 脚本文件,使 php-fpm 自动运行vim /etc/init.d/nginx在最后 esac 后面添加下面内容 

这里写图片描述
启动php-fpm出现错误
这里写图片描述
netstat -lntup | grep 9000
killall php-fpm
php-fpm

3) 配置 nginx 支持解析 php vim /etc/nginx/nginx.conf  在 server 区域里面添加下面几行(有注释) ( 这段配置要放到你要解析的根目录的 server 区域下 )  一会在 /var/www/google 根目录下建立 php 网页, 

这里写图片描述
这里写图片描述
最后执行 server nginx reload 即可生效
cd /var/www/google
vim test.php

<?php$link=mysql_connect('localhost','root','root');if($link) echo "<h1>ok</h1>";mysql_close();?>

解压下载的软件包放在
/var/www/google/目录下
这里写图片描述
浏览器访问http://www.google.com/zypc/index.php
这里写图片描述