第一步安装nginx1.10.3
优化nginx的介绍:jemalloc https://ideas.spkcn.com/software/os/linux/577.html 预先安装autoconf 和 make yum -y install autoconf make jemalloc的安装 jiemalloc 开源项目网站 http://www.canonware.com/jemalloc/ wget https://github.com/jemalloc/jemalloc/releases/jemalloc-4.2.1.tar.bz2 tar -xjf jemalloc-4.2.1.tar.bz2 cd jemalloc-4.2.1 ./configure --prefix=/usr/local/jemalloc --libdir=/usr/local/lib make && make install make clean cd ../ echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf /sbin/ldconfig 使用jemalloc优化MYSQL数据库 MYSQL或者MariaDB源码编译时添加以下参数 -DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF 或者编辑mysqld_safe文件直接加载: 查找文件 /usr/local/mysql/bin/mysqld_safe 在# executing mysqld_safe 下面加上 LD_PRELOAD=/usr/local/lib/libjemalloc.so 重新启动MYSQL 使用下面代码自动修改mysqld_safe文件 sed -i 's@executing mysqld_safe@executing mysqld_safe export LD_PRELOAD=/usr/local/lib/libjemalloc.so@' /usr/local/mysql/bin/mysqld_safe service mysqld restart 使用jemalloc优化NGINX 编译NGINX时添加以下参数: --with-ld-opt="-ljemalloc" 验证 jemalloc 是否运行: lsof -n | grep jemalloc
这个配置稍后在lnmp第二部分,此处没有mysqld的优化。
nginx基础环境搭建:
service iptables stop iptables -I INPUT -p tcp --dport 80 -j ACCEPT vi /etc/sysconfig/selinux selinux=disabled 创建文件夹 mkdir -p /usr/local/src cd /usr/local/src 用户 useradd -s /sbin/nologin -M www groupadd nginx 下载稳定版nginx1.10.3 yum -y install zlib* gcc gcc-c++ libtool openssl* automake autoconf libtool pcre* wget http://nginx.org/download/nginx-1.10.3.tar.gz cd nginx-1.10.3 ./configure --help ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt='-ljemalloc' --with-ld-opt这个优化的是nginx内存参数。 make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin 创建家目录 mkdir -p /home/wwwlogs/ mkdir -p /usr/local/nginx/logs/ 将编写好的nginx.conf 拷贝到nginx.conf 创建vhost,并属主属组, chown -R www:www /home/wwwroot/ mkdir -p /home/wwwroot/ /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx -v /usr/local/nginx/sbin/nginx -t netstat -antpu | grep nginx curl ip地址 :查看能否正常运行nginx。 关于pid报错的信息,killall -9 nginx 编写自己启动文档。
[root@test1 html]# cat /usr/local/nginx/conf/nginx.conf
user www www; worker_processes auto; #error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; access_log off; include vhost/*.conf; }
在nginx的配置文件中
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000; 该处必须和php-fpm.conf中的lisen一样
#fastcigi_pass /tmp/php-cgi.sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
官网下载mysql(该配置文件没有优化mysql内存)
下载MySQL5.7.20源码包,和boost wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.gz wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/ 添加用户和组MySQL,并且不允许登录 groupadd mysql usradd -r -g mysql -s /sbin/nologin -M mysql 配置环境,解压包 yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake tar -xf mysql-5.7.20.tar.gz cd mysql-5.7.20 mkdir -p /usr/local/mysql/ mkdir -p /usr/local/boost/ cp boost_1_59_0.tar.gz /usr/local/boost 将包放到boost下 mkdir -p /data/mysql 其中data下的属主和属组必须是mysql,date是数据库目录。 ls /usr/local/mysql chown -R mysql:mysql /data/mysql cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DDOWNLOAD_BOOST=1 -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -DWITH_BOOST=/usr/local/boost -DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF 此处做优化内容。 make && make install 将安装目录添加到环境变量中: echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile source /etc/profile 查看文件错误日志出现这行信息, 说明文件没有初始化,需要删除/data/mysql(数据库目录)下的文档 2017-12-18T15:52:09.477533Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 2017-12-18T15:52:09.477899Z 0 [ERROR] unknown variable 'default-file=/etc/my.cnf' 2017-12-18T15:52:09.477915Z 0 [ERROR] Aborting 5 初始化安装MySQL cd /data/mysql 每次初始化时都要删除该目录下文件(数据库目录) rm -rf ./* /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql ps aux | grep mysql 会出现如下内容: Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ mysql 27672 0.1 2.5 914052 49200 pts/1 Sl 00:11 0:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/mysql-error.log --open-files-limit=65535 --pid-file=/usr/local/mysql/mysql.pid --socket=/usr/local/mysql/mysql.sock --port=3306 root 27721 0.0 0.0 103260 876 pts/1 S+ 00:15 0:00 grep 3306 6 配置my.cnf 在启动mysql服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到会搜索 $basedir/my.cnf 也就是安装目录,新版的配置文件默认位置。将/etc下的my.cnf备份 否则该文件会影响安装mysql的正确配置,造成无法启动。 cp my-default.cnf /etc/my.cnf 后台启动MySQL /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & 正常启动后没有其他内容,回车一下就出来了。 查看MySQL是否启动 ps -aux | grep mysql 将启动的MySQL杀死 kill -9 进程号(两个) 只留下一个就好了:root 80654 0.0 0.0 103256 840 pts/1 S+ 08:18 0:00 grep mysql cd /usr/local/mysql/support-files 将其中的mysql-server复制到启动程序下,并改名为mysqld cp mysql-server /etc/init.d/ mv mysql-server /etc/init.d/mysqld chmod 755 mysqld 修改vi /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/data/mysql 启动程序 /etc/init.d/mysqld restart 设置开机自启 chkconfig mysqld on chkconfig --add mysqld 修改mysqld的密码 vi /etc/my.cnf 在mysqld下添加:skip-grant-tables service mysqld restart mysql -u root -p 此时密码为空 登录并修改mysql的root密码: >use mysql; >update user set authentication_string=password('123456') where user='root'; >flush privileges; >quit 在5.7的版主中原来的password改为:authertication_string 在>select * from mysql.user G; 中得到。 删除skip-grant-tables 重启服务
mysql -v
关于mysql配置过程中出错的问题
启动MySQL时出错:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 32519
[root@slave1 mysql]# 2017-12-20T12:50:13.972444Z mysqld_safe error:
log-error set to '/usr/local/mysql/mysql-error.log',
however file don't exists. Create writable for user 'mysql'.
解决办法:
echo "" > /usr/local/mysql/mysql-error.log
chown -R mysql:mysql /usr/local/mysql/mysql-error.log
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
关于pid的问题
当初始化时会产生一个pid,所以每次读取配置文件都要读取该pid
pid报错时候,用service mysqld restart不能读取,应该用
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
然后从错误的文档中查看。出现的报错信息时间会对不上,找最近的。
关于cmake时报错
CMake Error at cmake/boost.cmake:76 (MESSAGE):
Call Stack (most recent call first):
cmake/boost.cmake:228 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:435 (INCLUDE)
解决方法一:
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/usr/local/mysql/data
-DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock
-DMYSQL_USER=mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DENABLED_LOCAL_INFILE=ON
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/boost
解决方法二:
下载boost包,放到/usr/local/boost目录下,
cmake后面加选项 -DWITH_BOOST=/usr/local/boost
关于sock问题
2018-01-15T03:10:11.729108Z 0 [ERROR] Could not create unix socket lock file /usr/local/mysql/mysql.sock.lock.
2018-01-15T03:10:11.729117Z 0 [ERROR] Unable to setup unix socket lock file.
尝试给my.cnf备份,然后在将my.cnf中的socket改下路径
vi /etc/my.cnf [root@test1 html]# cat /etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock #default-character-set = utf8mb4 [mysqld] port = 3306 socket = /data/mysql/mysql.sock basedir = /usr/local/mysql datadir = /data/mysql pid-file = /data/mysql/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 10 log-bin=/data/mysql/mysql-bin/mysql-bin relay-log=/data/mysql/relay-bin/relay-bin slow_query_log_file = /data/mysql/mysql-slow.log #init-connect = 'SET NAMES utf8mb4' log_error = /data/mysql/mysql-error.log character-set-server = utf8mb4 #skip-name-resolve #skip-networking #skip-grant-tables log-slave-updates binlog_format = mixed #binlog-ignore-db=mysql,information_schema,sys,performance_schema default_storage_engine = InnoDB #default-storage-engine = MyISAM back_log = 300 max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 8M query_cache_limit = 2M ft_min_word_len = 4 expire_logs_days = 30 slow_query_log = 1 long_query_time = 1 performance_schema = 0 explicit_defaults_for_timestamp #lower_case_table_names = 1 skip-external-locking innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 init-connect = 'SET NAMES utf8mb4' [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
wget http://cn2.php.net/distributions/php-5.6.10.tar.gz tar -zxvf php-5.6.10.tar.gz cd php-5.6.10 ./configure --help yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel gcc gcc-c++ yum install -y epel-release yum -y install libmcrypt libmcrypt-devel freetype-devel ./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo make && make install 指定 php 安装目录 --prefix=/usr/local/php # 指定php.ini位置 --with-config-file-path=/etc
# mysql安装目录,对mysql的支持 --with-mysql=/usr/local/mysql find / -name mysql.h 找到mysql.h的路径 /usr/local/mysql/include/mysql.h /usr/local/src/mysql-5.7.20/include/mysql.h /usr/local/src/mysql-5.7.20/rapid/plugin/x/mysqlxtest_src/mysqlx/mysql.h --with-mysql=/usr #这是mysql相关编译路径,这里网上很多都是--with-mysql=/usr/share/mysql,但我这样做,编译完提示一个error,Cannot find MySQL header files under /usr/share/mysql ,经查,有的说是因为64位电脑的原因,此处正确写法是 先 用 find / -name mysql.h 找到mysql.h的路径,然后写该路径的前缀。比如我电脑搜出来路径/usr/local/mysql/include/mysql.h ,所以前缀是 /usr/local/mysql --with-mysql-sock --with-mysqli= # 这应该是mysqli 编译配置,后面的参数可以先 find / -name mysql_config /usr/local/mysql/bin/mysql_config /usr/local/src/mysql-5.7.20/scripts/mysql_config --enable-fpm # 开启php-fpm 功能 cp php.ini-development /usr/local/php/etc/php.ini cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf cd /usr/local/src/php-5.6.10/sapi/fpm/ cp init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm service php-fpm start chkconfig --add php-fpm chkconfig php-fpm on 将配置文件中的php-fpm.conf文档复制下。 重启服务 service php-fpm restart 如果在cmake的时候没有指定为用户和用户组www, 可以在php-fpm.conf配置文件中,添加include php-fpm.d/*.conf 然后在创建php-fpm.d文件夹。在该文件夹下创建www.conf ln -s /usr/local/php/etc/php.ini /etc/php.ini 如果不添加如下,php -v读不出来 export PATH=/usr/local/php/bin:$PATH source /etc/profile vi /usr/local/php/etc/php-fpm.conf [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice
安装php5.6版本
查看编译:/usr/local/php/sbin/php -i | grep configure
[www] ;listen = /tmp/php-cgi.sock listen = 127.0.0.1:9000 listen.backlog = -1 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www pm = dynamic pm.max_children = 300 pm.start_servers = 30 pm.min_spare_servers = 30 pm.max_spare_servers = 60 request_terminate_timeout = 400 request_slowlog_timeout = 0 slowlog = var/log/slow.log pm.status_path = /status 查看nginx,php,mysql编译的参数 nginx: /usr/local/nginx/sbin/nginx -v php: /usr/local/php/bin/php -i | grep configure mysql: /usr/local/mysql/bin/mysqlbug | grep configure ps -ef | grep mysql
测试篇
cat /usr/local/nginx/conf/vhost/test.conf server { listen 80; server_name www.test.com; location / { root html; index index.php index.html index.htm ; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } cd /usr/local/nginx/html cat test.php <?php phpinfo(); ?> chown www.www /usr/local/nginx/html/ -R chmod 700 /usr/local/nginx/html/ -R
在浏览器中输入服务器IP/test.php地址,会看到php界面
到此,LNMP环境部署完成!