源码编译lnmp(Nginx 1.8 + MySQL5.5 + PHP 5.3)

目录

    安装

    安装Nginx1.8

        安装pcre库

tar xzfpcre-8.36.tar.gz
cd pcre-8.36
./configure
make &&make install
cd ../

        创建运行Nginx用户

useradd -M -s/sbin/nologin www

        解压Nginx软件包,调整源码文件

tar xzf nginx-1.8.0.tar.gz
cd nginx-1.8.0
# Modify Nginxversion
sed -i 's@#defineNGINX_VERSION.*$@#define NGINX_VERSION     "1.2"@' src/core/nginx.h
sed -i 's@#defineNGINX_VER.*NGINX_VERSION$@#define NGINX_VER          "BWS/" NGINX_VERSION@'src/core/nginx.h
# close debug
sed -i's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc
./configure--prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module--with-http_spdy_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module--with-http_realip_module --with-http_flv_module
make &&make install

        调整环境变量

cat>>/etc/profile.d/nginx.sh<<EOF
exportPATH=$PATH:$NGINX_INSTALL_DIR/sbin
EOF
source/etc/profile.d/nginx.sh

        启动脚本

vim/etc/init.d/nginx
#!/bin/bash
#
# nginx - thisscript starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
#description:  Nginx is an HTTP(S) server,HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname:nginx
# config:      /usr/local/nginx/conf/nginx.conf 
# pidfile:     /var/run/nginx.pid
 
# Source functionlibrary.
./etc/rc.d/init.d/functions
 
# Sourcenetworking configuration.
./etc/sysconfig/network
 
