CentOS源碼安裝搭建LNMP全過程(包括nginx,mysql,php,svn)【轉】

來源:互聯網
上載者:User

標籤:

轉自:http://blog.csdn.net/yanzi1225627/article/details/49123659

伺服器環境為:CentOS6.5 64位

目標:搭建LNMP(Linux + Nginx + MySQL + PHP +SVN),其中svn是用來代替ftp,方便開發中調試同步代碼

相關目錄:所有軟體都安裝到/www/目錄下,在www目錄下建立web檔案夾作為網站的根路徑,www目錄下建立wwwsvn作為svn的倉庫地址。/www/software用來放nginx,mysql,php的安裝包和源碼。nginx運行分組和賬戶www:www

一,安裝前的準備

yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel cmake

直接將所有待安裝的依賴安裝完。

然後下載nginx ,mysql, php的原始碼:

nginx:http://nginx.org/en/download.html 下載1.8.0的穩定版:nginx-1.8.0.tar.gz

MySQL:http://dev.mysql.com/downloads/mysql/ ,下載Community Server版本的尾碼為tar.gz的源碼mysql-5.6.27.tar.gz:

 

php:http://php.net/downloads.php 下載php-5.6.14.tar.gz

將這三份tar.gz檔案通過scp命令弄到伺服器上/www/software目錄下。

二,安裝nginx

解壓縮檔案,然後進到nginx-1.8.0裡,輸入命令:

./configure --user=www --group=www --prefix=/www/nginx

然後make,make install就安裝完畢了。

安裝完後第一件事,建立www的使用者和分組,否則會遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的錯誤。

執行:

/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www

nginx命令在/www/nginx/sbin/下,拷貝到/etc/init.d/一份,接下來設定開機啟動。

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

然後

cd /etc/rc.d/init.d/ 目錄下建立nginx,內容如下:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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=/www/nginx/sbin/nginx
nginx_config=/www/nginx/conf/nginx.conf
nginx_pid=/www/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 /www/nginx/logs/nginx.pid
}
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

注意:如果nginx的安裝路徑不是在/www/nginx下,則適當修改就好。

 

chmod 775 /etc/rc.d/init.d/nginx #賦予執行許可權chkconfig nginx on #設定開機啟動/etc/rc.d/init.d/nginx restartservice nginx restart

至此nginx安裝就ok了,但遺留兩個問題:

 

1,是更改預設web根目錄在/www/web的問題 

2,是與php的整合,預設nginx是不認php得

對於1,nginx預設web根目錄在 nginx安裝路徑下的html檔案夾,我們把他改到/www/web目錄下。

進到/www/nginx/conf目錄下,vim nginx.conf,將

 

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

修改為:

 

 

        location / {

            root   /www/web;

            index  index.html index.php;

        }

注意,增加了對index.php的識別。

 

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;        }

修改為:

 

 

        location ~ \.php$ {

            root           /www/web;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            #include        fastcgi_params;

            include fastcgi.conf;

       }

然後就ok了。

第二個問題跟php的整合,待安裝完畢php後再整。

三,安裝MySQL

解壓縮並進到源碼目錄,執行:

 

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

之後make make install安裝。安裝完畢後需要做以下幾個事:

1,檢查/etc/下是否存在my.conf, 如果有的話通過mv命令改名為

my.cnf.backup

ps:此步驟非常重要!!!

2,建立mysql使用者和分組

 #/usr/sbin/groupadd mysql

#/usr/sbin/useradd -g mysql mysql 增加mysql使用者和分組。

執行

cat /etc/passwd 查看使用者列表
cat /etc/group  查看使用者組列表

chown -R mysql:mysql /www/mysql修改mysql安裝目錄的許可權。

3,進到/www/mysql,建立系統內建的資料庫。

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql

4,添加服務,啟動MySQL

cp support-files/mysql.server /etc/init.d/mysql
chkconfig mysql on
service mysql start  --啟動MySQL

5,設定root密碼

為了讓任何地方都能用mysql/bin下的命令,vim /etc/prifile

添加:

PATH=/www/mysql/bin:$PATH
export PATH

儲存後source /etc/profile

執行:

 

mysql -uroot  mysql> SET PASSWORD = PASSWORD(‘root‘);

 

 

設定root使用者的密碼為root。

6,為了支援遠端存取資料庫,執行;

 

mysql> grant all on *.* to [email protected]"%" identified by "xroot”;

mysql> flush privileges; //更新許可權

這樣就建立了一個使用者名稱為xroot,密碼為xroot的使用者,可以遠端存取資料庫。

四,安裝php

解壓並進入源碼:

 

#./configure --prefix=/www/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl --with-libxml-dir --with-zlib --enable-mbstring --with-mysql=/www/mysql --with-mysqli=/www/mysql/bin/mysql_config --enable-mysqlnd --with-pdo-mysql=/www/mysql --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-curl

然後make make install。接著需要做以下事:

1,整合nginx,啟動php

進到cd /www/php/etc/ 目錄下,拷貝php-fpm.conf.default 為php-fpm.conf。執行/www/php/sbin/php-fpm start 啟動php-fpm。

2,配置php.ini 

將安裝源碼裡的/www/software/php-5.6.14/php.ini-production 拷貝到php的安裝目錄的lib檔案夾下。

3,如果需要安裝curl擴充的話(上面的configure已經帶了),進到源碼ext/curl目錄下,保證電腦上已經安裝了curl和curl-devel,然後:

 a,/www/php/bin/phpize 以下,為了方便可以把這個目錄加到/etc/profile裡:

 

