前言:本文是根据blog.s135.com [搭建胜过Apache十倍的Web服务器] 为基础,主要讲解如何在最新版本Ubuntu下部署 FastCGI PHP + Mysql + Nginx 环境的搭建。全文分文三篇 mysql , php , nginx. 这是第一篇主要讲如何部署安装mysql。
本文用到的所有软件可以到blog.s135.com进行下载
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz
1. 首先要做的是对apt源的更新,这里我比较推荐sohu源
sudo etc/apt/source.list
deb http://mirrors.sohu.com/ubuntu/ maverick main restricted
deb-src http://mirrors.sohu.com/ubuntu/ maverick main restricted
deb http://mirrors.sohu.com/ubuntu/ maverick-updates main restricted
deb-src http://mirrors.sohu.com/ubuntu/ maverick-updates main restricted
deb http://mirrors.sohu.com/ubuntu/ maverick universe
deb-src http://mirrors.sohu.com/ubuntu/ maverick universe
deb http://mirrors.sohu.com/ubuntu/ maverick-updates universe
deb-src http://mirrors.sohu.com/ubuntu/ maverick-updates universe
deb http://mirrors.sohu.com/ubuntu/ maverick multiverse
deb-src http://mirrors.sohu.com/ubuntu/ maverick multiverse
deb http://mirrors.sohu.com/ubuntu/ maverick-updates multiverse
deb-src http://mirrors.sohu.com/ubuntu/ maverick-updates multiverse
sudo apt-get update
2. 第二部我们要进行Mysql安装
#下边是俩个需要用到的lib
sudo apt-get install libncurses5-dev
sudo apt-get install g++
tar xzvf mysql-5.5.3-m3.tar.gz
#这里我只开启了对MyISAM的支持,如果需要innoDB请阅读mysql官方文档,对innoDB支持进行配置
sudo ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile
(notice: 在使用gcc4.5版本遇到一个bug error do_abi_check 需要手动修改 makefile 里边注释掉 do_abi_check 后的内容。)
sudo make
sudo make install
#创建mysql用户
sudo groupadd mysql
sudo useradd -g mysql mysql
#创建log和data文件夹
sudo mkdir usr/local/mysql/data/
sudo mkdir usr/local/mysql/log/
sudo mkdir usr/local/mysql/log/binlog
sudo mkdir usr/local/mysql/log/relaylog
sudo chown -R mysql:mysql usr/local/mysql/
#初始化数据表
sudo ./mysql_install_db –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data/ –user=mysql
#编码mysql配置 以下配置可以根据自己服务器环境进行配置调优
sudo vim /usr/local/mysql/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 = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/log/mysql_error.log
pid-file = /usr/local/mysql/mysql.pid
open_files_limit = 2000
back_log = 600
max_connections = 1000
max_connect_errors = 2000
table_cache = 100
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 20
#thread_concurrency = 8
query_cache_size = 128M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-UNCOMMITTED
tmp_table_size = 32M
max_heap_table_size = 64M
long_query_time = 2
log-slave-updates
log-bin = /usr/local/mysql/log/binlog
binlog_cache_size = 2M
binlog_format = MIXED
max_binlog_cache_size = 4M
max_binlog_size = 512M
relay-log-index = /usr/local/mysql/log/relaylog
relay-log-info-file = /usr/local/mysql/log/relaylog
relay-log = /usr/local/mysql/log/relaylog
expire_logs_days = 30
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
[mysqldump]
quick
max_allowed_packet = 32M
#编写mysql启动shell
/etc/init.d/mysql
#!/bin/sh
mysql_port=3306
mysql_username="root"
mysql_password="root"
function_start_mysql()
{
printf "Starting MySQL...\n"
sudo /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
sudo /usr/local/mysql/bin/mysqladmin -u${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
}
if [ "$1" = "start" ]; then
function_start_mysql
elif [ "$1" = "stop" ]; then
function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_stop_mysql
else
printf "Usage: mysql-server {start|stop|restart|kill}\n"
fi
3.第三步我们就要开始 FastCGI PHP 的安装
#首先是安装FastCGI PHP编译所需要的基本库
sudo apt-get install patch;
sudo apt-get install libxml2-dev
sudo apt-get install libssl-dev
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libjpeg62-dev
sudo apt-get install libpng12-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libsasl2-dev
sudo apt-get install autoconf
#编译安装一些非 apt-get 安装的库
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure –prefix=/usr/local
sudo make
sudo make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
sudo make
sudo make install
sudo /sbin/ldconfig
cd libltdl/
./configure –enable-ltdl-install
sudo make
sudo make install
cd ../../
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
sudo make
sudo make install
cd ../
sudo ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
sudo ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
sudo ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
sudo ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
sudo ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
sudo ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
sudo ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
sudo ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
sudo ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
sudo ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
sudo /sbin/ldconfig
./configure
sudo make
sudo make install
cd ../
# fast-cgi php 5.2 的编译与安装
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
cd php-5.2.14/
./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-fpm –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
sudo make ZEND_EXTRA_LIBS=’-liconv’
sudo make install
sudo cp php.ini-dist /usr/local/php/etc/php.ini
#接下来就是 安装 php 拓展
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
cd ../
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
cd ../
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql
sudo make
sudo make install
cd ../
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
sudo make
sudo make install
cd ../
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
cd ../
wuxinxinggg补充:configure imagick时会提示找不到MagickWang的路径,按照如下操作就OK 了
Are you trying to install the PHP imagick extension but keep getting this error?
Besides the imagick extension there are 2 packages you need: imagemagick and libmagick9-dev.
apt-get install imagmagick libmagick9-dev
pecl install imagick
Don’t forget to add “extension=imagick.so” to your php.ini file and you should be ready to go.
#对PHP拓展进行配置
sudo vim /usr/local/php/etc/php.ini
a.查找/usr/local/php/etc/php.ini中的extension_dir = "./" 修改为 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 并在此行后增加以下几行,然后保存: extension = "memcache.so" extension = "pdo_mysql.so"
b.再查找output_buffering = Off 修改为output_buffering = On
c.配置eAccelerator加速PHP: sudo mkdir -p /var/eaccelerator_cache sudo vim /usr/local/php/etc/php.ini 按shift+g键跳到配置文件的最末尾,加上以下配置信息(shm_size 可根据自身情况编写不同值): [eaccelerator] zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" eaccelerator.shm_size="16" eaccelerator.cache_dir="/var/eaccelerator_cache" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="3600" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"
d.在ubuntu下还需要配置一条东西 但忘记了什么用 sudo vim /etc/sysctl.conf kernel.shmmax = 128000000 sudo sysctl -p
e.配置fpm sudo vim /usr/local/php/etc/php-fpm.conf 这里主要对俩个东西进行配置 www www 设置由谁来跑PHP进行,我一般会创建一个www 用户来专门跑nginx 和 php 进程 还有一个最重要的参数 开启FastCGI 的进行数 这个需要根据不同服务器情况进行不同设置。 128
#启动 php-fpm
sudo /usr/local/php/sbin/php-fpm start
#为root 用户 建立密码
sudo ./mysqladmin -u root password ‘root’
#启动mysql
sudo /etc/init.d/mysql start
#安装所需要库
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
sudo make
sudo make install
cd ../
sudo ln -s /usr/local/lib/libpcre.so.0 /usr/lib/libpcre.so.0
#编译安装 Nginx
tar zxvf nginx-0.8.46.tar.gz
cd nginx-0.8.46/
./configure –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
make
sudo make install
cd ../
#对nginx进行配置
sudo vim /usr/local/nginx/conf/nginx.conf
user www www;
#开启的nginx进程个数
worker_processes 8;
error_log /usr/local/nginx/logs/error.log crit;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/nginx.pid;
events {
worker_connections 1024;
}
http {
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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name 127.0.0.1 localhost;
index index.html index.htm index.php;
root /var/www/;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/nginx/logs/localhost.log access;
}
}
sudo vim /usr/local/nginx/conf/fastcgi.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;
#启动和平滑reload
sudo /usr/local/nginx/sbin/nginx
sudo /usr/local/nginx/sbin/nginx -s reload
#最后编写自动启动脚本
sudo vim /etc/rc.local
/etc/init.d/mysql start
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx