第一步安裝nginx1.10.3
javascript
優化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\nexport 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基礎環境搭建:php
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.confcss
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的配置文件中html
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內存)java
下載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改下路徑node
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 configuremysql
[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
測試篇 linux
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環境部署完成!nginx