lxy777 发表于 2015-8-21 10:35:42

ubuntu11.10 搭建lnmp服务器

  本文主要根据http://www.rocknoon.com/ubuntu-lnmp-mysql/ 及张宴的《实战nginx:取代Apache的高性能web服务器》 and 下面在安装时configure后面的参数请手动将 – 改成- -
  首先下载所需要的软件

  wget http://nginx.org/download/nginx-1.0.10.tar.gz

  (ps:原来的版本是0.8.46,好几年前的一个版本,我在后面编译安装的时候出现了
  src/core/ngx_resolver.c: In function ‘ngx_resolver_process_ptr’:
src/core/ngx_resolver.c:1425:43: error: variable ‘qclass’ set but not used
[-Werror=unused-but-set-variable]
src/core/ngx_resolver.c:1425:36: error: variable ‘qtype’ set but not used
[-Werror=unused-but-set-variable]
cc1: all warnings being treated as errors
make: *** Error 1
make: Leaving directory `/usr/local/src/nginx-1.0.0′
make: *** Error 2这个错误,这应该是一个BUG,网上搜了下,说要这个打这个补丁,可是我打上了却没有用,换成这个最新的稳定版就顺利编译通过了)

  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://ghostscript.com/releases/ghostscript-9.00.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz

  然后更新下apt源,官方的源下载太慢了,如何更新自行谷歌。。
  1.安装mysql
  需要用到下面两个lib

  apt-get install libncurses5-dev
  apt-get install g++
  tar xzvf mysql-5.5.3-m3.tar.gz
  #config mysql
./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 –with-plugins=partition,innobase,myisammrg

  ps:这里有一个bug error do_abi_check 需要手动修改 makefile 里边注释掉 do_abi_check 后的内容。

  make
  make install
  #创建mysql用户
groupadd mysql
useradd -g mysql mysql
  #创建log和data文件夹
mkdir /usr/local/mysql/data/
mkdir /usr/local/mysql/log/
mkdir /usr/local/mysql/log/binlog
mkdir /usr/local/mysql/log/relaylog
chown -R mysql:mysql /usr/local/mysql/

  #初始化数据表
/usr/local/mysql/bin/mysql_install_db –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data/ –user=mysql
  #编写mysql配置
  vim /usr/local/mysql/my.cnf





character-set-server = utf8
port = 3306
socket = /tmp/mysql.sock

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

quick
max_allowed_packet = 32M  #创建管理mysql数据库的shell脚本
  vim /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  #赋予可执行权限及启动mysql
  chmod +x /etc/init.d/mysql
  /etc/init.d/mysql start
  #为刚刚在my.cnf中的root建立账户密码
  /usr/local/mysql/bin/mysqladmin -u root password ‘root’
  2.编译安装PHP(FastCGI模式)
  #首先安装编译时所需的工具及基本库
  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
sudo apt-get install libperl-dev
  然后编译安装PHP 5.2.14所需的支持库
  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 http://www.iyunv.com/
  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 ../
  #编译安装fastcgi php
  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-xmlrpc –enable-zip –enable-soap
  ps:(这边原来的编译选项里面有–with-ldap –with-ldap-sasl,可是加上这两个选项后会出现一个错误configure: error: LDAP build check failed. Please check config.log for more information. 这似乎是一个BUG,不知道怎么解决,无奈只能去掉这两个参数)
  make ZEND_EXTRA_LIBS=’-liconv
  (这步又遇到一个错误:undefined reference to `SSLv2_server_method’,参考了这篇文章,打上了这个补丁,终于顺利的过了)
  make install
  cp php.ini-dist /usr/local/php/etc/php.ini
  #编译安装PHP5扩展模块
  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 ../
  (这边原来要安装一个ghostscript,可是在make的时候出现
  ./base/time_.h:49:8: error: redefinition of ‘struct timeval’

/usr/include/i386-linux-gnu/bits/time.h:75:8: note: originally defined here
./base/gp_unix.c: In function ‘gp_get_realtime’:
./base/gp_unix.c:148:9: warning: implicit declaration of function ‘gettimeofday’ [-Wimplicit-function-declaration]
make: *** Error 1找了下没找到解决方法,我又不知道有什么用,就把它给忽略了)
  #修改php.ini文件
  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”

extension = “imagick.so”

(以上可以用sed命令修改)





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 可根据自身情况编写不同值):



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. vim /etc/sysctl.conf 添加kernel.shmmax=128000000 , 使这个配置文件生效 sysctl -p 。这个参数在这有介绍


e. 创建www用户和组 及修改php-fpm配置文件(php-fpm是为PHP 打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无须重启php-cgi)

groupadd www

useradd -g www www





  vim /usr




r/local/php/etc/php-fpm.conf 将其改成www来跑php即;<value name=”user”>www</value>和 <value name=”group”>www</value>  如果安装Nginx+PHP用于调试程序,将<value name=”display_errors”>0</value>改成<value name=”display_errors”>1</value>,以便显示PHP错误信息,否则Nginx会报状态为500的错误页


#启动php-fpm

/usr/local/php/sbin/php-fpm start (ps:修改php-ini后不重启php-cgi。重新加载配置文件使用reload)

3. nginx的安装

#安装所需的pcre库

  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
  vim /usr/local/nginx/conf/nginx.conf


userwww www;

#开启的nginx进程个数

worker_processes8;

error_log   /usr/local/nginx/logs/error.log crit;

#error_loglogs/error.lognotice;

#error_loglogs/error.loginfo;

pid      /usr/local/nginx/nginx.pid;

events {

    use epoll;

    worker_connections1024;

}

http {

    include       mime.types;

    default_typeapplication/octet-stream;

    #log_formatmain’$remote_addr – $remote_user [$time_local] “$request” ‘

    #                  ’$status $body_bytes_sent “$http_referer” ‘

    #                  ’”$http_user_agent” “$http_x_forwarded_for”‘;

    #access_loglogs/access.logmain;

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

    keepalive_timeout65;

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

    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_name127.0.0.1 localhost;

      index index.html index.htm index.php;

      root/var/www/;

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

      {

                #fastcgi_passunix:/tmp/php-cgi.sock;

                fastcgi_pass127.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_formataccess’$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.logaccess;

    }

}  #配置fcgi.conf
  我看了下,似乎书上的配置文件和原来的一模一样的, 所以这里就不贴了
  #启动Nginx
  ulimit -SHn 65535
  /usr/local/nginx/sbin/nginx
  4.设置开机自启动 Nginx+PHP+Mysql




在/etc/rc.local末尾加上 ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/etc/init.d/mysql start
/usr/local/nginx/sbin/nginx  来源:http://zscorpio.com/?p=717
页: [1]
查看完整版本: ubuntu11.10 搭建lnmp服务器