2013年5月21日星期二

安装搭建CentOS6.x+Nginx+PHP+MariaDB(MySQL)+phpMyAdmin+Git+Gitweb(带 LNMP全自动安装shell脚本)


本篇教程中,我们用MariaDB代替大家所熟知的MySQL。因为MariaDB比MySQL有比较大的优越性。具体优点可以Google查阅相关文章。


名词解说:
LNMP是Linux web服务器组合套装的缩写,分别是Nginx+MySQL+PHP。


Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。


MariaDB 是一个采用 Maria 存储引擎的 MySQL 分支版本,是由原来 MySQL 的作者 Michael Widenius 创办的公司所开发的免费开源的数据库服务器。MariaDB数据库完全兼容MySQL数据库,因此可以直接把MySQL数据库的文件(.frm、.MYD、.MYI文件)拷贝到MariaDB数据库中直接运行。


与 MySQL 相比较,MariaDB 更强的地方在于:


Maria 存储引擎
PBXT 存储引擎
XtraDB 存储引擎
FederatedX 存储引擎
更快的复制查询处理
线程池
更少的警告和bug
运行速度更快
更多的 Extensions (More index parts, new startup options etc)
更好的功能测试
数据表消除
慢查询日志的扩展统计
支持对 Unicode 的排序


Git是一个由林纳斯·托瓦兹为了更好地管理linux内核开发而创立的分布式版本控制/软件配置管理软件。最初的开发动力来自于 BitKeeper和Monotone。Git最初只是作为一个可以被其他前端比如Cogito或StGIT包装的后端而开发的。不过,后来Git内核已 经成熟到可以独立地用作版本控制。很多有名的软件都使用Git来进行版本控制,其中有Linux内核、X.Org服务器和OLPC内核开发。想了解更多 Git的信息和Git优于其它版本控制软件如Svn等可以自行Google查询。


Gitweb为Git 资源库提供了一个web界面,其中一个功能是提供RSS输入,用于跟踪项目的开发进展。简单的说,Gitweb和Git的关系就像是phpMyAdmin和MySQL的关系。


安装步骤:



  1. 我们使用root账户进行安装,首先切换到root账户,输入命令:
    su -

  2. 开始安装Nginx和PHP-FPM之前,我们必须卸载系统中以前安装的或系统自动安装的Apache和PHP然后更新软件库。输入下面的命令:
    yum remove httpd* php*  mysql
    yum update

  3. Nginx+PHP安装
    使用Nginx官方源,需要先安装epel扩展库和remi源,remi源基于epel,必须先安装epel源,remi包含php-fpm,mysql-server5.5(在本篇教程中我们已经使用MariaDB取代MySQL),如果只需要php-fpm可以单独安装php-fpm后禁用此源什么是epel:
    epel 即: Extra Packages for Enterprise Linux 的缩写,是yum 的一个相关丰富的的软件库!
    企业版Linux附加软件包(Extra Packages for Enterprise Linux,以下简称EPEL)是一个由特别兴趣小组(EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造)创建、维护并管理的,针对红帽企业版Linux(RHEL)及其衍生发行版(比如CentOS、Scientific Linux)的一个高质量附加软件包项目。为什么要配置epel源呢
    因为rhel6里很多开发包没有在iso里,在epel的源里(rhel6 是建立在fedra 12上的)!
    官方地址: http://fedoraproject.org/wiki/EPEL
    中国下载地址: ftp://mirrors.ustc.edu.cn/fedora/epel/或http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-5.noarch.rpm下面用到的参数说明:
    uvh指的是升级安装。而ivh就是指安装而已。
    u—upgrade install
    i—install安装EPEL源(注意:CentOs 5 的下载 EPEL5,CentOs6的下载 EPEL6 的版本,64位系统和32位系统的也请选择相应的源下载):
    CentOS6源:
    ## CentOS 6 64位Epel源##
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
    ## CentOS 6 32位Epel源##
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpmCentOS5源:
    ## CentOS 5  64位Epel源##
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
    ## CentOS 5  32位Epel源##
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm安装REMI源(注意:CentOs 5 的下载 EPEL5,CentOs6的下载 EPEL6 的版本):
    ## CentOS 6 Remi源##
    rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    ## CentOS 5 Remi源##
    rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm安装Nginx源:
    CentOS6
    rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    CentOS5
    rpm -Uvh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm安装compat-mysql55,解决MariaDB与mysql-lib版本中突的问题
    ##CenotOS6用到
    rpm -Uvh http://rpms.famillecollet.com/enterprise/6/test/x86_64/compat-mysql55-5.5.11-1.el6.remi.x86_64.rpm
    ##CenotOS5用到
    #rpm -Uvh http://rpms.famillecollet.com/enterprise/5/test/x86_64/compat-mysql55-5.5.11-1.el5.remi.x86_64.rpm安装Nginx,PHP 5.3.8,PHP-FPM和所需的PHP模块:
    yum --enablerepo=remi,remi-test install nginx php php-fpm php-common
    yum --enablerepo=remi,remi-test install php-pear php-pdo php-mysql
    yum --enablerepo=remi,remi-test install php-pgsql php-pecl-memcache
    yum --enablerepo=remi,remi-test install php-gd php-mbstring php-mcrypt php-xml

     


    PHP模块列表和说明:


    APC (php-pecl-apc) – APC caches and optimizes PHP intermediate code
    CLI (php-cli) – Command-line interface for PHP
    PEAR (php-pear) – PHP Extension and Application Repository framework
    PDO (php-pdo) – A database access abstraction module for PHP applications
    MySQL (php-mysql) – A module for PHP applications that use MySQL databases
    PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
    MongoDB (php-pecl-mongo) – PHP MongoDB database driver
    SQLite (php-sqlite) – Extension for the SQLite V2 Embeddable SQL Database Engine
    Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
    Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
    GD (php-gd) – A module for PHP applications for using the gd graphics library
    XML (php-xml) – A module for PHP applications which use XML
    MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
    MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support


    nginx安装方法二:
    a、配置CentOS 6.3 第三方yum源(CentOS默认的标准源里没有nginx软件包):
    #安装下载工具wget
    yum install wget
    #下载atomic yum源
    wget http://www.atomicorp.com/installers/atomic
    #安装
    sh ./atomic
    #更新yum软件包
    yum check-update
    b、安装nginx:
    #安装nginx,根据提示,输入Y安装即可成功安装
    yum install nginx


    配置nginx支持php
    cp /etc/nginx/nginx.conf  /etc/nginx/nginx.confbak    #备份原有配置文件
    vi /etc/nginx/nginx.conf  #编辑
    user   www www;  #修改nginx运行账号为:www组的www用户
    :wq!    #保存退出


    cp /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.confbak   #备份原有配置文件
    vi /etc/nginx/conf.d/default.conf   #编辑


    index  index.php index.html index.htm;   #增加index.php


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    include        fastcgi_params;
    }
    #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径


    配置php-fpm


    cp /etc/php-fpm.d/www.conf   /etc/php-fpm.d/www.confbak   #备份原有配置文件
    vi /etc/php-fpm.d/www.conf   #编辑
    user = www   #修改用户为www
    group = www   #修改组为www
    启动 Nginx 和 PHP-FPM:
    /etc/init.d/nginx start ## use restart after update
    ## OR ##
    service nginx start ## use restart after update


    /etc/init.d/php-fpm start ## use restart after update
    ## OR ##
    service php-fpm start ## use restart after update


    最后,设置 Nginx 和 PHP-FPM 开机自动启动:
    chkconfig --add nginx
    chkconfig --levels 235 nginx on
    chkconfig --add php-fpm
    chkconfig --levels 235 php-fpm on



  4. MariaDB的安装
    方法一:
    yum安装参考网址(速度可能有点慢):
    1、http://kb.askmonty.org/en/installing-mariadb-with-yum/#importing-the-mariadb-signing-key
    2、http://downloads.mariadb.org/mariadb/repositories/
    #导入MariaDB签名##########################################################
    rpm –import http://yum.mariadb.org/RPM-GPG-KEY-MariaDB
    #添加MariaDB Yum 源#######################################################
    echo ‘# MariaDB repository list – created 2012-08-12 10:47 UTC
    # http://downloads.mariadb.org/mariadb/repositories/
    [mariadb]
    name = MariaDB
    baseurl = http://yum.mariadb.org/5.5/centos5-amd64
    gpgcheck=1′>>/etc/yum.repos.d/MariaDB.repo
    echo 中的内容是针对CentOS6系列64位的系统,如果您的系统是其它的版本,请在http://downloads.mariadb.org/mariadb/repositories/中自行选择,然后替换上面的内容#通过Yum安装MariaDB#######################################################
    yum install MariaDB-server MariaDB-client
    #启动MariaDB#############################################################
    /etc/init.d/mysql start方法二:
    rpm安装:
    参考网址:http://kb.askmonty.org/en/installing-mariadb-with-the-rpm-tool/加入启动项并启动mysql
    chkconfig –levels 235 mysql on
    /etc/init.d/mysql start
    设置mysql密码及相关设置
    mysql_secure_installation
    因为第一次启动这命令,所以直接回车下一步,然后输入你的mysql密码,按照提示操作,删除test数据库和禁止root用户远程登录等。

  5. 安装phpMyAdmin来管理MySQL:
    yum -y install phpmyadmin
    将phpMyAdmin的配置文件复制到apache配置文件目录:
    修改nginx配置文件cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak  #备份配置文件vi /etc/nginx/conf.d/default.conf  #修改配置文件,在server {
    listen       80;
    server_name  localhost;#charset koi8-r;

     


    #access_log  logs/host.access.log  main;


    location / {
    root   html;
    index index.php  index.html index.htm;
    }


    下面添加以下内容:


    location /phpmyadmin {
    alias /usr/share/phpmyadmin;
    index index.php;
    }


    location ~ /phpmyadmin/.+\.php$ {
    if ($fastcgi_script_name ~ /phpmyadmin/(.+\.php.*)$) {
    set $valid_fastcgi_script_name $1;
    }
    include fastcgi_params;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$valid_fastcgi_script_name;
    }


    :wq!   #保存,退出


    设置/home/phpmyadmin目录权限


    chown www.www /usr/share/phpmyadmin -R  #修改目录所有者为www账号


    service nginx restart   #重启nginx


    service php-fpm restart    #重启php-fpm


    现在可以使用域名+phpmyadmin来访问了http://192.168.1.100/phpmyadmin/



  6. 安装git和gitweb安装git
    安装git非常简单,在CentOS下只要以下命令即可# yum install git
    (用 yum install git-core也可以)
    安装gitweb
    # yum install gitweb
    系统默认安装到了/var/www/git下
    配置nginx访问git,git需要CGI支持,配置nginx支持CGI请参考网址:http://blog.onovps.com/archives/nginx-perl-fastcgi.html
    配置文件参考:










    01server {










    02    listen 80;










    03  #listen 443;










    04  server_name cgi.test.com;










    05  index gitweb.cgi;










    06 










    07  error_log /var/log/nginx/git.error.log;










    08  access_log /var/log/nginx/git.access.log;










    09 










    10  # ssl because cox sucks










    11  #ssl on;










    12  #ssl_certificate /etc/nginx/ssl/git.eatabrick.org;










    13  #ssl_certificate_key /etc/nginx/ssl/server.key;










    14 










    15  root /var/www/git;










    16 










    17  # static repo files for cloning over https










    18  location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {










    19    root /data/wwwroot/git/;










    20  }










    21 










    22  # requests that need to go to git-http-backend










    23  location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {










    24    root /data/wwwroot/git;










    25 










    26    fastcgi_pass  unix:/tmp/perl-fastcgi.sock;










    27    fastcgi_param SCRIPT_FILENAME   /usr/libexec/git-core/git-http-backend;










    28    fastcgi_param PATH_INFO         \$uri;










    29    fastcgi_param GIT_PROJECT_ROOT  /data/wwwroot/git;










    30    include fastcgi_params;










    31  }










    32 










    33  # send anything else to gitweb if it\'s not a real file










    34  try_files \$uri @gitweb;










    35  location @gitweb {










    36    fastcgi_pass  unix:/tmp/perl-fastcgi.sock;










    37    fastcgi_param SCRIPT_FILENAME   /var/www/git/gitweb.cgi;










    38    fastcgi_param PATH_INFO         \$uri;










    39    fastcgi_param GITWEB_CONFIG     /etc/gitweb.conf;










    40    include fastcgi_params;










    41  }










    42}




    service nginx restart   #重启nginx
    service php-fpm restart    #重启php-fpm
    就可以通过IE浏览,http://xxxx/git/gitweb.cgi
    Git全自动安装shell脚本












    001#!/bin/bash










    002 










    003# -------------------------------------------------------------------------------










    004# Filename:    git.sh










    005# Revision:    1.1










    006# Date:        2012/09/10










    007# Author:      三木










    008# Email:       linmaogan#gmail.com










    009# Website:     www.3mu.me










    010# Description: 安装git










    011# Notes:       需要切换到root运行,版本针对64位系统,操作系统为CentOS6.3










    012# -------------------------------------------------------------------------------










    013# Copyright:   2012 (c) 三木










    014# License:     GPL










    015#










    016# This program is free software; you can redistribute it and/or










    017# modify it under the terms of the GNU General Public License










    018# as published by the Free Software Foundation; either version 2










    019# of the License, or (at your option) any later version.










    020#










    021# This program is distributed in the hope that it will be useful,










    022# but WITHOUT ANY WARRANTY; without even the implied warranty










    023# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the










    024# GNU General Public License for more details.










    025#










    026# you should have received a copy of the GNU General Public License










    027# along with this program (or with Nagios);










    028#










    029# Credits go to Ethan Galstad for coding Nagios










    030# If any changes are made to this script, please mail me a copy of the changes










    031# -------------------------------------------------------------------------------










    032#Version 1.0










    033#2012-08-21 三木 初始版本建立










    034#Version 1.1










    035#2012-09-10 三木 增加git的web服务器类型判断










    036# -------------------------------------------------------------------------------










    037 










    038#变量定义#############################################################################










    039#设置做为git的web服务器的类型,apache或nginx










    040SERVICE=apache










    041#设置Git默认访问的用户名和密码,可根据实际进行修改










    042DEFAULT_USER1=linmaogan










    043DEFAULT_USER2=linmaogan










    044DEFAULT_PASSWORD=123456










    045#安装git###############################################################################










    046yum -y install git










    047#安装gitweb############################################################################










    048yum -y install gitweb










    049#设置版本库的根目录####################################################################










    050cp -p /etc/gitweb.conf /etc/gitweb.conf.bak










    051sed -i 's/$projectroot = /#$projectroot = /g' /etc/gitweb.conf










    052echo '$projectroot = "/data/wwwroot/git";' >> /etc/gitweb.conf










    053#开启网址伪静态










    054echo '$feature{'pathinfo'}{'default'} = [1];' >> /etc/gitweb.conf










    055#生成虚拟主机配置文件##################################################################










    056if [ $SERVICE == 'apache' ];then










    057echo '










    058    ServerAdmin webmaster@build-server










    059    ServerName nb.tooqe.com










    060    #ServerAlias guiwan.com










    061 










    062    Alias /gitweb.css /var/www/git/gitweb.css










    063    Alias /gitweb.js /var/www/git/gitweb.js










    064    Alias /git-logo.png /var/www/git/git-logo.png










    065    Alias /git-favicon.png /var/www/git/git-favicon.png










    066 










    067    SetEnv GIT_PROJECT_ROOT /data/wwwroot/git










    068    SetEnv GIT_HTTP_EXPORT_ALL










    069    SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER










    070 










    071        AuthType Basic










    072        AuthName "Git"










    073        AuthUserFile /data/conf/gitweb.passwd










    074        Require valid-user










    075 










    076        Options +ExecCGI -Includes










    077 










    078    # This pattern matches git operations and passes them to http-backend










    079    ScriptAliasMatch \










    080        "(?x)^/git/(.*/(HEAD | \










    081                        info/refs | \










    082                        objects/(info/[^/]+ | \










    083                                 [0-9a-f]{2}/[0-9a-f]{38} | \










    084                                 pack/pack-[0-9a-f]{40}\.(pack|idx)) | \










    085                        git-(upload|receive)-pack))$" \










    086        /usr/libexec/git-core/git-http-backend/$1










    087 










    088    # Anything not matched above goes to displayable gitweb interface










    089    ScriptAlias /git /var/www/git/gitweb.cgi/










    090 










    091        Options FollowSymLinks +ExecCGI










    092        AddHandler cgi-script .cgi










    093 










    094' > /data/conf/sites-available/gitweb.conf










    095else










    096cat > /data/conf/sites-available/gitweb.conf << EOFI










    097server {










    098    listen 80;










    099  #listen 443;










    100  server_name cgi.test.com;










    101  index gitweb.cgi;










    102 










    103  error_log /var/log/nginx/git.error.log;










    104  access_log /var/log/nginx/git.access.log;










    105 










    106  # ssl because cox sucks










    107  #ssl on;










    108  #ssl_certificate /etc/nginx/ssl/git.eatabrick.org;










    109  #ssl_certificate_key /etc/nginx/ssl/server.key;










    110 










    111  root /var/www/git;










    112 










    113  # static repo files for cloning over https










    114  location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {










    115    root /data/wwwroot/git/;










    116  }










    117 










    118  # requests that need to go to git-http-backend










    119  location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {










    120    root /data/wwwroot/git;










    121 










    122    fastcgi_pass  unix:/tmp/perl-fastcgi.sock;










    123    fastcgi_param SCRIPT_FILENAME   /usr/libexec/git-core/git-http-backend;










    124    fastcgi_param PATH_INFO         \$uri;










    125    fastcgi_param GIT_PROJECT_ROOT  /data/wwwroot/git;










    126    include fastcgi_params;










    127  }










    128 










    129  # send anything else to gitweb if it\'s not a real file










    130  try_files \$uri @gitweb;










    131  location @gitweb {










    132    fastcgi_pass  unix:/tmp/perl-fastcgi.sock;










    133    fastcgi_param SCRIPT_FILENAME   /var/www/git/gitweb.cgi;










    134    fastcgi_param PATH_INFO         \$uri;










    135    fastcgi_param GITWEB_CONFIG     /etc/gitweb.conf;










    136    include fastcgi_params;










    137  }










    138}










    139EOFI










    140fi










    141 










    142#apache创建sites-available/*到sites-enabled/*的链接










    143ln -s /data/conf/sites-available/gitweb.conf /data/conf/sites-enabled/










    144#创建初始密码##########################################################################










    145htpasswd -bc /data/conf/gitweb.passwd $DEFAULT_USER1 $DEFAULT_PASSWORD










    146htpasswd -b /data/conf/gitweb.passwd $DEFAULT_USER2 $DEFAULT_PASSWORD










    147#重启所有服务器










    148if [ $SERVICE == 'apache' ];then










    149    /etc/init.d/nginx restart










    150    /etc/init.d/php-fpm restart










    151    /etc/init.d/perl-fastcgi restart










    152else










    153    /etc/init.d/httpd restart










    154fi










    155 










    156/etc/init.d/mysql restart





  7. 本文在CentOS 6 64bit minimal版本上测试通过

  8. 到此,我们LNMP运行环境的搭建已经完成。

  9. 本来还打算写另一篇教程:[三木]十分钟教程之服务器安装搭建CentOS5.x+Nginx+PHP+MariaDB(MySQL)+phpMyAdmin(带LAMP全自动安装shell脚本),因为CentOS5.x和6.x的安装设置差不多,只是需要对yum源做相应的修改即可,所以针对CentOS5.x不再额外出教程,需要在这个版本上配置LNMP运行环境的同学直接参考这篇教程即可。

  10. LNMP全自动安装Shell脚本:











    001#!/bin/bash










    002 










    003# -------------------------------------------------------------------------------










    004# Filename:    lnmp.sh










    005# Revision:    1.0










    006# Date:        2012/08/21










    007# Author:      三木










    008# Email:       linmaogan#gmail.com










    009# Website:     www.3mu.me










    010# Description: CentOS6.3+Nginx+PHP+MariaDB+Memcache及相关扩展安装脚本










    011# Notes:       需要切换到root运行,版本针对64位系统,操作系统为CentOS6.3










    012# -------------------------------------------------------------------------------










    013# Copyright:   2012 (c) 三木










    014# License:     GPL










    015#










    016# This program is free software; you can redistribute it and/or










    017# modify it under the terms of the GNU General Public License










    018# as published by the Free Software Foundation; either version 2










    019# of the License, or (at your option) any later version.










    020#










    021# This program is distributed in the hope that it will be useful,










    022# but WITHOUT ANY WARRANTY; without even the implied warranty










    023# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the










    024# GNU General Public License for more details.










    025#










    026# you should have received a copy of the GNU General Public License










    027# along with this program (or with Nagios);










    028#










    029# Credits go to Ethan Galstad for coding Nagios










    030# If any changes are made to this script, please mail me a copy of the changes










    031# -------------------------------------------------------------------------------










    032#Version 1.0










    033#2012-08-21 三木 初始版本建立










    034#Version 1.1










    035#2012-09-01 三木 修复php不能连接mysql的bug










    036# -------------------------------------------------------------------------------










    037 










    038#变量定义#############################################################################










    039IS_VPS=1










    040#解锁系统文件#########################################################################










    041chattr -i /etc/passwd










    042chattr -i /etc/group










    043chattr -i /etc/shadow










    044chattr -i /etc/gshadow










    045#如果已安装Apache和PHP,则卸载########################################################










    046yum -y remove httpd* php*  mysql










    047#更新软件库###########################################################################










    048yum -y update










    049#安装Nginx源##########################################################################










    050rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm










    051#安装EPEL源###########################################################################










    052rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm










    053#安装REMI源###########################################################################










    054rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm










    055#安装Nginx############################################################################










    056yum -y --enablerepo=remi,remi-test install nginx










    057#加入启动项###########################################################################










    058chkconfig --levels 235 nginx on










    059#start Nginx##########################################################################










    060/etc/init.d/nginx start










    061#安装compat-mysql55,解决MariaDB与mysql-lib版本突的问题###############################










    062##CenotOS6用到










    063rpm -Uvh http://rpms.famillecollet.com/enterprise/6/test/x86_64/compat-mysql55-5.5.11-1.el6.remi.x86_64.rpm










    064##CenotOS5用到










    065#rpm -Uvh http://rpms.famillecollet.com/enterprise/5/test/x86_64/compat-mysql55-5.5.11-1.el5.remi.x86_64.rpm










    066#安装PHP和常用库######################################################################










    067yum -y --enablerepo=remi,remi-test install php php-fpm php-common php-pear php-pdo php-mysql php-pgsql php-pecl-memcache php-gd php-mbstring php-mcrypt php-xml










    068#php-fpm加入启动项####################################################################










    069chkconfig --levels 235 php-fpm on










    070#安装phpmyadmin#######################################################################










    071yum -y install phpmyadmin










    072#Importing the MariaDB Signing Key####################################################










    073rpm --import http://yum.mariadb.org/RPM-GPG-KEY-MariaDB










    074#Adding the MariaDB YUM Repository####################################################










    075echo '# MariaDB repository list - created 2012-08-12 09:38 UTC










    076# http://downloads.mariadb.org/mariadb/repositories/










    077[mariadb]










    078name = MariaDB










    079baseurl = http://yum.mariadb.org/5.5/centos6-amd64










    080gpgcheck=1'>>/etc/yum.repos.d/MariaDB.repo










    081#Installing MariaDB with YUM##########################################################










    082yum -y install MariaDB-server MariaDB-client










    083#加入启动项###########################################################################










    084chkconfig --levels 235 mysql on










    085#start MariaDB########################################################################










    086/etc/init.d/mysql start










    087#设置mysql密码及相关设置##############################################################










    088mysql_secure_installation










    089#配置php-fpm修改用户为nginx










    090cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak   #备份原有配置文件










    091sed -i 's/user = apache/user = www/g










    092s/group = apache/group = www/g' /etc/php-fpm.d/www.conf










    093#配置nginx支持php










    094cp /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak    #备份原有配置文件










    095sed -i '1,5s/user  nginx/user   www  www/g










    096s/include \/etc\/nginx\/conf.d/#include \/etc\/nginx\/conf.d/g










    097/include \/etc\/nginx\/conf.d/a\\include \/data\/conf\/sites-enabled\/\*;' /etc/nginx/nginx.conf #修改nginx运行账号为:www组的www用户










    098 










    099echo "server {










    100    listen       80;










    101    server_name  localhost;










    102 










    103    charset utf8;










    104    #access_log  /var/log/nginx/log/host.access.log  main;










    105 










    106    location / {










    107        root   /www/wwwroot/web;










    108        index  index.html index.htm index.php;  #增加index.php










    109    }










    110 










    111    #error_page  404              /404.html;










    112 










    113    # redirect server error pages to the static page /50x.html










    114    #










    115    error_page   500 502 503 504  /50x.html;










    116    location = /50x.html {










    117        root   /www/wwwroot/web;










    118    }










    119 










    120    # proxy the PHP scripts to Apache listening on 127.0.0.1:80










    121    #










    122    #location ~ \.php$ {










    123    #    proxy_pass   http://127.0.0.1;










    124    #}










    125 










    126    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000










    127    #










    128    #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径










    129    location ~ \.php$ {










    130        root           /www/wwwroot/web;










    131        fastcgi_pass   127.0.0.1:9000;










    132        fastcgi_index  index.php;










    133        fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;










    134        include        fastcgi_params;










    135    }










    136 










    137    # deny access to .htaccess files, if Apache's document root










    138    # concurs with nginx's one










    139    #










    140    #location ~ /\.ht {










    141    #    deny  all;










    142    #}










    143}" > /etc/nginx/conf.d/default.conf










    144 










    145#####################################################################################










    146#####################################################################################










    147#目录设置############################################################################










    148#创建网站相关目录####################################################################










    149if [ $IS_VPS ];then










    150mkdir /home/data










    151ln -s /home/data /data










    152else










    153mkdir /data










    154fi










    155 










    156mkdir /www










    157mkdir /data/wwwroot










    158ln -s /data/wwwroot /www/










    159mkdir /data/wwwroot/log










    160mkdir /data/wwwroot/web










    161mkdir /data/wwwroot/git










    162mkdir /data/conf










    163mkdir /data/conf/sites-available










    164mkdir /data/conf/sites-enabled










    165 










    166mkdir /backup










    167ln -s /backup /data/










    168 










    169#配置文件目录设置######################################################################










    170#移动nginx配置文件










    171cp -p /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.conf.bak










    172mv /etc/nginx/conf.d/default.conf /data/conf/sites-available/










    173ln -s /data/conf/sites-available/default.conf /data/conf/sites-enabled/










    174 










    175cp -p /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak










    176mv /etc/nginx/nginx.conf /data/conf/










    177ln -s /data/conf/nginx.conf /etc/nginx/










    178#移动mysql配置文件










    179cp -p /etc/my.cnf /etc/my.cnf.bak










    180mv /etc/my.cnf /data/conf/










    181ln -s /data/conf/my.cnf /etc/










    182#移动mysql数据库










    183cp -p /var/lib/mysql /var/lib/mysql-bak










    184mv /var/lib/mysql /data/










    185ln -s /data/mysql /var/lib/










    186#移动php配置文件










    187cp -p /etc/php.ini /etc/php.ini.bak










    188mv /etc/php.ini /data/conf/










    189ln -s /data/conf/php.ini /etc/










    190#移动php配置文件










    191cp -p /etc/php-fpm.conf  /etc/php-fpm.conf.bak










    192mv /etc/php-fpm.conf /data/conf/










    193ln -s /data/conf/php-fpm.conf /etc/










    194 










    195#添加www组和www用户####################################################################










    196groupadd www










    197useradd -g www www










    198#设置目录权限##########################################################################










    199chown -R www:www /data/wwwroot/web










    200#php配置#############################################################################










    201#修改时区










    202#禁用的函数










    203#禁止显示php版本的信息










    204sed -i 's/;date.timezone \=/date.timezone \= PRC/










    205s/disable_functions \=/disable_functions \= passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname/










    206s/expose_php = On/expose_php = Off/' /data/conf/php.ini










    207#设置session文件为www用户组










    208chown -R root:www /var/lib/php/session










    209#phpMyAdmin配置######################################################################










    210#设置config.inc.php文件为www用户组










    211chown root:www /usr/share/phpmyadmin/config.inc.php










    212#配置配置文件,将这里tooqe.com文字修改为自己的字符,可任意










    213sed -i "/blowfish_secret/s/''/'tooqe.com'/" /usr/share/phpmyadmin/config.inc.php










    214#phpMyAdmin虚拟主机设置










    215echo 'server {










    216        server_name  nb.tooqe.com;










    217        root /data/wwwroot/web;










    218        access_log /data/wwwroot/log/nb.tooqe.com-access.log;










    219        error_log /data/wwwroot/log/nb.tooqe.com-error.log;










    220 










    221        location / {










    222                index  index.html index.htm index.php;










    223        }










    224        location /phpmyadmin {










    225            alias /usr/share/phpmyadmin;










    226            index index.php;










    227        }










    228 










    229        location ~ /phpmyadmin/.+\.php$ {










    230            if ($fastcgi_script_name ~ /phpmyadmin/(.+\.php.*)$) {










    231                 set $valid_fastcgi_script_name $1;










    232                }










    233            include fastcgi_params;










    234            fastcgi_pass   127.0.0.1:9000;










    235            fastcgi_index  index.php;










    236            fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$valid_fastcgi_script_name;










    237        }










    238        location ~ \.php$ {










    239                include /etc/nginx/fastcgi_params;










    240                fastcgi_pass  127.0.0.1:9000;










    241                fastcgi_index index.php;










    242                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;










    243        }










    244}' > /data/conf/sites-available/phpmyadmin.conf










    245ln -s /data/conf/sites-available/phpmyadmin.conf /data/conf/sites-enabled/phpmyadmin.conf










    246#设置/usr/share/phpmyadmin目录权限










    247chown www.www /usr/share/phpmyadmin -R  #修改目录所有者为www账号










    248 










    249#开启防火墙










    250/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT










    251/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT










    252/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT










    253/etc/rc.d/init.d/iptables save










    254/etc/init.d/iptables restart










    255 










    256#重启所有服务器










    257/etc/init.d/nginx restart










    258/etc/init.d/php-fpm restart










    259/etc/init.d/mysql restart










    260#/etc/init.d/memcached restart










    261 










    262#系统文件加锁










    263chattr +i /etc/passwd










    264chattr +i /etc/shadow










    265chattr +i /etc/gshadow










    266chattr +i /etc/group






 


本文转自三木人生十分钟教程 转载下来只是为了方便自己查看

没有评论:

发表评论