程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> Centos 下 Nginx+php5.3+mysql環境搭建

Centos 下 Nginx+php5.3+mysql環境搭建

編輯:關於MYSQL數據庫

一、CentOS准備工作.

# 更新所有已安裝軟件包
yum -y update # 安裝必要的開發工具
yum -y install gcc gcc-c++ autoconf make libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clIEnts openldap-servers libxml2 libXML2-devel patch pcre-devel# 上面安裝的東東,像gcc, make, autoconf是必要的編譯工具
# 像libjpeg,freetype,zlib等,編譯PHP時用得到
# 像patch, libXML2等,在使用php-fpm對PHP打補頂時用得著
# 像pcre-dev等,在編譯Nginx服務器時用得著

二、編譯安裝Nginx服務器.
0. 確保安裝了如下軟件.

yum install gcc openssl-devel pcre-devel zlib-devel

1. 創建nginx運行的用戶.

groupadd nginx
useradd nginx -g nginx

2. 創建網頁文件存儲目錄.

mkdir /var/www
chmod +w /var/www
chown -R nginx:nginx /var/www

3. 下載Nginx源碼包【Nginx官方維基】.

cd /work/soft
wget http://sysoev.ru/nginx/nginx-0.8.33.tar.gz
tar -zxvf nginx-0.8.33.tar.gz
cd nginx-0.8.33
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.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_gzip_static_module \
--http-log-path=/var/log/nginx/Access.log \
--http-client-body-temp-path=/var/tmp/nginx/clIEnt/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fCGI/ \
--with-http_stub_status_module
make && make install

# with-http_stub_status_module 模塊可用來統計當前連接數 【更多Nginx模塊】
# 添加指定的 Nginx 擴展模塊只需要 configure 時帶上 --with-模塊名 即可
# 小技巧:如已經安裝好了Nginx,好添加一個新模塊,只需要重新配置,重新 configure && make 但別 make install, 直接將obJS/nginx 拷貝到{$prefix}/sbin下即可,【注意備份原來的】
4. 創建nginx需要的文件/文件夾.

mkdir -p /var/tmp/nginxvi /work/webServer/nginxStart.sh
#!/bin/sh
/usr/sbin/nginx vi /work/webServer/nginxRestart.sh
#!/bin/sh
killall -9 nginx
/usr/sbin/nginxchmod +x /work/webServer/nginxStart.sh
chmod +x /work/webServer/nginxRestart.sh

5. 啟動 nginx.

/usr/sbin/nginx

/work/webServer/nginxStart.sh

6. 訪問一下看看. 看到 Welcome to nginx! 安裝便算OK了!三、編譯安裝MySQL.

cd /work/soft
/usr/sbin/groupadd MySQL
/usr/sbin/useradd -g mysql MySQL

# 自己看看哪個下載的速度好就用哪個吧,由於我的服務器在國外,國內的下載反而慢一些
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.0-m2.tar.gz/from/http://opensource.become.com/MySQL/
tar zxvf MySQL-5.5.0-m2.tar.gz
cd MySQL-5.5.0-m2/# 國內s135的服務器,當然你也可以去s135.com上看看,那裡的文章一直以來我都認為挺不錯的
wget http://blog.s135.com/soft/Linux/nginx_PHP/mysql/MySQL-5.5.2-m2.tar.gz
tar zxvf MySQL-5.5.2-m2.tar.gz
cd MySQL-5.5.2-m2/

./configure --prefix=/work/webServer/MySQL/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-clIEnt --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make && make install
chmod +w /work/webServer/MySQL
chown -R mysql:mysql /work/webServer/MySQL

①、創建MySQL數據庫存放目錄

mkdir -p /work/webServer/MySQLData/data/
mkdir -p /work/webServer/MySQLData/binlog/
mkdir -p /work/webServer/MySQLData/relaylog/
chown -R mysql:mysql /work/webServer/MySQLData/

②、以MySQL用戶帳號的身份建立數據表:

/work/webServer/mysql/bin/mysql_install_db --basedir=/work/webServer/mysql --datadir=/work/webServer/mysqlData/data --user=MySQL

