• linux上搭建nginx+php+mysql环境详细讲解


    1、mysql安装

     1 #安装编译环境
     2 yum install -y gcc gcc-c++ gcc-devel g++ g++-devel;
     3 yum install -y wget
     4 yum install -y tar
     5 
     6 #创建mysql用户组及用户
     7 groupadd -f mysql
     8 useradd -g mysql mysql
     9 
    10 
    11 #编译mysql
    12 #安装依赖
    13 yum install -y ncurses ncurses-devel
    14 yum install -y cmake
    15 
    16 #解压mysql包
    17 tar -xf mysql-5.6.35.tar.gz
    18 cd  mysql-5.6.35.tar.gz
    19 
    20 #创建mysql的data数据目录
    21 mkdir -p /data/mysql/data
    22 chown -R mysql:mysql /data/mysql
    23 
    24 #编译,主要注意几个目录以及端口
    25 cmake -DCMAKE_INSTALL_PREFIX=/data/mysql  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1  -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/data/mysql -DEXTRA_CHARSETS=all -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/boost
    26 
    27 #安装    
    28 make -j 4 && make install
    29 
    30 
    31 #配置数据库
    32 cd  /data/mysql
    33 
    34 #初始化数据库
    35 ./scripts/mysql_install_db --basedir="/data/mysql" --datadir="/data/mysql/data"
    36 
    37 chown -R mysql:mysql 、/data/mysql/data/
    38 
    39 #复制服务到init.d目录
    40 cp /data/mysql/support-files/mysql.server  /etc/init.d/mysql
    41     
    42 #将mysql服务加入chkconfig管理列表 ,然后就可以用service进行操作,如果要开机自启再执行 chkconfig mysql on 
    43 
    44 chkconfig /etc/init.d/mysql
    45 
    46 #软链接(快捷方式),方便直接使用mysql客户端和备份命令
    47 ln -s /data/mysql/bin/mysql  /bin/mysql
    48 ln -s /data/mysql/bin/mysqldump  /bin/mysqldump
    49 
    50 #最后检查mysql目录下 my.cnf  配置文件,无误后启动mysql服务
    51 service mysql start
    52 
    53 #关闭命令
    54 service mysql stop
    55 #重启命令
    56 service mysql restart 
    57 
    58 #客户端第一次登陆数据库,没有密码的
    59 mysql -uroot -p 

    mysql配置参考,有些参数视具体而定

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.
    [client]
    port=3306
    socket =/tmp/mysql.sock
    [mysqld]
    
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    
    # These are commonly set, remove the # and set as required.
    basedir =/data/mysql
    datadir =/data/mysql/data
    port =3306
    socket =/tmp/mysql.sock
    key_buffer_size =  128M
    open_files_limit = 10240
    sort_buffer_size = 8M
    join_buffer_size = 4M
    read_buffer_size = 16M
    read_rnd_buffer_size=16M
    myisam_sort_buffer_size = 64M
    thread_cache_size = 8
    query_cache_size = 32M 
    query_cache_limit = 16M #允许临时存放在查询缓存区里的查询结果的最大长度(默认设置是1M)
    max_connections=4000
    max_allowed_packet = 1G
    default_storage_engine = InnoDB #默认引擎innodb
    bulk_insert_buffer_size = 200M
    #thread_stack = 512K #线程使用的堆大小. 此容量的内存在每次连接时被预留
    thread_concurrency=16 #CPU的2倍,用于多核CPU上
    
    innodb_buffer_pool_size = 1G
    innodb_log_buffer_size = 16M
    innodb_additional_mem_pool_size=32M
    innodb_flush_log_at_trx_commit=2 # 1:(默认值) 每一次事务提交或事务外的指令都需要把日志写入硬盘 2:把日志写入系统缓存 0:延迟写入
    #innodb_flush_method=3 # 1) Default – 使用fsync。 2) O_SYNC 以sync模式打开文件,通常比较慢。 3) O_DIRECT,在Linux上使用Direct IO。
    innodb_thread_concurrency=16
    innodb_file_io_threads=8
    innodb_file_per_table = 1
    # innodb_strict_mode=1 #建议加上
    innodb_io_capacity = 500
    
    relay_log_recovery=1
    tmp_table_size=268435456
    max_heap_table_size=268435456
    
    slow_query_log=ON
    long_query_time=60 #添加慢查询
    slow_query_log_file=slowquery.log
    log_bin=mysql-bin
    binlog_format=mixed
    expire_logs_days=5
    max_binlog_size=512M #日志文件太大读写效率降低
    #从配置
    slave-skip-errors=1062,1064
    relay_log=mysql-relay-bin
    log_slave_updates=1
    replicate-ignore-db=mysql,performance_schema,information_schema
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M 
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
    
    [mysqldump]
    quick
    max_allowed_packet = 1G
    
    [mysql]
    no-auto-rehash
    
    [myisamchk]
    key_buffer_size = 400M
    sort_buffer_size = 256M
    read_buffer = 2M
    write_buffer = 2M
    
    [mysqlhotcopy]
    interactive-timeout

    2、php安装,需要自己下载libmcrypt-2.5.8.tar.gz 依赖

     1 #安装依赖
     2 yum install -y libxml2-devel
     3 yum install -y bzip2-devel
     4 yum install -y libcurl-devel
     5 yum install -y libjpeg-devel libpng-devel freetype-devel
     6     
     7 #手动安装libmcrypt依赖
     8 tar -xf libmcrypt-2.5.8.tar.gz
     9 cd libmcrypt-2.5.8
    10 
    11 #配置 自定义安装目录
    12 ./configure --prefix=/usr/local/libmcrypt
    13 #开始安装
    14 make -j 4 && make install
    15 cd ../
    16 
    17 #安装php
    18 tar -xf php-5.6.29.tar.gz
    19 cd php-5.6.29
    20 
    21 #配置信息
    22 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm  --with-mcrypt=/usr/local/libmcrypt --with-zlib --enable-mbstring --with-openssl --with-mysql --with-mysqli --with-mysql-sock --with-gd --enable-gd-native-ttf --with-jpeg-dir=/usr/lib64 --with-freetype-dir=/usr/lib64 --with-png-dir=/usr/lib64 --enable-pdo --with-pdo-mysql --with-gettext --with-curl --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip --enable-pcntl --enable-sysvmsg --enable-mysqlnd=mysqlnd --enable-calendar --enable-mbstring --enable-maintainer-zts
    23 #开始安装    
    24 make -j 4 && make install
    25 cd ../

    3、安装nginx,需要手动下载pcre-8.39.tar.gz 和zlib-1.2.10.tar.gz

     1 #我这里是提前把pcre、zlib以及nginx的包下载到了/data/install目录
     2 #实际安装中的目录根据具体的而定
     3 
     4 #解压依赖包
     5 tar -xf pcre-8.39.tar.gz
     6 tar -xf zlib-1.2.10.tar.gz
     7 
     8 tar -xf nginx-1.9.15.tar.gz
     9 cd nginx-1.9.15
    10 yum install -y openssl openssl-devel
    11 
    12 #配置
    13 ./configure --sbin-path=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/data/install/pcre-8.39 --with-zlib=/data/install/zlib-1.2.10
    14 
    15 #开始安装
    16 make -j 4 && make install

    4、配置nginx

    (1)创建根目录和日志目录:

      mkdir  /data/wwwroot  

      mkdir -p /data/logs/nginx

    (2)修改配置文件

      

    user  www;
    worker_processes  8;
    worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
    
    error_log   /data/logs/nginx/error.log;
    
    pid        nginx.pid;
    
    
    events {
        worker_connections 65535;
        multi_accept on;
        use epoll;
    }
    
    
    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  /data/logs/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  120;
    	add_header Access-Control-Allow-Origin "*";
    	server_names_hash_bucket_size 128;
        gzip  on;
    	
    	server{
    		listen 80;
    		server_name localhost;
    		root /data/wwwroot;
    
    		location / {
    			index index.html index.htm index.php;
    		}
    		location ~ .php {
    			root /data/wwwroot;
    			fastcgi_pass  127.0.0.1:9000;
    			fastcgi_index index.php;
    			fastcgi_param SCRIPT_FILENAME /usr/local/php$fastcgi_script_name;
    			include    fastcgi_params;
    			set $path_info "";
    			set $real_script_name $fastcgi_script_name;
    			if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
    				 set $real_script_name $1;
    				 set $path_info $2;
    			}
    			fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    			fastcgi_param SCRIPT_NAME $real_script_name;
    			fastcgi_param PATH_INFO $path_info;
    	   }
    	}
        include 'conf/*.conf';
    
    }

    (3)用www用户启动nginx

         创建www用户:groupadd www

         创建www用户组:useradd -g www www

         启动nginx/usr/local/nginx/nginx

       关闭nginx: /usr/local/nginx/nginx -s stop

      如果是在虚拟机安装可能还需要关闭防火墙 (外部访问 虚拟机要先关闭防火墙chkconfig iptables off 或者 service iptables stop 或者 iptables -F)

    5、phpnginx关联

      开启php-fpm配置文件:

      cd  /usr/local/php/etc

      cp  php-fpm.conf.default  ./php-fpm.conf

      开启php-fpm:

      /usr/local/php/sbin/php-fpm

    6、常见问题:

    (1)启动mysql服务 ,提示my.cnf被忽略?

       解决方法:修改my.cnf的权限  chmod 644 /data/mysql1/my.cnf

    (2)终端mysql进不去?

      解决办法:mysql1 -h127.0.0.1 -uroot -p  主机加上127.0.0.1

    (3)外部连不上虚拟机mysql

      解决办法:iptables -F

      参考网址:http://blog.csdn.net/ynh_123/article/details/53023621

    (4)如何开机mysql服务自动开启?

      解决办法:上传启动控制脚本到/etc/init.d (一般安装后里面会自动生成,不需上传),mysql添加到系统服务,然后设置自动开启。

      chkconfig --add /etc/init.d/mysql  

      chkconfig  mysql  on

      详细内容可参考网址:http://blog.csdn.net/b_shuang1113/article/details/75635477

    (5)如何设置nginx开机自启动?

       (1) 上传启动控制脚本nginx /etc/init.d

      (2) 设置权限:chmod 755 /etc/init.d/nginx

      (3) 添加到系统服务:chkconfig --add /etc/init.d/nginx

      (4) 设置开机启动:chkconfig  ginx  on

      (5) 查看是否设置成功:chkconfig --list | grep nginx   (2-5选项为on)

      参考网址:http://www.jb51.net/article/51973.htm

    (6)如何设置php-fpm开机自启动?

      (1)上传启动控制脚本php-fpm/etc/init.d

      (2)设置权限:chmod 755 /etc/init.d/php-fpm

      (3)添加到系统服务:chkconfig --add /etc/init.d/php-fpm

      (4)设置开机启动:chkconfig  ginx  on

      (5)查看是否设置成功:chkconfig --list | grep php-fpm   (2-5选项为on)

      参考网址:http://www.jb51.net/article/68153.htm

    (7)替换了配置文件mysql也无法启动?

         解决办法:如果替换了配置文件,启动还是报这个错误:mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.ERROR! The server quit without updating PID file (/data/mysql/data/bogon.pid).

      这时可以去/etc目录下删掉默认的my.cnf ,然后再次启动即可

    最后附上完整的一键安装脚本,以及所需安装包,配置文件、启动控制脚本

      链接:https://pan.baidu.com/s/1XkxgW9fRINqg_Zi3W27OUg 密码:ddan 

  • 相关阅读:
    LeetCode "Palindrome Partition II"
    LeetCode "Longest Substring Without Repeating Characters"
    LeetCode "Wildcard Matching"
    LeetCode "Best Time to Buy and Sell Stock II"
    LeetCodeEPI "Best Time to Buy and Sell Stock"
    LeetCode "Substring with Concatenation of All Words"
    LeetCode "Word Break II"
    LeetCode "Word Break"
    Some thoughts..
    LeetCode "Longest Valid Parentheses"
  • 原文地址:https://www.cnblogs.com/myIvan/p/9533474.html
Copyright © 2020-2023  润新知