CentOS 6.5 安装 Nginx + MySQL + PHP

说明
Linux : CentOS release 6.5 (Final)
Core : Linux nate-vps 2.6.32-573.7.1.el6.x86_64
Nginx : 1.8.0
PHP : 5.6.14
系统环境配置
  • 更新源

    yum install wget -y
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    yum makecache
    
  • 更新包 & 系统

    yum update -y
    
  • 配置 hostname

    echo "HOSTNAME=nate-vps" >> /etc/sysconfig/network
    
    hostname "nate-vps"
    
  • 重启

    reboot
    
  • 配置SSH

    vi /etc/ssh/sshd_config
    
    Port 30022
    ClientAliveInterval 30
    UseDNS no
    GSSAPIAuthentication no
    
  • 重启SSH

    service sshd restart
    
  • 增加 history 的数量 & 设置显示history命令的时间戳

    vi /etc/profile
    
    HISTSIZE=100000
    
    export HISTTIMEFORMAT="%F %T "
    
  • source profile

    source /etc/profile
    
  • 设置时区

    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  
    
  • 安装ntp

    yum -y install ntp
    
    service ntpdate start
    service ntpd start
    chkconfig ntpdate on
    chkconfig ntpd on
    
  • 安装相关的包

    yum -y install gcc gcc-c++ make cmake ncurses-devel pcre-devel openssl-devel libxml2-devel gd gd-devel libjpeg-devel libpng-devel patch libcurl libcurl-devel bison
    
