Skip to main content

Lnmp环境超详细搭建教程[Nginx(1.6)+Mysql(5.6)+PHP(5.6)]

一、准备工作

1.清理已经安装过得安装包:

 yum -y remove httpd
 yum -y remove mysql
 yum -y remove php

2.搜索apache包

 rpm -qa http*

3.检查是否卸载干净

 rpm -qa|grep http*

4.关闭SELINUX:

 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
 setenforce 0

5.安装开发包:

 yum -y groupinstall "Desktop Platform Development" "development tools"

二、安装Nginx

1. 安装nginx依赖包

nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法:

 yum -y install pcre pcre-devel

nginx的各种模块中需要使用gzip压缩:

 yum -y install zlib zlib-devel

安全套接字层密码库:

 yum -y install openssl openssl-devel

2.创建nginx守护用户

 groupadd -r nginx
 useradd -r -g nginx nginx

3.下载报文头部修改工具

 地址:https://github.com/openresty/headers-more-nginx-module/tags
 wget -P /data/ https://github.com/openresty/headers-more-nginx-module/archive/v0.32.tar.gz
 tar xf /data/v0.32.tar.gz

4.nginx下载并安装

wget -P /data http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf /data/nginx-1.6.3.tar.gz
接着开始编译和安装:
  ./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.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_stub_status_module \
  --with-http_gzip_static_module \
  --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/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre \
  --add-module=/data/headers-more-nginx-module-0.32/
  make && make install

5.创建启动脚本

新建文件/etc/rc.d/init.d/nginx,内容如下:
#!/bin/sh
#
# nginx - this script 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:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/nginx.lock
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   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 1
    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/rc.d/init.d/nginx

 添加至服务管理列表,并让其开机自动启动:
 chkconfig --add nginx
 chkconfig nginx on

 而后就可以启动服务并测试了:
 service nginx start

6.添加到环境变量

 vim /etc/profile.d/nginx.sh
 export PATH=/usr/local/nginx/sbin:$PATH
 . /etc/profile.d/nginx.sh
 hash -r

三、安装Mysql数据库

1.安装数据编译依赖

yum -y install cmake ncurses-devel   

2.新建用户守护进程

 groupadd -r mysql
 useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

3.创建文件系统

 mkdir /mydata/data
 chown -R mysql:mysql /mydata/data

4.安装并初始化

 tar xf m mysql-5.6.13.tar.gz 
 cd mysql-5.6.13
 cmake \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/mydata/data \
 -DSYSCONFDIR=/etc \
 -DWITH_MYISAM_STORAGE_ENGINE=1 \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DWITH_MEMORY_STORAGE_ENGINE=1 \
 -DWITH_READLINE=1 \
 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
 -DMYSQL_TCP_PORT=3306 \
 -DENABLED_LOCAL_INFILE=1 \
 -DWITH_PARTITION_STORAGE_ENGINE=1 \
 -DEXTRA_CHARSETS=all \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DMYSQL_USER=mysql \
 -DWITH_DEBUG=0 \
 -DWITH_SSL=system
 make && make install

5.创建配置文件

 cd /usr/local/mysql
 cp support-files/my-default.cnf /etc/my.cnf

6.修改配置文件

 vim /etc/my.cnf
 datadir = /mydata/data           #数据存储目录
 skip_name_resolve = ON         #跳过域名解析
 innodb_file_per_table = ON     #innodb文件整理

7.数据初始化

 chown -R mysql:mysql /usr/local/mysql
 chmod +w /usr/local/mysql
 chown -R mysql:mysql /mydata/data
 scripts/mysql_install_db --user=mysql --datadir=/mydata/data
 注意:初始化,应该在安装目录内进行

8.创建socket文件目录

 mkdir -p /var/lib/mysql
 chmod 777 /var/lib/mysql

9.设置服务启动脚本

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

10.增加到环境变量

 vim /etc/profile.d/mysql.sh
 export PATH=/usr/local/mysql/bin:$PATH
 . /etc/profile.d/mysql.sh
 hash -r

11.mysql授权

 grant all privileges on testDB.* to test@localhost identified by '1234';
 flush privileges;

 

四、安装PHP

1.安装依赖

 yum install libmcrypt libmcrypt-devel mhash mhash-devel mcrypt mcrypt-devel curl-devel bz2-devel libxml2 libxml2-devel  bzip2 bzip2-devel  libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel  libedit-devel  recode-devel  systemtap-sdt-devel libcurl-devel enchant-devel libpng-devel libicu-devel libmcrypt-devel  net-snmp-devel  libtidy-devel libxslt-devel autoconf -y

2.下载

 wget http://jp2.php.net/get/php-5.6.30.tar.gz/from/this/mirror
 tar xf mirror

3.安装

 ./configure --prefix=/usr/local/php5    \
 --with-mysql=mysqlnd   \
 --with-openssl     \
 --enable-fpm     \
 --enable-sockets     \
 --enable-sysvshm     \
 --with-mysqli=mysqlnd \
 --with-pdo-mysql=mysqlnd \
 --enable-mbstring     \
 --with-freetype-dir    \
 --with-jpeg-dir     \
 --with-png-dir     \
 --with-zlib-dir     \
 --with-libxml-dir=/usr    \
 --enable-xml     \
 --with-mhash     \
 --with-mcrypt     \
 --with-config-file-path=/etc/php5    \
 --with-config-file-scan-dir=/etc/php5.d    \
 --with-bz2  \
 --with-curl  \
 --enable-ftp \
 --with-gd   \
 --disable-ipv6 \
 --enable-gd-native-ttf \
 --enable-calendar \
 --with-gettext \
 --with-libdir=lib64
make && make install

4.创建配置文件

 cp php.ini-production /etc/php5/php.ini
 cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf 

5.创建服务脚本

 cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm5
 chmod +x /etc/rc.d/init.d/php-fpm5
 chkconfig --add php-fpm5
 chkconfig php-fpm5 on

五、环境整合

nginx优化配置:

user www www;
worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections 51200;
    multi_accept on;
    }

http {
    fastcgi_intercept_errors on;
    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 1024m;
    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/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    include vhost/*.conf;
}

虚拟主机配置:

server
{
	listen       80;
	server_name  www.backworld.cn;
	root  /home/www/backworld;
	error_page 404 403 = /404.html;

	location / {
 	   index        index.php index.html index.htm;
   	   try_files $uri $uri/ /index.php?$args;
        }

	error_log   logs/www.backworld.com-error.log;
	access_log  logs/www.backworld.com-access.log;

	client_max_body_size 300M;
        client_body_buffer_size 1024k;
	
	location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
	       access_log off; 
	       log_not_found off;
	       expires max;
	}
	
	location ~ .*\.php?$ {
		include fastcgi.conf;
		fastcgi_pass 127.0.0.1:10000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_index index.php;
	}
}

 

服务启动:

 service php-fpm5 start
 service nginx start
 service mysqld start

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

− 6 = 2