③、創建my.cnf配置文件: vi /work/webServer/my.cnf

輸入以下內容:

[clIEnt]
character-set-server = utf8
port    = 3306
socket  = /tmp/mysql.sock[MySQLd]
character-set-server = utf8
replicate-ignore-db = MySQL
replicate-ignore-db = test
replicate-ignore-db = information_schema
user    = MySQL
port    = 3306
socket  = /tmp/MySQL.sock
basedir = /work/webServer/MySQL
datadir = /work/webServer/MySQLData/data
log-error = /work/webServer/mysqlData/MySQL_error.log
pid-file = /work/webServer/mysqlData/MySQL.pid
open_files_limit    = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /work/webServer/MySQLData/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /work/webServer/MySQLData/relaylog/relaylog
relay-log-info-file = /work/webServer/MySQLData/relaylog/relaylog
relay-log = /work/webServer/MySQLData/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recoverinteractive_timeout = 120
wait_timeout = 120skip-name-resolve
master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396#master-host     =   192.168.1.2
#master-user     =   username
#master-password =   passWord
#master-port     =  3306server-id = 1innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0#log-slow-querIEs = /work/webServer/MySQLData/slow.log
#long_query_time = 10[MySQLdump]
quick
max_allowed_packet = 32M

④、創建管理MySQL數據庫的shell腳本: vi /work/webServer/MySQL.sh

輸入以下內容 #!/bin/sh
printf "Starting MySQL...\n"
/bin/sh /work/webServer/mysql/bin/MySQLd_safe --defaults-file=/work/my.cnf 2>&1 > /dev/null &

⑤、賦予shell腳本可執行權限:

mkdir -p /work/webServer/MySQL/var/
chmod +x /work/webServer/MySQL.sh

⑥、啟動MySQL

/work/webServer/MySQL.sh

⑦、通過命令行登錄管理MySQL服務器(提示輸入密碼時直接回車):

/work/webServer/mysql/bin/mysql -u root -p -S /tmp/MySQL.sock

⑧、輸入以下SQL語句,創建一個具有root權限的用戶

GRANT ALL PRIVILEGES ON *.* TO 'li'@'localhost' IDENTIFIED BY '111111' with grant option;

⑨、(可選)停止MySQL

/work/webServer/mysqlData/MySQL stop


四、編譯安裝PHP (FastCGI模式)使用PHP-fpm管理方式.

1.安裝 libiconv.

選擇最新的, 解包,安裝 http://FTP.gnu.org/pub/gnu/libiconv/cd /work/soft
wget http://FTP.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr && make && make install

2.安裝mhash.

cd /work/soft
wget http://blog.s135.com/soft/Linux/nginx_PHP/mhash/mhash-0.9.9.9.tar.gz
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr && make && make install

<<上面三個庫也可以直接使用 yum install 安裝,注意安裝 devel版即可>>
## 即 yum install libmcrypt-devel libmcrypt-devel mhash-devel

3.安裝mcrypt.

cd /work/soft
wget http://blog.s135.com/soft/Linux/nginx_PHP/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure && make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install && make && make installcd /work/soft
wget http://blog.s135.com/soft/Linux/nginx_PHP/mcrypt/mcrypt-2.6.8.tar.gz
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure --prefix=/usr && make && make install

4. 編譯安裝libevent.

下載 http://www.monkey.org/~provos/libevent/
[建議不要使用yum的方式安裝 libevent,php-fpm建議Libevent 1.4.12-stable or higher is recommended, and at least libevent 1.4.3-stable is required,因此PHP-fpm需要1.4.3以上版本libevent的支持,所以去libevent的官網下最穩定版的libevent源碼包進行編譯安裝]

cd /work/soft
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
tar zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/usr && make && make install

4. 下載PHP源碼包.

cd /work/soft
wget http://cn.PHP.Net/get/PHP-5.3.1.tar.gz/from/this/mirror
tar -zxvf PHP-5.3.1.tar.gz

5. 下載PHP-fpm.