安装Nginx
  • 增加nginx用户

    useradd -M -r --shell /bin/sh --home-dir /opt/nginx nginx
    
  • 下载

    http://nginx.org/
    wget http://nginx.org/download/nginx-1.8.0.tar.gz
    
  • 解压

    tar zxvf nginx-1.8.0.tar.gz
    cd nginx-1.8.0
    
  • 编译安装

    ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module
    make -j 4
    make install
    
  • 编辑脚本

    touch /etc/rc.d/init.d/nginx
    vi /etc/rc.d/init.d/nginx
    
    #!/bin/sh
    #
    # nginx – this script starts and stops the nginx daemongg
    #
    # chkconfig: - 85 15
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    # proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config: /opt/nginx/conf/nginx.conf
    # pidfile: /opt/nginx/logs/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="/opt/nginx/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
    
    lockfile=/var/lock/subsys/nginx
    
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        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
        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
    
  • 增加相关的目录

    mkdir -p /data0/www/yhz.me/public_html
    mkdir -p /data0/www/yhz.me/logs
    
  • 编辑 nginx 配置文件

    vi /opt/nginx/conf/nginx.conf
    
    user	nginx nginx;
    worker_processes  4;
    
    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    pid /opt/nginx/logs/nginx.pid;
    
    events {
        use epoll;
        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 1024m;
    
        sendfile        on;
        tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        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;
    
        fastcgi_intercept_errors 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;
    
    	 	gzip  on;
    
    	  server {
    			  listen       80;
    			  server_name  _;
    			  return 444;
    	  }
    
    	  include /opt/nginx/conf/sites-enabled/*;
    
    }
    
  • 增加网站配置文件

    mkdir -p /opt/nginx/conf/sites-enabled/
    vi /opt/nginx/conf/sites-enabled/yhz.me.conf
    
    server {
    	listen   80;
    	server_name yhz.me;
    	access_log /data0/www/yhz.me/logs/access.log;
    	error_log /data0/www/yhz.me/logs/error.log;
    
    	location / {
    	   root   /data0/www/yhz.me/public_html;
    	   index  index.html index.htm;
    	}
    }
    
  • 操作nginx

    service nginx start
    service nginx restart
    service nginx stop
    service nginx reload
    
安装PHP
  • 下载

    http://php.net/
    wget http://cn2.php.net/get/php-5.6.14.tar.gz/from/this/mirror -O php-5.6.14.tar.gz
    
  • 解压

    tar zxvf php-5.6.14.tar.gz
    cd php-5.6.14
    
  • 编译安装

    ./configure \
     --prefix=/opt/php \
     --with-gd\
     --with-jpeg-dir \
     --with-png-dir \
     --with-freetype-dir \
     --with-iconv \
     --with-zlib \
     --enable-xml \
     --enable-bcmath \
     --enable-shmop \
     --enable-sysvsem \
     --enable-inline-optimization \
     --enable-mbregex \
     --enable-fpm \
     --enable-mbstring \
     --enable-ftp \
     --enable-gd-native-ttf \
     --with-openssl \
     --enable-pcntl \
     --enable-sockets \
     --with-xmlrpc \
     --enable-zip \
     --enable-soap \
     --without-pear \
     --with-gettext \
     --enable-session \
     --with-curl \
     --with-mysql=mysqlnd \
     --with-mysqli=mysqlnd \
     --with-pdo-mysql=mysqlnd 
    
     make -j 4
     make install
    
  • 配置 php.ini

    cp php.ini-production /opt/php/lib/php.ini
    vi /opt/php/lib/php.ini
    
    date.timezone = Asia/Shanghai
    
  • 配置 php-fpm.conf

    cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
    vi /opt/php/etc/php-fpm.conf
    
    pid = run/php-fpm.pid
    error_log = log/php-fpm.log
    log_level = notice
    daemonize = yes
    rlimit_files = 2560
    rlimit_core = 0
    process.max = 128
    
    pm.max_children = 100
    pm.start_servers = 30
    pm.min_spare_servers = 20
    pm.max_spare_servers = 80
    
  • 启动

    ln -s /opt/php/sbin/php-fpm /usr/sbin/php-fpm
    ln -s /opt/php/bin/php /usr/bin/php
    
    php-fpm -D
    
  • 修改网站nginx配置文件

    vi /opt/nginx/conf/sites-enabled/yhz.me.conf
    
    server {
    	listen 80;
    	server_name yhz.me;
    	access_log /data0/www/yhz.me/logs/access.log ;
    	error_log /data0/www/yhz.me/logs/error.log ;
    	root /data0/www/yhz.me/public_html;
    
    	error_page 404 /404.html;
    
    	location / {
    		root /data0/www/yhz.me/public_html;
    		index index.html index.htm index.php;
    	}
    
    	location ~ \.php$ {
    		include /opt/nginx/conf/fastcgi_params;
    		fastcgi_pass  127.0.0.1:9000;
    		fastcgi_index index.php;
    		fastcgi_param SCRIPT_FILENAME /data0/www/yhz.me/public_html$fastcgi_script_name;
    	}
    }
    
  • 重启nginx

    service nginx restart
    
安装MySQL
  • 下载

    http://www.percona.com/software/percona-server
    
    wget https://www.percona.com/downloads/Percona-Server-5.6/Percona-Server-5.6.26-74.0/source/tarball/percona-server-5.6.26-74.0.tar.gz
    tar zxvf percona-server-5.6.26-74.0.tar.gz 
    cd percona-server-5.6.26-74.0
    
  • 增加用户 & 设置目录

    groupadd mysql && useradd -g mysql mysql && mkdir -p /opt/mysql && mkdir -p /data1/mysql && chown -R mysql:mysql /data1/mysql
    
  • 编译安装

    cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql \
    -DMYSQL_UNIX_ADDR=/data1/mysql/mysql.sock \
    -DDEFAULT_CHARSET=gbk \
    -DDEFAULT_COLLATION=gbk_chinese_ci \
    -DWITH_EXTRA_CHARSETS:STRING=armscii8,ascii,big5,cp1250,cp1251,cp1256,cp1257,cp850,cp852,cp866,cp932,dec8,eucjpms,euckr,gb2312,gbk,geostd8,greek,hebrew,hp8,keybcs2,koi8r,koi8u,latin1,latin2,latin5,latin7,macce,macroman,sjis,swe7,tis620,ucs2,ujis,utf8 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_HEAP_STORAGE_ENGINE=0 \
    -DWITH_EDITLINE=bundled \
    -DENABLED_LOCAL_INFILE=1 \
    -DMYSQL_DATADIR=/data1/mysql \
    -DMYSQL_TCP_PORT=3306
    
    make -j 4
    make install
    
  • 复制启动文件 & 设置自动启动

    cp ./support-files/mysql.server /etc/init.d/mysqld && chmod 755 /etc/init.d/mysqld
    
    echo '/etc/init.d/mysqld start' >> /etc/rc.d/rc.local && chkconfig mysqld on
    
  • 生成配置文件

    https://tools.percona.com/wizard
    
  • 编辑配置文件

    vi /etc/my.cnf
    
  • 初始化数据库

    cd /opt/mysql
    chmod 755 ./scripts/mysql_install_db
    mkdir -p /data1/mysql
    ./scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/data1/mysql
    chmod -R 755 /data1/mysql
    
  • 复制控制脚本

    cp /opt/mysql/support-files/mysql.server /opt/mysql/bin/
    chmod a+x /opt/mysql/bin/mysql.server
    
  • 设置软链接

    ln -s /opt/mysql/bin/mysql /usr/bin/mysql
    
  • 启动MySQL

    /opt/mysql/bin/mysql.server start
    
  • 修改密码

    mysql -uroot -p
    
    use mysql;
    select host,user,password from user;
    delete from user where user='';
    update user set password=password('123123') where user='root';
    flush privileges;
    exit
    
防火墙设置
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
	
service iptables save
service iptables restart
0%