源码方式搭建LNMP环境

 

一.安装Nginx

# 创建www组与www用户

shell># groupadd www

shell># useradd -g www -s /usr/sbin/nologin www

# 安装nginx所需的依赖软件

shell># cd /home/jinnan/tar

shell># tar zxvf pcre-7.9.tar.gz

shell># cd pcre-7.9/

shell># ./configure && make && make install

# 安装Nginx

shell># tar zxvf nginx-0.8.15.tar.gz

shell># cd nginx-0.8.15/

shell># ./configure --user=www --group=www  \

--prefix=/usr/local/nginx \

--with-http_stub_status_module \

--with-http_ssl_module

(https  secure:是比http协议更安装的协议,一般用于支付场合)

第一次执行configure指令会提示依赖错误:

 

去光盘安装该openssl-devel即可

 

之后重新执行configure即可

 

 

 

shell># make && make install

 

 

 

启动Nginx

shell># /usr/local/nginx/sbin/nginx

 

测试配置文件是否正确

shell># /usr/local/nginx/sbin/nginx -t

关闭nginx

shell># killall nginx

 

启动服务后,给linux通过setup指令关闭防火墙

在本机电脑浏览器通过ip地址进行访问:

 

上述页面内容来之:/usr/local/nginx/html/index.html

此时说明nginx的安装成功!

 

 

1.nginx支持php

编辑文件:

shell># vi /usr/local/nginx/conf/fcgi.conf

并写入如下内容:

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;

 

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200;

 

编辑nginx配置文件(找到server项目设置如下内容):

shell># vim /usr/local/nginx/conf/nginx.conf

# 定义如何处理PHP文件-只有加上这段才能处理PHP文件  

#  如果请求的是一个PHP文件,就把这个文件发给本地的9000端口来处理这个文件,处理完之后把结果反回给NGINX,再返回级客户端                  

location ~ .*\.(php|php5)?$

{

  fastcgi_pass  127.0.0.1:9000;       

  #fastcgi(管理PHP),

  fastcgi_index index.php;

  include fcgi.conf;

}


如果是ThinkPHP,则需要定义如下内容:

location ~ .+\.php($|/) {

   set $script    $uri;

   set $path_info  "/";

   if ($uri ~ "^(.+\.php)(/.+)") {

       set $script     $1;

       set $path_info  $2;

    }

        

   fastcgi_pass 127.0.0.1:9000;

   fastcgi_index  index.php?IF_REWRITE=1;

   include fcgi.conf;

   fastcgi_param PATH_INFO $path_info;

   fastcgi_param SCRIPT_FILENAME  $document_root/$script;

   fastcgi_param SCRIPT_NAME $script;

}

 

另外可以在http段内设置如下代码,就会显示目录下对应的文件:

    #自动显示目录

    autoindex on;

    #人性化方式显示文件大小否则以byte显示

    autoindex_exact_size off;

    #按服务器时间显示,否则以gmt时间显示

    autoindex_localtime on;

 

测试nginx:

打开浏览器:http://虚拟机ip地址,看到如下内容说明nginx是成功的!

 

打开浏览器:http://虚拟机ip地址/a.php

a.php具体内容:<?php  phpinfo()  ?>看到如下内容,说明nginx和php合作成功!

 

 

 

配置虚拟主机

1)配置hosts文件

打开C:/windows/system32/drivers/etc/hosts 文件

增加域名记录

如:

192.168.9.38       www.xx1.com

192.168.9.38       www.xx2.com

2) 增加虚拟主机

vi /usr/local/nginx/conf/nginx.conf

server{

  listen 80;

  server_name web.ec1.com;

  index index.php;

  root /var/www/shop;

  location ~ \.php$ {

       fastcgi_pass   127.0.0.1:9000;

       fastcgi_index  index.php;

       include        fcgi.conf;

  }

}

server{

  listen 80;

  server_name web.ec2.com;

  index index.php;

  root /var/www/shop;

  location ~ \.php$ {

       fastcgi_pass   127.0.0.1:9000;

       fastcgi_index  index.php;

       include        fcgi.conf;

  }

}

 

如果使用ThinkPHP框架则location段使用如下:

 location ~ .+\.php($|/) {

     set $script    $uri;

     set $path_info  "/";

     if ($uri ~ "^(.+\.php)(/.+)") {

         set $script     $1;

         set $path_info  $2;

      }

         

     fastcgi_pass 127.0.0.1:9000;

     fastcgi_index  index.php?IF_REWRITE=1;

     include fcgi.conf;

     fastcgi_param PATH_INFO $path_info;

     fastcgi_param SCRIPT_FILENAME  $document_root/$script;

     fastcgi_param SCRIPT_NAME $script;

 }

 

注意:/var/www/shop  以上三个目录var  www  shop 的其他用户必须有x可执行权限

 

 

 

 

以上任何一个目录如果没有x执行权限,则访问就是403 forbidden提示。

 

 

3)

       shell># cd /usr/local/http2/htdocs

        shell># mkdir ec1 ec2

       shell># echo this is ec1.com > ec1/index.html

       shell># echo this is ec2.com > ec2/index.html

 

4)重启nginx

>  killall nginx

>  /usr/local/nginx/sbin/nginx

 

5)浏览器打开www.xx1.com,和www.xx2.com

看到不同的网站内容,虚拟主机创建完毕!

 

安装图形库,为编译PHP做准备

libxml2-2.7.2.tar.gz

jpegsrc.v8b.tar.gz

libpng-1.4.3.tar.gz     

freetype-2.4.1.tar.gz

gd-2.0.35.tar.gz

 

二.安装libxml2

shell># cd /home/jinnan/tar

shell># tar zxvf libxml2-2.7.2.tar.gz

shell># cd libxml2-2.7.2

shell>#./configure --prefix=/usr/local/libxml2  \

--without-zlib

shell># make && make install

 

三.安装jpeg8

shell># cd /home/jinnan/tar

shell># tar -zxvf jpegsrc.v8b.tar.gz

shell># cd jpeg-8b

shell>#./configure --prefix=/usr/local/jpeg \

--enable-shared --enable-static

shell># make && make install

--enable-shared  把jpeg需要的函数库程序都编译到该软件里边

                  优点:函数调用速度快

                              缺点:软件本身比较大

--enable-static   静态方式函数处理,需要什么函数,马上include来

              优点:软件本身比较小

              缺点:函数调用速度慢

四.安装libpng

shell># cd /home/jinnan/tar

shell># tar zxvf libpng-1.4.3.tar.gz

shell># cd libpng-1.4.3

shell>#./configure  #和zlib一样不要带参数,让它默认安装到相应目录

shell># make && make install

 

五.安装freetype(字体库)

shell># cd /home/jinnan/tar

shell># tar zxvf freetype-2.4.1.tar.gz

shell># cd freetype-2.4.1

shell>#./configure --prefix=/usr/local/freetype

shell># make && make install

 

六.安装GD库

shell># cd /home/jinnan/tar

shell># tar -zvxf gd-2.0.35.tar.gz

shell># cd gd-2.0.35

shell>#./configure --prefix=/usr/local/gd  \

--with-jpeg=/usr/local/jpeg/    \

--with-png --with-zlib \

--with-freetype=/usr/local/freetype

shell># make && make install

 

七.安装 php5

shell># cd /home/jinnan/tar

shell># tar -jxvf php-5.3.6.tar.bz2

shell># cd php-5.3.6

shell>#./configure --prefix=/usr/local/php \

--with-mysql=mysqlnd \

--with-pdo-mysql=mysqlnd \

--with-mysqli=mysqlnd \

--with-freetype-dir=/usr/local/freetype \

--with-gd=/usr/local/gd \

--with-zlib --with-libxml-dir=/usr/local/libxml2 \

--with-jpeg-dir=/usr/local/jpeg \

--with-png-dir \

--enable-mbstring=all \

--enable-mbregex \

--enable-shared \

--with-openssl \

--enable-fpm \

--with-config-file-path=/usr/local/php/etc \

--with-curlwrappers

shell># make && make install

复制php.ini配置文件到指定目录

shell># cp php.ini-development /usr/local/php/etc/php.ini

shell># cd /usr/local/php/etc

shell># cp ./php-fpm.conf.default ./php-fpm.conf

 

启动php服务

shell># /usr/local/php/sbin/php-fpm

关闭php服务

shell>#  killall php-fpm

如果有错误,就修改php-fpm.conf配置文件

 

 

上述错误解决:vi  php-fpm.conf   配置文件

去除pm.min前边的”;分号”

 

 

此时php安装成功!

 

 

查看端口占用情况:

shell># netstat -anp

 

 

 

八.安装MySQL

1.安装cmake(更先进的configure)

shell># rpm -ivh cmake-2.8.12.2-4.el6.i686

shell># yum -y install cmake

2.编译安装MySQL

shell># cd /home/jinnan/tar

shell># tar zxvf mysql-5.5.17.tar.gz

shell># cd mysql-5.5.17

shell># cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci

(准备安装到那里

数据存储目录

默认的字符集

校对字符集)

(报错就安装ncurses-devel)

shell># mount .....挂载光盘/


shell># rpm -ivh ncurses-devel-5.7-3.20090208.el6.i686.rpm

shell># rm -f CMakeCache.txt    //删除该文件

shell># cmake 。。。 。。。            //重新cmake

shell># make && make install

shell># cp support-files/my-medium.cnf /etc/my.cnf

3.配置并初始化MySQL

shell># useradd  mysql   (该mysql用户会存在于同名的组下)

shell># chmod +x /usr/local/mysql

(数据库用户信息不小心删除光了

① 删除data目录,

② 同时删除旧的mysql服务

> ps -A | grep mysql     

> killall mysqld  或  kill -9  mysql进程号码

③ 从此开始执行以下指令,直至结束

)

 

shell># chown -R mysql.mysql /usr/local/mysql

 

初始化mysql数据库  

shell># /usr/local/mysql/scripts/mysql_install_db \

--user=mysql \

--basedir=/usr/local/mysql \

--datadir=/usr/local/mysql/data &

 

把mysql安装文件(除了data)的主人都改为root,避免数据库恢复为出厂设置。

shell># chown -R root /usr/local/mysql

shell># chown -R mysql /usr/local/mysql/data

 

& 后台运行mysql服务

shell># /usr/local/mysql/bin/mysqld_safe --user=mysql &

 

//查看mysql是否有启动

shell># ps -A | grep mysql   

 

测试数据库

shell># /usr/local/mysql/bin/mysql –u root

mysql> show databases;  

 

接上步,修改mysql密码(可不做此步,默认无密码)

mysql> UPDATE user SET Password=password('123456') WHERE user='root';

mysql> flush privileges;

4.配置开机自启动服务项

shell># cp 安装包解压目录/support-files/mysql.server /etc/init.d/mysqld

shell># chmod +x /etc/init.d/mysqld

shell># chkconfig --add mysqld

shell># chkconfig mysqld on    //设置开机自启动

 

配置文件路径:

shell># vi /etc/rc.d/rc.local

在  文件中增加启动相关服务的命令如下:

/usr/local/http2/bin/apachectl start

/usr/local/mysql/bin/mysqld_safe --user=mysql &

service vsftpd start

 

 

 

重新安装apache启动失败

[root@localhost httpd-2.2.19]# /usr/local/http2/bin/apachectl restart

httpd not running, trying to start

(98)Address already in use: make_sock: could not bind to address [::]:80

(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shutting down

Unable to open logs

 

原因是80端口被占用

解决:

 

查看80端口使用情况

[root@localhost httpd-2.2.19]# netstat -lnp|grep 80

tcp        0      0 :::80                       :::*                        LISTEN      28195/httpd        

unix  2      [ ACC ]     STREAM     LISTENING     6580   1957/gpm            /dev/gpmctl

unix  2      [ ACC ]     STREAM     LISTENING     5422   1800/pcscd          /var/run/pcscd.comm

 

 

查看80的使用者是谁端口

[root@localhost httpd-2.2.19]# ps 28195

  PID TTY      STAT   TIME COMMAND

28195 ?        Ss     0:00 /usr/local/http2/bin/httpd -k restart

 

 

经过分析知道了80端口被系统的一个进程占用,这个进程是旧的apache服务

 

将这个进程杀之

[root@localhost httpd-2.2.19]# kill -9 28195

[root@localhost httpd-2.2.19]#

 

 

 

 

 

 

九.卸载操作系统自带apache

 

 

  1. 删除默认apache进程
  • ps –A | grep  http
  • 杀死apache对应进程

 

 

killall  httpd  杀死全部的httpd进程

  1. 把默认apache服务给删除

 

 

  1. 启动自己的apache

 

 

 

十.配置本地yum源

配置yum为本地光盘源地址后,可以使用yum进行软件安装

 

1. 挂载光驱(操作系统光盘)

shell># mount   /dev/cdrom    /home/jinnan/rom

 

2. 进入yum配置目录

shell># cd /etc/yum.repos.d

 

3. 修改文件名字(默认找Base,其次找Media)

shell># mv ./CentOS-Base.repo ./CentOS-Base.repo.bk

 

4. 修改文件

修改该文件:./CentOS-Media.repo,内容为如下蓝色部分

shell># vi CentOS-Media.repo

 

baseurl=file:///home/jinnan/rom/

        file:///media/cdrom/

        file:///media/cdrecorder/

gpgcheck=1

enabled=1  #改为1

 

现在就可以这样安装gcc等相关rpm软件:

shell># yum -y install gcc

shell># yum -y install vim

remove/install

 

十一. 防火墙配置

在开启了防火墙时,做如下设置,开启相关端口,
修改/etc/sysconfig/iptables 文件,添加以下内容:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

#最后重启防火墙使配置生效

/etc/init.d/iptables restart

(1) 重启后永久性生效:

开启:chkconfig iptables on

关闭:chkconfig iptables off

(2) 即时生效,重启后失效:

开启:service iptables start

关闭:service iptables stop

需要说明的是对于Linux下的其它服务都可以用以上命令执行开启和关闭操作。

在开启了防火墙时,做如下设置,开启相关端口,

修改/etc/sysconfig/iptables 文件,添加以下内容:

-A RH-Firewall-1-INPUT -m state ——state NEW -m tcp -p tcp ——dport 80 -j ACCEPT

-A RH-Firewall-1-INPUT -m state ——state NEW -m tcp -p tcp ——dport 22 -j ACCEPT

或者:

/etc/init.d/iptables status 会得到一系列信息,说明防火墙开着。

/etc/rc.d/init.d/iptables stop 关闭防火墙

1.防火墙bug

纯净版的没有相关防火墙配置文件:/etc/sysconfig/iptables

> vi /etc/sysconfig/iptables

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

然后再启动防火墙

service iptables start

查看防火墙服务

service iptables status

2.没有setup命令

> yum -y install setuptool

> yum -y install ntsysv

> yum -y install system-config-securitylevel-tui

> yum -y install system-config-network-tui

 

十二.设置nginx启动关闭服务

1. 在任意位置创建如下文件,里面写入以下shell脚本如:

 

 

进入编辑模式并复制以下内容:

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

 

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/usr/local/nginx/logs/nginx.pid

 

RETVAL=0

prog="nginx"

 

# Source function library.

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

 

# Source networking configuration.

. /etc/sysconfig/network

 

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

 

[ -x $nginxd ] || exit 0

 

 

# Start nginx daemons functions.

start() {

 

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

 

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

 

}

 

 

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

 

 

# reload nginx service functions.

reload() {

 

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

 

}

 

# See how we were called.

case "$1" in

start)

        start

        ;;

 

stop)

        stop

        ;;

 

reload)

        reload

        ;;

 

restart)

        stop

        start

        ;;

 

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

 

exit $RETVAL

  1. 把这个文件复制到/etc/init.d目录下

shell># cp ./nginx /etc/init.d

  1. 修改这个文件为可执行的权限

shell># chmod +x /etc/init.d/nginx

  1. 把这个可执行文件加到服务服务中去

shell># chkconfig --add nginx

之后就可以使用 service 命令来管理了!

例如:

shell># service nginx stop

shell># service nginx start

shell># service nginx restart

 

十三.设置php-fpm启动关闭服务

1. 编辑文件

shell># vi /etc/init.d/php-fpm

2. 给文件设置如下内容:

#!/bin/sh  

# DateTime: 2013-09-16

# Author: lianbaikai

# site:http://www.ttlsa.com/html/3039.html

# chkconfig:   - 84 16   

# Source function library.  

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

 

# Source networking configuration.  

. /etc/sysconfig/network  

 

# Check that networking is up.  

[ "$NETWORKING" = "no" ] && exit 0  

 

phpfpm="/usr/local/php/sbin/php-fpm"  

prog=$(basename ${phpfpm})  

 

lockfile=/var/lock/subsys/phpfpm

 

start() {  

    [ -x ${phpfpm} ] || exit 5  

    echo -n $"Starting $prog: "  

    daemon ${phpfpm}

    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  

    start  

}  

 

reload() {  

    configtest || return $?  

    echo -n $"Reloading $prog: "  

    killproc ${phpfpm} -HUP  

    RETVAL=$?  

    echo  

}  

 

force_reload() {  

    restart  

}  

 

configtest() {  

  ${phpfpm} -t

}  

 

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  

        ;;  

    status)  

        rh_status  

        ;;  

    *)  

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

        exit 2  

esac

3. 修改这个文件为可执行的权限

shell># chmod +x /etc/init.d/php-fpm

4. 把这个可执行文件加到服务服务中去

shell># chkconfig --add nginx

5. 之后就可以如下方式操作php

shell># service  php-fpm  start

shell># service  php-fpm  stop

shell># service  php-fpm  restart

 

posted @ 2017-04-10 14:40  酷炫地骨折  阅读(829)  评论(0编辑  收藏  举报