# 參考文檔【http://PHP-fpm.org/wiki/Documentation】
# 說明:根據php-fpm.org官網上所說,在PHP 5.3.2+以後,php-fpm將包含到php core中,不再需要打補丁了,對於目前的5.3.1還是需要通過補丁擴展PHP-fpm功能。
# 下載

cd /work/soft
wget http://launchpad.Net/php-fpm/master/0.6/+download/PHP-fpm-0.6~5.3.1.tar.gz
tar -zxvf PHP-fpm-0.6~5.3.1.tar.gz

# 生成補丁

PHP-fpm-0.6-5.3.1/generate-fpm-patch
cd PHP-5.3.1

# 對php源碼打上PHP-fpm的補丁()

patch -p1 < ../fpm.patch

./buildconf --force
mkdir fpm-build
cd fpm-build

# 特別注意以下的配置參數
# 特別注意
# --enable-fastCGI \
# !!!! 不是 --enable-fpm 而是 --with-fpm
# --with-fpm \
# --with-libevent=/usr/lib\
# 這三項,第一個是開啟fastCGI, 第二個是開啟 php-fpm,第三個是指定PHP-fpm所需要的libevent的位置

../configure --prefix=/usr \
--with-config-file-path=/etc/PHP5 \
--with-mysql=/work/webServer/MySQL \
--with-mysqli=/work/webServer/mysql/bin/MySQL_config \
--with-iconv-dir=/usr \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libXML-dir=/usr \
--enable-XML \
--enable-discard-path \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fastCGI \
--with-fpm \
--with-libevent=/usr/lib\
--enable-force-CGI-redirect \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-XMLrpc \
--enable-zip \
--enable-soap \
--without-pear

make clean
make ZEND_EXTRA_LIBS='-liconv'
make install
# 應當可以看到這一行
# Installing PHP SAPI module: fpm
# …
# 並且存在 /usr/bin/PHP-fpm 即代表安裝成功

cd ..
mkdir /etc/PHP5
cp php.ini-production /etc/php5/PHP.ini

安裝PEAR
curl http://pear.PHP.Net/go-pear | /usr/bin/PHP

啟動PHP-fpm
/etc/init.d/PHP-fpm start

五、配置與優化.
添加開機服務:

vim /etc/rc.local/usr/sbin/nginx
/work/webServer/MySQL.sh
/etc/init.d/PHP-fpm start

重啟命令:

killall -9 nginx

/usr/sbin/nginx


大概說明:

現在,Nginx,MySQL, FastCGI模式的PHP都已經安裝完畢了,需要進行的工作是配置優化,首先熟悉一下配置文件的位置

Nginx的配置文件在 /etc/nginx 下面
PHP的配置文件,即熟悉的php.ini 在 /etc/PHP5 中
php-fpm的配置文件為 /etc/PHP-fpm.conf
MySQL 的配置文件為 /etc/my.cnf

Nginx日志文件在 /var/log/nginx 下面
php-fpm日志文件在 /var/log/PHP-fpm.log
[一定要經常查看日志記錄,找出系統潛在的問題]

附常見錯誤1:
No input file specifIEd.
注意如下兩個參數的值
fastcgi_param SCRIPT_FILENAME $document_root$fastCGI_script_name;
fastcgi_param SCRIPT_NAME $fastCGI_script_name;

建立一些日志文件:

vi /work/webServer/NginxAccess.log

Nginx.conf例子: #user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;events 
{
    worker_connections  1024;
}
http 
{
# 設定mime類型
    include mime.types;
    default_type application/octet-stream;
# 設定日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
# 設定Access log
    access_log /work/webServer/NginxAccess.log main;
    sendfile on;
    keepalive_timeout 65;    server 
    {
        listen      80;
        server_name *.vcn2008.com;
charset UTF-8;        location / 
        {
            root /work/vcn2008.com;
            index index.Html index.htm index.PHP;
        }        location ~ \.PHP$ 
        {
            root           /work/vcn2008.com;
            fastCGI_pass   127.0.0.1:9000;
            fastCGI_index  index.PHP;
            fastcgi_param  SCRIPT_FILENAME  /work/vcn2008.com$fastCGI_script_name;
            include        fastCGI_params;
        }
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved