centos7 編譯安裝新版LNMP環境javascript
環境版本以下:php
一、系統環境:Centos 7 x86_64css
二、NGINX:nginx-1.11.3.tar.gzhtml
三、數據庫:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gzjava
四、PHP:php-5.6.25.tar.gznode
記得不要忘了先安裝gcc gcc-c++ wget net-tools等功能哦。mysql
1、首先安裝mariadblinux
應爲數據庫編譯須要很長時間,因此我這裏下載的是已經編譯好了的二進制包,下載版本爲:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gznginx
一、下載二進制包到/usr/local/src 目錄下:c++
二、將壓縮包解壓到/home/work/env 目錄下: #根據我的習慣更改安裝目錄,下面再也不作解釋。#/home/work/env/
[root@centos74 src]#tar zvxf mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz -C /home/work/env
三、[root@centos74 src]# mv /home/work/env/mariadb-10.0.28-linux-x86_64/ /home/work/env/mariadb
四、建立mariadb 數據初始化目錄/home/work/env/mariadb/data/mysql:
[root@centos74 src]# mkdir -p /home/work/env/mariadb/data/mysql
五、將mariadb 數據初始化目錄所屬主和組都修改成work:
[root@centos74 src]# chown -R work:work /home/work/env/mariadb/data/mysql
六、進入重命名後的目錄,初始化mariadb:
root@centos74 src]# cd /home/work/env/mariadb [root@centos74 mysql]# ./scripts/mysql_install_db --datadir=/home/work/env/mariadb/data/mysql --user=work
Installing MariaDB/MySQL system tables in '/home/work/env/mariadb/data/mysql
' ... 140906 2:03:19 [Note] InnoDB: Using mutexes to ref count buffer pool pages 140906 2:03:19 [Note] InnoDB: The InnoDB memory heap is disabled 140906 2:03:19 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 140906 2:03:19 [Note] InnoDB: Compressed tables use zlib 1.2.3 140906 2:03:19 [Note] InnoDB: Using Linux native AIO 140906 2:03:19 [Note] InnoDB: Using CPU crc32 instructions 140906 2:03:19 [Note] InnoDB: Initializing buffer pool, size = 128.0M ........................................................................ The latest information about MariaDB is available at http://mariadb.org/. You can find additional information about the MySQL part at: http://dev.mysql.com Support MariaDB development by buying support/new features from SkySQL Ab. You can contact us about this at sales@skysql.com. Alternatively consider joining our community based development effort: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
報錯:WARNING: The host 'test4' could not be looked up with resolveip.
解決辦法:vim /etc/hosts 在最後一行添加192.168.1.242 test4
報錯:./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解決辦法:yum -y install libaio-devel libaio
七、配置文件/home/work/env/mariadb/my.cnf
[client] port = 3306 socket = /home/work/env/mariadb/data/mysql.sock default-character-set = utf8 [mysqld] port = 3306 tmp_table_size = 512M max_heap_table_size = 512M socket = /home/work/env/mariadb/data/mysql.sock character-set-server = utf8 character-set-filesystem = utf8 skip-external-locking key_buffer_size = 256M max_allowed_packet = 32M table_open_cache = 1024 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M ft_min_word_len = 2 thread_cache_size = 8 query_cache_size = 16M thread_concurrency = 8 max_connections = 500 slave_net_timeout = 30 max_connect_errors = 100000 binlog_format = row expire-logs-days = 3 #binlog-ignore-db = mysql server-id = 96 log-bin = mysql-bin log-slave-updates relay-log = mysqld-relay-bin relay-log-purge = 1 slow-query-log = 1 long_query_time = 2 innodb_file_per_table = 1 innodb_open_files = 800 innodb_buffer_pool_size = 6G innodb_additional_mem_pool_size = 16M innodb_log_file_size = 512M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 2 innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash default-character-set=utf8 [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
八、編輯mariadb啓動腳本文件/home/work/env/mariadb/start.sh
#!/bin/sh dir=`dirname $(realpath $0)` cmd="nohup $dir/bin/mysqld_safe --defaults-file=$dir/my.cnf --basedir=$dir --ledir=$dir/bin --datadir=$dir/data --log-error=$dir/data/mysql.err --pid-file=$dir/data/mysql.pid >> mysql.log 2>&1 & echo $!" echo $cmd nohup $dir/bin/mysqld_safe --defaults-file=$dir/my.cnf --basedir=$dir --ledir=$dir/bin --datadir=$dir/data --log-error=$dir/data/mysql.err --pid-file=$dir/data/mysql.pid >> mysql.log 2>&1 & echo $! sleep 1 # 確保mysql已經起來後, 再退出 while true do pid_file="$dir/data/mysql.pid" if test -f "$pid_file" then PID=`cat "$pid_file"` if kill -0 $PID > /dev/null 2> /dev/null then if ps wwwp $PID | grep -v mysqld_safe | grep -- mysqld > /dev/null then #mysqld 已經被啓動了 echo "mysqld has been started!" break fi fi fi echo "mysqld is still starting, waiting for it" sleep 2 done
編輯mariadb啓動腳本文件/home/work/env/mariadb/stop.sh
#!/bin/sh dir=`dirname $(realpath $0)` cmd="$dir/bin/mysqladmin -S $dir/data/mysql.sock -uroot shutdown" echo $cmd $dir/bin/mysqladmin -S $dir/data/mysql.sock -uroot shutdown
編輯mariadb啓動腳本文件/home/work/env/mariadb/connnect.sh
bin/mysql -uroot -Sdata/mysql.sock -A
十、將mariadb自帶命令放入$PATH
[root@localhost ~]# echo "export PATH=$PATH:/home/work/env/mariadb/bin/" >>/etc/profile [root@localhost ~]# source !$
十一、啓動mariadb:
chmod +x start.sh
sh start.sh
[root@localhost src]
# tar zxf php-5.6.25.tar.gz -C /home/work/env
[root@localhost src]# cd php-5.6.25 [root@localhost src]# yum -y install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel [root@localhost php-5.6.25]# ./configure --prefix=/home/work/env/php --with-config-file-path=/home/work/env/php/etc --enable-fpm --with-fpm-user=work --with-fpm-group=work --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gd --with-libxml-dir=/home/work/env --with-gettext
錯誤:configure: error: xml2-config not found. Please check your libxml2 installation.
解決辦法:yum -y install libxml2-devel
錯誤:configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/
解決辦法:yum -y install libcurl-devel
錯誤:configure: error: jpeglib.h not found.
解決辦法:yum -y install libjpeg-turbo-devel
錯誤:configure: error: png.h not found.
解決辦法:yum -y install libpng-devel
錯誤:configure: error: freetype-config not found.
解決辦法:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
錯誤:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決辦法:yum -y install libmcrypt-devel
若是yum沒有這個libmcrypt-devel包,那就本身編譯安裝libmcrypt-2.5.8.tar.gz(去下載)
#tar -zxvf mcrypt-2.6.8.tar.gz #cd mcrypt-2.6.8 #LD_LIBRARY_PATH=/usr/local/lib ./configure #make #make install
四、安裝php
[root@localhost php-5.6.25]
# make && make install
以上每個步驟,若是沒有徹底執行正確,那麼下一步是沒法進行的,使用 echo $? 看結果是否爲 「0」 , 若是不是,就是沒有執行正確。
五、修改配置文件
cp php.ini-production /home/work/env/php/etc/php.ini vim /home/work/env/php/etc/php-fpm.conf
六、把以下內容寫入該文件:
[global] pid = /home/work/env/php/var/run/php-fpm.pid error_log = /home/work/env/php/var/log/php-fpm.log [www] listen = /home/work/env/php/php-fcgi.sock #或 listen = 127.0.0.1:9000 user = work group = work listen.owner = work listen.group = work pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
七、保存配置文件後,檢驗配置是否正確的方法爲:/home/work/env/php/sbin/php-fpm -t
若是出現諸如 「test is successful」 字樣,說明配置沒有問題。
八、啓動php-fpm
#!/bin/bash #opt.sh dir=`dirname $(realpath $0)` export LD_LIBRARY_PATH=$dir/lib usage() { echo "usage: sh opt.sh start|stop|restart|count" } OPT=$1 if [ $# -ne 1 ]; then usage exit 1 fi case $OPT in start|Start) echo "Starting..." /home/work/env/php/sbin/php-fpm --prefix=/home/work/env/php -c /home/work/env/php/etc echo "Done" ;; stop|Stop) echo "Stopping..." kill -INT `cat /home/work/env/php/var/run/php-fpm.pid` echo "Done" ;; restart|Restart) echo "Restarting..." kill -USR2 `cat /home/work/env/php/var/run/php-fpm.pid` echo "Done" ;; count|Count) echo "Count..." ps aux | grep -c php-fpm echo "Done" ;; *)usage ;; esac
九、檢測是否啓動:
ps aux |grep php-fpm
看看是否是有不少個進程(大概20多個)。
3、安裝nginx
一、下載nginx軟件包:nginx-1.11.3.tar.gz
二、解壓nginx
tar zxvf nginx-1.11.3.tar.gz
三、配置編譯參數
cd nginx-1.11.3 ./configure \ --prefix=/home/work/env/nginx \ --with-http_realip_module \ --with-http_sub_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre
報錯:./configure: error: the HTTP rewrite module requires the PCRE library.
解決辦法:yum -y install pcre-devel
報錯:./configure: error: the HTTP gzip module requires the zlib library.
解決辦法:yum install -y zlib-devel
四、編譯nginx
make
安裝nginx
make install
八、更改nginx配置
首先把原來的配置文件清空: 建議mv備份
>
/home/work/env/nginx/conf/nginx
.conf
「>」 這個符號爲重定向的意思,單獨用它,能夠把一個文本文檔快速清空。
vim
/home/work/env/nginx/conf/nginx
.conf
user work work; worker_processes 2; error_log /home/work/env/nginx/logs/nginx_error.log crit;
pid /home/work/env/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /home/work/env/nginx/client_body_temp;
proxy_temp_path /home/work/env/nginx/proxy_temp;
fastcgi_temp_path /home/work/env/nginx/fastcgi_temp;
fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /home/work/env/nginx/html;
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
} } }
user work; worker_processes 8; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { 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; sendfile on; #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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; fastcgi_connect_timeout 120; fastcgi_send_timeout 120; fastcgi_read_timeout 120; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; tcp_nodelay on; client_max_body_size 128m; gzip on; gzip_min_length 1000; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css application/xml application/json; gzip_comp_level 2; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; user work; worker_processes 8; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; fastcgi_connect_timeout 120; fastcgi_send_timeout 120; fastcgi_read_timeout 120; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; tcp_nodelay on; client_max_body_size 128m; gzip on; gzip_min_length 1000; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css application/xml application/json; gzip_comp_level 2; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include hosts/*.conf; }
九、保存配置後,先檢驗一下配置文件是否有錯誤存在:
/home/work/env/nginx/sbin/nginx
-t
/nginx/conf/nginx
.conf
十一、ps
aux |
grep
nginx
看是否有進程。
十二、測試是否解析php文件
建立測試文件:
vim
/usr/local/nginx/html/2
.php
firewall-cmd --permanent --zone=public --add-port=80/tcp #添加80端口
firewall-cmd --reload #從新加載
firewall-cmd --list-port #查看是否添加成功
[root@localhost nginx]
# curl localhost/2.php
listen.owner = work
listen.group = work
這兩行,再重啓一下服務就能使用php了
緣由是/home/work/env/php/php-fcgi.sock這個文件沒有讀權限
至此,最新版的LNMP環境源碼編譯安裝完成了
docker容器裏安裝須要把端口映射到本地上:
登錄到172.18.1.34 (服務器ip)
機器上執行:
ssh -C -f -N -g -L 8080:127.0.0.1:80 172.18.1.144
目的把144的80端口映射到 34的8080上
/home/work/env