centos7 编译安装新版LNMP环境
环境版本如下:
1、系统环境:Centos 7 x86_64
2、NGINX:nginx-1.11.3.tar.gz
3、数据库:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz
4、PHP:php-5.6.25.tar.gz
记得不要忘了先安装gcc gcc-c++ wget net-tools等功能哦。
一、首先安装mariadb
应为数据库编译需要很长时间,所以我这里下载的是已经编译好了的二进制包,下载版本为:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz
1、下载二进制包到/usr/local/src 目录下:
2、将压缩包解压到/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
3、[root@centos74 src]# mv /home/work/env/mariadb-10.0.28-linux-x86_64/ /home/work/env/mariadb
4、创建mariadb 数据初始化目录/home/work/env/mariadb/data/mysql:
[root@centos74 src]# mkdir -p /home/work/env/mariadb/data/mysql
5、将mariadb 数据初始化目录所属主和组都修改为work:
[root@centos74 src]# chown -R work:work /home/work/env/mariadb/data/mysql
6、进入重命名后的目录,初始化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
' ... :: [Note] InnoDB: Using mutexes to ref count buffer pool pages :: [Note] InnoDB: The InnoDB memory heap is disabled :: [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins :: [Note] InnoDB: Compressed tables use zlib 1.2. :: [Note] InnoDB: Using Linux native AIO :: [Note] InnoDB: Using CPU crc32 instructions :: [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
7、配置文件/home/work/env/mariadb/my.cnf
[client]
port =
socket = /home/work/env/mariadb/data/mysql.sock
default-character-set = utf8
[mysqld]
port =
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 =
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
ft_min_word_len =
thread_cache_size =
query_cache_size = 16M
thread_concurrency =
max_connections =
slave_net_timeout =
max_connect_errors =
binlog_format = row
expire-logs-days =
#binlog-ignore-db = mysql
server-id =
log-bin = mysql-bin
log-slave-updates
relay-log = mysqld-relay-bin
relay-log-purge =
slow-query-log =
long_query_time =
innodb_file_per_table =
innodb_open_files =
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 =
innodb_lock_wait_timeout = [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
8、编辑mariadb启动脚本文件/home/work/env/mariadb/start.sh
#!/bin/sh
dir=`dirname $(realpath $)`
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 >& & echo $! sleep
# 确保mysql已经起来后, 再退出
while true
do
pid_file="$dir/data/mysql.pid"
if test -f "$pid_file"
then
PID=`cat "$pid_file"`
if kill - $PID > /dev/null > /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
done
编辑mariadb启动脚本文件/home/work/env/mariadb/stop.sh
#!/bin/sh
dir=`dirname $(realpath $)`
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
10、将mariadb自带命令放入$PATH
[root@localhost ~]# echo "export PATH=$PATH:/home/work/env/mariadb/bin/" >>/etc/profile
[root@localhost ~]# source !$
11、启动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.
[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.]# ./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..tar.gz
#cd mcrypt-2.6.
#LD_LIBRARY_PATH=/usr/local/lib ./configure
#make
#make install
4、安装php
[root@localhost php-5.6.25]
# make && make install
以上每一个步骤,如果没有完全执行正确,那么下一步是无法进行的,使用 echo $? 看结果是否为 “0” , 如果不是,就是没有执行正确。
5、修改配置文件
cp php.ini-production /home/work/env/php/etc/php.ini
vim /home/work/env/php/etc/php-fpm.conf
6、把如下内容写入该文件:
[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 =
pm.start_servers =
pm.min_spare_servers =
pm.max_spare_servers =
pm.max_requests =
rlimit_files =
7、保存配置文件后,检验配置是否正确的方法为:/home/work/env/php/sbin/php-fpm -t
如果出现诸如 “test is successful” 字样,说明配置没有问题。
8、启动php-fpm
#!/bin/bash
#opt.sh
dir=`dirname $(realpath $)`
export LD_LIBRARY_PATH=$dir/lib
usage()
{
echo "usage: sh opt.sh start|stop|restart|count"
} OPT=$
if [ $# -ne ]; then
usage
exit
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
9、检测是否启动:
ps aux |grep php-fpm
看看是不是有很多个进程(大概20多个)。
三、安装nginx
1、下载nginx软件包:nginx-1.11.3.tar.gz
2、解压nginx
tar zxvf nginx-1.11.3.tar.gz
3、配置编译参数
cd nginx-1.11.
./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
4、编译nginx
make
安装nginx
make install
8、更改nginx配置
首先把原来的配置文件清空: 建议mv备份
>
/home/work/env/nginx/conf/nginx
.conf
“>” 这个符号为重定向的意思,单独用它,可以把一个文本文档快速清空。
vim
/home/work/env/nginx/conf/nginx
.conf
user work work;
worker_processes ;
error_log /home/work/env/nginx/logs/nginx_error.log crit;
pid /home/work/env/nginx/logs/nginx.pid;
worker_rlimit_nofile ; events
{
use epoll;
worker_connections ;
} http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size ;
server_names_hash_max_size ;
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 ;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size ;
client_header_buffer_size 1k;
large_client_header_buffers 4k;
request_pool_size 4k;
output_buffers 32k;
postpone_output ;
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 8k;
gzip_comp_level ;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml; server
{
listen ;
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 ; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ;
fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ;
fastcgi_buffer_size 128k;
fastcgi_buffers 128k;
tcp_nodelay on;
client_max_body_size 128m; gzip on;
gzip_min_length ;
gzip_buffers 16k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
gzip_comp_level ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; user work;
worker_processes ; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ;
fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ;
fastcgi_buffer_size 128k;
fastcgi_buffers 128k;
tcp_nodelay on;
client_max_body_size 128m; gzip on;
gzip_min_length ;
gzip_buffers 16k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
gzip_comp_level ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
}
9、保存配置后,先检验一下配置文件是否有错误存在:
/home/work/env/nginx/sbin/nginx
-t
nginx: configuration file /home/work/env/nginx/conf/nginx.conf test is successful
/nginx/conf/nginx
.conf
11、ps
aux |
grep
nginx
看是否有进程。
12、测试是否解析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