Manjaro 安装 LNMP环境

Posted by LB on Sat, Mar 24, 2018

安装PHP、PHP-FPM ; 编译安装 Nginx、MYSQL; 详细配置信息。

1. 编译Nginx

1.1 创建用户 及 用户组

1sudo groupadd -r www
2sudo useradd -s /sbin/nologin -g www -r www

1.2 下载Nginx源码包 并 解压

1wget http://nginx.org/download/nginx-1.14.0.tar.gz

1.3 配置编译参数

1./configure --user=www --group=www --prefix=/usr/local/nginx \ 
2	   		   --with-http_stub_status_module --with-http_ssl_module \
3	   		    --with-http_v2_module --with-http_gzip_static_module \
4	   		   --with-http_sub_module --with-openssl=/home/corerman/temp/nginx-1.13.12/src/openssl-1.0.2o/

1.4 编译及安装(root权限)

1sudo make && make install

1.5 配置Nginx 配置文件

1.5.1 配置enable-php.conf

1        location ~ [^/]\.php(/|$)
2        {
3            try_files $uri =404;
4            fastcgi_pass  unix:/tmp/php-cgi.sock;
5            fastcgi_index index.php;
6            include fastcgi.conf;
7        }

1.5.2 配置nginx.conf

 1user  www www;
 2
 3worker_processes auto;
 4error_log  /home/wwwlogs/nginx_error.log  crit;
 5pid        /usr/local/nginx/logs/nginx.pid;
 6
 7worker_rlimit_nofile 51200;
 8
 9events
10     {
11	      use epoll;
12          worker_connections 51200;
13          multi_accept on;
14	  }
15http
16     {
17         include       mime.types;
18         default_type  application/octet-stream;
19         server_names_hash_bucket_size 128;
20         client_header_buffer_size 32k;
21         large_client_header_buffers 4 32k;
22         client_max_body_size 50m;
23
24         sendfile   on;
25         tcp_nopush on;
26
27         keepalive_timeout 60;
28
29         tcp_nodelay on;
30
31         fastcgi_connect_timeout 300;
32         fastcgi_send_timeout 300;
33         fastcgi_read_timeout 300;
34         fastcgi_buffer_size 64k;
35         fastcgi_buffers 4 64k;
36         fastcgi_busy_buffers_size 128k;
37         fastcgi_temp_file_write_size 256k;
38
39         gzip on;
40         gzip_min_length  1k;
41         gzip_buffers     4 16k;
42         gzip_http_version 1.1;
43         gzip_comp_level 2;
44         gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml
45         application/xml+rss;
46         gzip_vary on;
47         gzip_proxied   expired no-cache no-store private auth;
48         gzip_disable   "MSIE [1-6]\.";
49         server_tokens off;
50         access_log off;
51
52       server
53           {
54              listen 80 default_server;
55               #listen [::]:80 default_server ipv6only=on;
56               server_name _;
57               index index.html index.htm index.php;
58               root  /home/wwwroot/default;
59               #error_page   404   /404.html;
60               include enable-php.conf;
61               location /nginx_status
62               {
63                    stub_status on;
64                   access_log   off;
65               }
66
67                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
68                {
69                    expires      30d;
70                }
71
72                location ~ .*\.(js|css)?$
73                {
74                    expires      12h;
75                }
76
77                location ~ /.well-known {
78                    allow all;
79               }
80
81                location ~ /\.
82                {
83                   deny all;
84               }
85
86               access_log  /home/wwwlogs/access.log;
87           }
88            include vhost/*.conf;
89        }

1.5 .3 配置Vhost 虚拟主机文件 例如list.conf

 1server {
 2            listen       81;
 3            server_name  debug.njapld.com;
 4            index index.php index.html;
 5            root  /home/corerman/ICODE/PHP/default;
 6            rewrite_log on;
 7             #   include thinkphp.conf;
 8            include enable-php.conf;
 9            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
10            {
11                expires      30d;
12            }
13
14           location ~ .*\.(js|css)?$
15            {
16                expires      12h;
17            }
18           location ~ /.well-known {
19               allow all;
20            }
21           location ~ /\.
22           {
23               deny all;
24           }
25            #access_log  /home/wwwlogs/access.log; 
26 }

2. 安装 MYSQL 系列数据库

2.1 创建用户 及 用户组

1groupadd mysql
2useradd -s /sbin/nologin -M -g mysql mysql

2.2 下载boost库 并 解压到 /usr/local/lib

https://astuteinternet.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

2.3 安装 rpcgen 附属库

1sudo pacman-S rpcsvc-proto

2.4 CMAKE生成编译文件系统

 1sudo  cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \
 2			-DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 \
 3			-DWITH_INNOBASE_STORAGE_ENGINE=1 \
 4			-DWITH_PARTITION_STORAGE_ENGINE=1 \
 5			-DWITH_FEDERATED_STORAGE_ENGINE=1 \
 6			-DEXTRA_CHARSETS=all \
 7			-DDEFAULT_CHARSET=utf8mb4 \
 8			-DDEFAULT_COLLATION=utf8mb4_general_ci \
 9			-DWITH_EMBEDDED_SERVER=1 \
10			-DENABLED_LOCAL_INFILE=1
11
12sudo make && make install

2.5 创建MYSQL DATA目录

1mkdir ${MySQL_Data_Dir}
2chown -R mysql:mysql ${MySQL_Data_Dir}

2.6 配置my.cnf (/etc/my.cnf)

 1[client]
 2#password   = your_password
 3port        = 3306
 4socket      = /tmp/mysql.sock
 5
 6[mysqld]
 7port        = 3306
 8socket      = /tmp/mysql.sock
 9datadir = ${MySQL_Data_Dir}
10skip-external-locking
11key_buffer_size = 16M
12max_allowed_packet = 1M
13table_open_cache = 64
14sort_buffer_size = 512K
15net_buffer_length = 8K
16read_buffer_size = 256K
17read_rnd_buffer_size = 512K
18myisam_sort_buffer_size = 8M
19thread_cache_size = 8
20query_cache_size = 8M
21tmp_table_size = 16M
22performance_schema_max_table_instances = 500
23
24explicit_defaults_for_timestamp = true
25#skip-networking
26max_connections = 500
27max_connect_errors = 100
28open_files_limit = 65535
29
30log-bin=mysql-bin
31binlog_format=mixed
32server-id   = 1
33expire_logs_days = 10
34early-plugin-load = ""
35
36#loose-innodb-trx=0
37#loose-innodb-locks=0
38#loose-innodb-lock-waits=0
39#loose-innodb-cmp=0
40#loose-innodb-cmp-per-index=0
41#loose-innodb-cmp-per-index-reset=0
42#loose-innodb-cmp-reset=0
43#loose-innodb-cmpmem=0
44#loose-innodb-cmpmem-reset=0
45#loose-innodb-buffer-page=0
46#loose-innodb-buffer-page-lru=0
47#loose-innodb-buffer-pool-stats=0
48#loose-innodb-metrics=0
49#loose-innodb-ft-default-stopword=0
50#loose-innodb-ft-inserted=0
51#loose-innodb-ft-deleted=0
52#loose-innodb-ft-being-deleted=0
53#loose-innodb-ft-config=0
54#loose-innodb-ft-index-cache=0
55#loose-innodb-ft-index-table=0
56#loose-innodb-sys-tables=0
57#loose-innodb-sys-tablestats=0
58#loose-innodb-sys-indexes=0
59#loose-innodb-sys-columns=0
60#loose-innodb-sys-fields=0
61#loose-innodb-sys-foreign=0
62#loose-innodb-sys-foreign-cols=0
63
64default_storage_engine = InnoDB
65#innodb_file_per_table = 1
66#innodb_data_home_dir = ${MySQL_Data_Dir}
67#innodb_data_file_path = ibdata1:10M:autoextend
68#innodb_log_group_home_dir = ${MySQL_Data_Dir}
69#innodb_buffer_pool_size = 16M
70#innodb_log_file_size = 5M
71#innodb_log_buffer_size = 8M
72#innodb_flush_log_at_trx_commit = 1
73#innodb_lock_wait_timeout = 50
74
75[mysqldump]
76quick
77max_allowed_packet = 16M
78
79[mysql]
80no-auto-rehash
81
82[myisamchk]
83key_buffer_size = 20M
84sort_buffer_size = 20M
85read_buffer = 2M
86write_buffer = 2M
87
88[mysqlhotcopy]
89interactive-timeout

2.7 配置my.cnf 2 (/etc/my.cnf)

1sed -i 's:^#innodb:innodb:g' /etc/my.cnf
23sed -i '/^default_storage_engine/d' /etc/my.cnf
4sed -i 's#default_storage_engine.*#default_storage_engine = MyISAM#' /etc/my.cnf

2.8 初始化数据库存储

1chown -R mysql:mysql ${MySQL_Data_Dir}
2/usr/local/mysql/bin/mysqld --initialize  --basedir=/usr/local/mysql --datadir=${MySQL_Data_Dir} --user=mysql

2.9 链接mysql开发库

1cat > /etc/ld.so.conf.d/mysql.conf<<EOF
2 /usr/local/mysql/lib/mysql
3 /usr/local/lib
4EOF
5
6ldconfig
7
8ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
9ln -sf /usr/local/mysql/include/mysql /usr/include/mysql

3.0 链接Shell命令系统

1ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
2ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
3ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
4ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
5ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck

3.1 修改mysql密码

1 /usr/local/mysql/bin/mysqladmin -u root password "${DB_Root_Password}"
2 ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

3.3 启动服务

1/usr/local/mysql/support-files/mysql.server start

3.2 MYSQL高级配置

 1        MySQL_Opt()
 2        {
 3        if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
 4            sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
 5            sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
 6            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
 7            sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
 8            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
 9            sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
10            sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
11            sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
12            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
13            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
14            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 1000" /etc/my.cnf
15        elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
16            sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
17            sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
18            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
19            sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
20            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
21            sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
22            sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
23            sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
24            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
25            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
26            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 2000" /etc/my.cnf
27        elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
28            sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
29            sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
30            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
31            sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
32            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
33            sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
34            sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
35            sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
36            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
37            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
38            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 4000" /etc/my.cnf
39        elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
40            sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
41            sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
42            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
43            sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
44            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
45            sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
46            sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
47            sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
48            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
49            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
50            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 6000" /etc/my.cnf
51        elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
52            sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
53            sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
54            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
55            sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
56            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
57            sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
58            sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
59            sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
60            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
61            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
62            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 8000" /etc/my.cnf
63        elif [[ ${MemTotal} -ge 32768 ]]; then
64            sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
65            sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
66            sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
67            sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
68            sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
69            sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
70            sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
71            sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
72            sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
73            sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
74            sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 10000" /etc/my.cnf
75        fi
76    }

3. 安装PHP模块

3.1 安装PHP 7

1sudo pacman -S php

3.2 配置php.ini文件

1        sed -i 's/post_max_size =.*/post_max_size = 50M/g' /etc/php/php.ini
2        sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /etc/php/php.ini
3        sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /etc/php/php.ini
4        sed -i 's/short_open_tag =.*/short_open_tag = On/g' /etc/php/php.ini
5        sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /etc/php/php.ini
6        sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /etc/php/php.ini
7        sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /etc/php/php.ini

3.3 安装PHP-FPM

1sudo pacman -S php-fpm

3.4 配置PHP-FPM 配置文件

 1[global]
 2pid = /usr/local/php/var/run/php-fpm.pid
 3error_log = /usr/local/php/var/log/php-fpm.log
 4log_level = notice
 5[www]
 6listen = /tmp/php-cgi.sock
 7listen.backlog = -1
 8listen.allowed_clients = 127.0.0.1
 9listen.owner = www
10listen.group = www
11listen.mode = 0666
12user = www
13group = www
14pm = dynamic
15pm.max_children = 10
16pm.start_servers = 2
17pm.min_spare_servers = 1
18pm.max_spare_servers = 6
19request_terminate_timeout = 100
20request_slowlog_timeout = 0
21slowlog = var/log/slow.log

3.5 以root权限执行PHP-FPM命令