# Check thatnetworking is up.
["$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename$nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
[ -f/etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep"configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`"]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'`]; then
           value=`echo $opt | cut -d"=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating"$value
               mkdir -p $value && chown-R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch$lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f$lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 3 
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case"$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
chmod  +x /etc/init.d/nginx
chkconfig  --add nginx
chkconfig  nginx on

        检查Nginx配置文件&并启动

/usr/local/nginx/sbin/nginx  -t
service nginx start

    安装MySQL5.5

        创建MySQL用户

groupadd  -r mysql
useradd  -g mysql -r -s /sbin/nologin  -M mysql

        解压缩(使用源码编译,未使用二进制包)

mkdir -pv/data/mydata
chown  -R mysql:mysql  /data/mydata/
tar zxvfmysql-5.5.43.tar.gz
cd mysql-5.5.43

        生成makefile文件

cmake .-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/data/mydata-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1-DENABLE_DTRACE=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci-DWITH_EMBEDDED_SERVER=1

        编译&编译安装

make -j `grepprocessor /proc/cpuinfo | wc -l`  && make install

        复制MySQL主配置文

cpsupport-files/my-large.cnf /etc/my.cnf
cp /etc/my.cnf/etc/my.cnf.default
:> /etc/my.cnf
cat >/etc/my.cnf << EOF
[client]
port = 3306
socket =/tmp/mysql.sock
 
[mysqld]
port = 3306
socket =/tmp/mysql.sock
 
basedir =/usr/local/mysql
datadir =/data/mydata
pid-file =/data/mydata/mysql.pid
user = mysql
bind-address =0.0.0.0
server-id = 1
 
skip-name-resolve
#skip-networking
back_log = 300
 
max_connections =1000
max_connect_errors= 6000
open_files_limit =65535
table_open_cache =128 
max_allowed_packet= 4M
binlog_cache_size= 1M
max_heap_table_size= 8M
tmp_table_size =16M
 
read_buffer_size =2M
read_rnd_buffer_size= 8M
sort_buffer_size =8M
join_buffer_size =8M
key_buffer_size =4M
 
thread_cache_size= 8
 
query_cache_type =1
query_cache_size =8M
query_cache_limit= 2M
 
ft_min_word_len =4
 
log_bin =mysql-bin
binlog_format =mixed
expire_logs_days =30
 
log_error =/data/mydata/mysql-error.log
slow_query_log = 1
long_query_time =1
slow_query_log_file= /data/mydata/mysql-slow.log
 
performance_schema= 0
 
#lower_case_table_names= 1
 
skip-external-locking
 
default_storage_engine= InnoDB
#default-storage-engine= MyISAM
innodb_file_per_table= 1
innodb_open_files= 500
innodb_buffer_pool_size= 64M
innodb_write_io_threads= 4
innodb_read_io_threads= 4
innodb_thread_concurrency= 0
innodb_purge_threads= 1
innodb_flush_log_at_trx_commit= 2
innodb_log_buffer_size= 2M
innodb_log_file_size= 32M
innodb_log_files_in_group= 3
innodb_max_dirty_pages_pct= 90
innodb_lock_wait_timeout= 120
 
bulk_insert_buffer_size= 8M
myisam_sort_buffer_size= 8M
myisam_max_sort_file_size= 10G
myisam_repair_threads= 1
 
interactive_timeout= 28800
wait_timeout =28800
 
[mysqldump]
quick
max_allowed_packet= 16M
 
[myisamchk]
key_buffer_size =8M
sort_buffer_size =8M
read_buffer = 4M
write_buffer = 4M
EOF

        复制MySQL启动脚本

cpsupport-files/mysql.server /etc/init.d/mysqld
chmod  +x /etc/init.d/mysqld
chkconfig  --add mysqld
chkconfig    mysqld on

        MySQL数据库初始化

/usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mydata/ --basedir=/usr/local/mysql/

        调整环境变量

cat >>/etc/profile.d/mysql.sh<<EOF
> exportPATH=$PATH:/usr/local/mysql/bin
> EOF

        输出MySQL头文件

ln -sv/usr/local/mysql/include /usr/include/mysql

        输出MySQL库文件

echo'/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig  -v > /dev/null 2>&1

        修改MySQL默认密码,并删除匿名用户

/usr/local/mysql/bin/mysql-e "grant all   privileges on *.*to  root@'127.0.0.1' identified by\"DBPASSWD\" with  grant option"
/usr/local/mysql/bin/mysql -e "grantall   privileges on *.* to  root@'localhost' identified by\"DBPASSWD\" with  grantoption"
/usr/local/mysql/bin/mysql�Ce 
/usr/local/mysql/bin/mysql-uroot -pDBPASSWD -e "delete from mysql.user where Password='';"
/usr/local/mysql/bin/mysql-uroot -pDBPASSWD -e "delete from mysql.db where User='';"
/usr/local/mysql/bin/mysql-uroot -pDBPASSWD -e "delete from mysql.proxies_priv whereHost!='localhost';"
/usr/local/mysql/bin/mysql-uroot -pDBPASSWD -e "drop database test;"
/usr/local/mysql/bin/mysql-uroot -pDBPASSWD -e "reset master;"

        启动MySQL

service mysqld start

    安装PHP5.3

         安装PHP的依赖包

tar zxvflibiconv-1.14.tar.gz
cd libiconv-1.14
./configure--prefix=/usr/local
make &&make install
cd ../
tarxzf libmcrypt-2.5.8.tar.gz
cdlibmcrypt-2.5.8
./configure
make&& make install
ldconfig
cdlibltdl/
./configure--enable-ltdl-install
make&& make install
cd../../
tarxzf mhash-0.9.9.9.tar.gz
cdmhash-0.9.9.9
./configure
make&& make install
cd../
/bin/rm-rf mhash-0.9.9.9
echo'/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
echo'/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig-v > /dev/null 2>&1
./configure
make&& make install
cd../
tarzxvf mcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8
ldconfig
./configure
make&& make install

        安装PHP,打补丁包

tar xzfphp-5.3.29.tar.gz
patch -dphp-5.3.29 -p0 < fpm-race-condition.patch
cd php-5.3.29

        生成makefile文件

CFLAGS= CXXFLAGS= ./configure--prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc--with-fpm-user=www --with-fpm-group=www --enable-fpm --disable-fileinfo--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd--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-bcmath--enable-shmop --enable-exif --enable-sysvsem --enable-inline-optimization--with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl--enable-sockets --with-xmlrpc --enable-ftp --with-gettext --enable-zip--enable-soap --disable-ipv6 --disable-debug

        编译&编译安装

makeZEND_EXTRA_LIBS='-liconv'
make test (可以不以执行)
make install

        输出PHP环境变量到全局

cat >>/etc/profile.d/php.sh<<EOF
> exportPATH=$PATH:/usr/local/php/bin
> EOF

        复制PHP-FPM启动脚本

cpsapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x/etc/init.d/php-fpm
chkconfig --addphp-fpm
chkconfig php-fpmon

        配置PHP-FPM配置文件

cat >/usr/local/php/etc/php-fpm.conf <<EOF
;;;;;;;;;;;;;;;;;;;;;
; FPMConfiguration ;
;;;;;;;;;;;;;;;;;;;;;
 
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
 
[global]
pid =run/php-fpm.pid
error_log =log/php-fpm.log
log_level =warning 
 
emergency_restart_threshold= 30
emergency_restart_interval= 60s 
process_control_timeout= 5s
daemonize = yes
 
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions;
;;;;;;;;;;;;;;;;;;;;
 
[www]
listen =/dev/shm/php-cgi.sock
listen.backlog =-1
listen.allowed_clients= 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
 
pm = dynamic
pm.max_children =12
pm.start_servers =8
pm.min_spare_servers= 6
pm.max_spare_servers= 12
pm.max_requests =2048
pm.process_idle_timeout= 10s
request_terminate_timeout= 120
request_slowlog_timeout= 0
 
slowlog =log/slow.log
rlimit_files =51200
rlimit_core = 0
 
catch_workers_output= yes
env[HOSTNAME] =$HOSTNAME
env[PATH] =/usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
> EOF

        复制PHP主配置文件

cp php.ini-production /usr/local/php/etc/php.ini

(备注:这里使用scoket连接,也可以使用TCP连接 

        启动PHP-FPM服务

service php-fpm start
ls -lh /dev/shm/
total 0
srw-rw-rw-. 1 wwwwww 0 May  4 23:48 php-cgi.sock

        调整Nginx主配置文件,使其解析PHP脚本

user www www;
worker_processes4;
 
error_log/data/weblogs/error_nginx.log crit;
pid/var/run/nginx.pid;
worker_rlimit_nofile51200;
 
events {
        use epoll;
        worker_connections 51200;
        }
 
http {
        include mime.types;
        default_type application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile on;
        tcp_nopush on;
        keepalive_timeout 120;
        server_tokens off;
        tcp_nodelay on;
 
        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 Compression
        gzip on;
        gzip_buffers 16 8k;
        gzip_comp_level 6;
        gzip_http_version 1.1;
        gzip_min_length 256;
        gzip_proxied any;
        gzip_vary on;
        gzip_types
            text/xml application/xmlapplication/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
            text/javascriptapplication/javascript application/x-javascript
            text/x-json application/jsonapplication/x-web-app-manifest+json
            text/css text/plaintext/x-component
            font/opentypeapplication/x-font-ttf application/vnd.ms-fontobject
            image/x-icon;
        gzip_disable  "msie6";
 
        open_file_cache max=1000 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 2;
        open_file_cache_errors on;
 
        server {
        listen 80;
        server_name _;
        access_log/data/weblogs/access_nginx.log combined;
        location / {
        root /data/wwwroot/htdocs;
        index index.php index.html index.htm;
        }
        if ( $query_string ~*".*[\;'\<\>].*" ){
                return 404;
                }
 
        location ~ .*\.(php|php5)?$ {
                root    /data/wwwroot/htdocs;
                #fastcgi_passremote_php_ip:9000;
                fastcgi_passunix:/dev/shm/php-cgi.sock;
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                fastcgi_index index.php;
                include fastcgi.conf;
                }
 
        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                expires 30d;
               }
 
        location ~ .*\.(js|css)?$ {
                expires 7d;
                }
        }
 
}

FAQ

        尝试解析PHP文件

wKioL1W0kkLg1RJIAALH1uTn1Vk835.jpg

        尝试连接数据库

<?php
$conn =mysql_connect("localhost",'root','DBPASSWD') or die("ConnectError:".mysql_error());
echo "MySQL connect is OK!";
?>

wKiom1W0kGzhX-MGAABq_3EIbgk364.jpg

你可能感兴趣的:(LNMP)