PATH=/www/php/bin:/www/mysql/bin:$PATH

export PATH

b,

./configure --with-curl --with-php-config=/www/php/bin/php-config

之後make make install,curl.so會產生在

/www/php/lib/php/extensions/no-debug-non-zts-20131226目錄下,然後編輯php.ini找到extension_dir和extension修改即可。

 

五,安裝svn配置post-commit

此步作用是代替ftp,方便開發人員開發並同步代碼。可以直接通過yum安裝即可。

# rpm -qa subversion  //檢查是否內建了低版本的svn

#yum remove subversion //卸載低版本的svn

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安裝svn

通過# svnserve --version驗證是否安裝成功。接下來就是建立倉庫並與web目錄同步。

1,mkdir -p /www/wwwsvn  此檔案夾就是svn倉庫. svnadmin create /www/wwwsvn 建立倉庫,執行上述命令後,可以發現裡面有conf, db,format,hooks, locks, README.txt等檔案,說明一個SVN庫已經建立。(ps:此處可以先通過svnserve -d -r /www/svndata 建立svn版本庫目錄,然後svnadmin在svndata目錄下建立倉庫)

2,配置使用者和密碼

在wwwsvn下進到conf檔案夾,裡面有三個檔案:authz  passwd  svnserve.conf均要編輯。

#vim passwd //設定使用者和密碼

[users]
# harry = harryssecret
# sally = sallyssecret
wangning=wangning
yanzi=yanzi

#vim authz  //設定許可權

 

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
auth-access = write
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file‘s location is relative to the directory containing
### this configuration file.
### If SASL is enabled (see below), this file will NOT be used.
### Uncomment the line below to use the default password file.
password-db = passwd
### The authz-db option controls the location of the authorization
### rules for path-based access control.  Unless you specify a path
### starting with a /, the file‘s location is relative to the the
### directory containing this file.  If you don‘t specify an
### authz-db, no path-based access control is done.
### Uncomment the line below to use the default authorization file.
authz-db = authz
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository‘s uuid.
realm = My First Repository

注意:上面這些有效行前面不能有空格。

3,啟動及停止svn

#svnserve -d -r /www/wwwwvn   //啟動svn

#killall svnserve    //停止

待啟動svn後,可以在外面測試了。

svn checkout svn://192.1.15.222 --username xxx

4,配置post-commit

經過上述配置後,svn的倉庫地址是/www/wwwsvn, 但是web的根目錄是/www/web,兩者不是一個目錄,無法svn push上來就看到作用。

a,首先在server的終端裡,#svn co svn://192.1.15.222 /www/web

記得將/www/web目錄許可權修改為www:www。

chown -R www:www /www/web

b, # cd /www/wwwsvn/hooks/,然後cp post-commit.tmpl post-commit  

vim post-commit,在裡面輸入:

 

export LANG=zh_CN.UTF-8

svn up --username yanzi --password yanzi /www/web/

chown -R www:www /www/web/

然後就一切ok了,在外面svn commit看看web目錄裡有麼有對應檔案吧!

ps:

1,svn up後面的名字和密碼是之前設的svn使用者。

2,上面up就是update的意思,按Git的意思來理解,就是有個倉庫A,然後建立了個B去跟蹤A,每次A有提交的時候,讓B也pull一下過來。在svn裡是update。

參考連結:

1. svn部分:http://www.cnblogs.com/davidgu/archive/2013/02/01/2889457.html 

http://www.cnblogs.com/zhoulf/archive/2013/02/02/2889949.html

2.lnmp部分:http://www.tuicool.com/articles/jqIb22

3.mysql部分:http://www.cnblogs.com/xiongpq/p/3384681.html

下載連結nginx + mysql + php:

http://yunpan.cn/cFQZxxhYeWxX8 (提取碼:7f93)

 

關於SVN的補充說明

1,一般建立目錄/www/wwwsvn為一級目錄,然後建立二級目錄/www/wwwsvn/project1作為倉庫放project1. 

命令:

svnadmin create /www/wwwsvn/project1

啟動svn:

svnserve -d -r /www/wwwsvn

這樣啟動的svn根目錄是wwwsvn,外側project1的地址是:svn:192.xx.xx.x/project1  這樣做的好處是可以建立多個project.在外面svn co svn:192.xx.xx.x/project1的時候也會建立project1檔案夾。

結論:如果希望自由控制項目的檔案名稱那就svnserve的時候直接把專案檔夾啟動,這樣svn checkout的時候先建好外面的檔案夾,然後進來checkout就行了。如果想直接checkout下來就帶有專案檔夾,那就svnserve的時候把外層目錄啟動。這樣在任意地方checkout的時候內建project1檔案夾。

2,關於post-commit

首先在伺服器的/a/apps/zhujibao/manager/public/路徑下,svn co svn:192.xx.xx.x/city52,這樣city52檔案夾就自動建立了。

其內容為:

 

export LANG=zh_CN.UTF-8

svn up --username yanzi --password yanzi /a/apps/zhujibao/manager/public/city52

chown -R www:www /a/apps/zhujibao/manager/public/city52

 

REPOS="$1"

REV="$2"

 

#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf

最後的那句一定要注釋掉!另外,要記得給post-commit增加可執行許可權

3,如果不設定post-commit,那麼你在外面提交了東西,在倉庫地址下是什麼東西都看不到的,因此這個post-commit是必須的!

CentOS源碼安裝搭建LNMP全過程(包括nginx,mysql,php,svn)【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.