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 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM