系統(tǒng)信息:
[jason@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[jason@localhost ~]$
一、準(zhǔn)備工作
1.1 安裝或更新gcc gcc-c++
因?yàn)槲野惭b的Centos是絕對(duì)純潔滴。啥也沒有。沒轍了。只有先安裝個(gè)編譯器了。
[jason@localhost ~]$ sudo yum install gcc gcc-c++ -y
1.2 創(chuàng)建需要使用的目錄
source 是用來存放源碼的文件夾。package是用來存放編譯后的庫文件。lnmp是我們真正需要的東西才放里面。(nginx+mariadb+memcached+php7+redis)
[jason@localhost ~]$ mkdir /source /package /lnmpmkdir: 無法創(chuàng)建目錄"/lnmp": 權(quán)限不夠[jason@localhost ~]$ lspackage source[jason@localhost ~]$ sudo mkdir /lnmp /source /package[jason@localhost ~]$
二、開始安裝(nginx篇)
2.1 解壓pcre
[官方網(wǎng)站] http://www.pcre.org/
命令流程:
[jason@localhost ~]$ cd /source/[jason@localhost source]$ ls[jason@localhost source]$ sudo wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz-bash: wget: 未找到命令[jason@localhost source]$ sudo yum install -y wget[jason@localhost source]$ sudo tar -zxvf pcre-8.39.tar.gz
說明:不需要編譯,只需要解壓就行。
2.2 解壓zlib
[官方網(wǎng)站] http://zlib.net/
命令流程:
[jason@localhost source]$ cd /source[jason@localhost source]$ sudo wget http://zlib.net/zlib-1.2.11.tar.gz[jason@localhost source]$ sudo tar -zxvf zlib-1.2.11.tar.gz
說明:不需要編譯,只需要解壓就行。
2.3 安裝nginx
[官方網(wǎng)站] http://nginx.org/
命令流程:
[jason@localhost source]$ pwd/source[jason@localhost source]$ sudo wget http://nginx.org/download/nginx-1.10.2.tar.gz[jason@localhost source]$ sudo tar -zxvf nginx-1.10.2.tar.gz[jason@localhost source]$ cd nginx-1.10.2[jason@localhost nginx-1.10.2]$ sudo ./configure --prefix=/lnmp/nginx --with-pcre=/source/pcre-8.39 --with-zlib=/source/zlib-1.2.11[jason@localhost nginx-1.10.2]$ sudo make[jason@localhost nginx-1.10.2]$ sudo make install
--with-pcre:用來設(shè)置pcre的源碼目錄。
--with-zlib:用來設(shè)置zlib的源碼目錄。
因?yàn)榫幾gnginx需要用到這兩個(gè)庫的源碼。
小章總結(jié):
此處告一段落,nginx安裝完成。我們可以先滿足下自己的欲望心。打開nginx服務(wù)看看Hello World吧。
啟動(dòng)nginx
[jason@localhost nginx-1.10.2]$ sudo /lnmp/nginx/sbin/nginx
啟動(dòng)后可以再瀏覽器中打開頁面,會(huì)顯示nginx默認(rèn)頁面。
不過CentOS7中不再使用iptables,而是默認(rèn)使用firewalld防火墻,我們通常使用的端口一般都被墻掉了,firewalld中可以通過以下兩個(gè)命令查看和開發(fā)端口。
firewall-cmd --query-port=80/tcp //查看80端口是否被墻掉 firewall-cmd --add-port=80/tcp//開放80端口。[jason@localhost nginx-1.10.2]$ sudo firewall-cmd --query-port=80/tcpno[jason@localhost nginx-1.10.2]$ sudo fiewall-cmd -add-port=80/tcpsudo:fiewall-cmd:找不到命令[jason@localhost nginx-1.10.2]$ sudo firewall-cmd -add-port=80/tcpusage: see firewall-cmd man pagefirewall-cmd: error: unrecognized arguments: -add-port=80/tcp[jason@localhost nginx-1.10.2]$ sudo firewall-cmd --add-port=80/tcpsuccess[jason@localhost nginx-1.10.2]$ sudo firewall-cmd --query-port=80/tcpyes[jason@localhost nginx-1.10.2]$
三、開始安裝(php篇)
3.1 安裝libxml2
[官方網(wǎng)站] http://xmlsoft.org/
命令流程:
[jason@localhost nginx-1.10.2]$ cd /source/[jason@localhost source]$ sudo wget ftp://xmlsoft.org/libxml2/libxml2-2.9.4.tar.gz[jason@localhost source]$ sudo tar -zxvf libxml2-2.9.4.tar.gz[jason@localhost libxml2-2.9.4]$ sudo ./configure --prefix=/package/libxml2 --with-python=no [jason@localhost libxml2-2.9.4]$ sudo make[jason@localhost libxml2-2.9.4]$ sudo make install
這里--with-python=no是
3.2 安裝php
[官方網(wǎng)站] http://php.net/
命令流程:
[jason@localhost libxml2-2.9.4]$ cd /source/[jason@localhost source]$ sudo wget http://cn2.php.net/distributions/php-7.1.0.tar.gz[jason@localhost source]$ sudo tar -zxvf php-7.1.0.tar.gz [jason@localhost source]$ cd php-7.1.0[jason@localhost php-7.1.0]$ sudo ./configure --prefix=/lnmp/php7 --with-libxml-dir=/package/libxml2 --with-config-file-path=/lnmp/php7/etc --enable-mbstring --enable-fpm --with-mysqli [jason@localhost php-7.1.0]$ sudo make[jason@localhost php-7.1.0]$ sudo make install
四、開始安裝(mariadb篇)
4.1 安裝cmake
[官方網(wǎng)站] https://cmake.org/
命令流程:
[jason@localhost php-7.1.0]$ cmake-bash: cmake: 未找到命令[jason@localhost php-7.1.0]$ cd ../[jason@localhost source]$ sudo wget https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz[jason@localhost source]$ sudo tar -zxvf cmake-3.7.2.tar.gz [jason@localhost source]$ cd cmake-3.7.2[jason@localhost cmake-3.7.2]$ sudo ./configure --prefix=/package/cmake[jason@localhost cmake-3.7.2]$ sudo make[jason@localhost cmake-3.7.2]$ sudo make install[jason@localhost cmake-3.7.2]$ sudo vim /etc/rc.local [jason@localhost cmake-3.7.2]$ export PATH=/package/cmake/bin:$PATH
4.2 安裝ncurses
[官方網(wǎng)站] http://ftp.gnu.org/gnu/ncurses/
命令流程:
[jason@localhost cmake-3.7.2]$ cd ../[jason@localhost source]$ sudo wget http://ftp.gnu.org/gnu/ncurses/ncurses-6.0.tar.gz[jason@localhost source]$ sudo tar -zxvf ncurses-6.0.tar.gz [jason@localhost source]$ cd ncurses-6.0[jason@localhost ncurses-6.0]$ sudo ./configure --prefix=/package/ncurese[jason@localhost ncurses-6.0]$ sudo make[jason@localhost ncurses-6.0]$ sudo make install
4.3 安裝mariadb
[官方網(wǎng)站] https://mariadb.org/
命令流程:
[jason@localhost ncurses-6.0]$ cd ../[jason@localhost ncurses-6.0]$ sudo su[root@localhost source]# wget http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-5.5.54/source/mariadb-5.5.54.tar.gz[root@localhost source]# tar -zxvf mariadb-5.5.54.tar.gz [root@localhost mariadb-5.5.54]# sudo useradd -M -s /sbin/nologin mysql[root@localhost mariadb-5.5.54]# sudo mkdir -p /data/mariadb[root@localhost mariadb-5.5.54]# sudo chown -R mysql.mysql /data/mariadb[root@localhost source]# cd mariadb-5.5.54[root@localhost mariadb-5.5.54]# yum install -y bison ncurses ncurses-devel zlib-devel[root@localhost mariadb-5.5.54]# cmake . -DCMAKE_INSTALL_PREFIX=/lnmp/mariadb -DMYSQL_DATADIR=/data/mariadb -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EXTRA_CHARSETS=1 -DWITH_SSL=bundled -DWITH_ZLIB=bundled[root@localhost mariadb-5.5.54]# make &&make install
2. 初始化數(shù)據(jù)庫
[root@localhost mariadb-5.5.54]# cd /lnmp/mariadb/[root@localhost mariadb]# scriptsscripts[root@localhost mariadb]# scripts/mysql_install_db --datadir=/data/mariadb/ --user=mysql
3. 提供腳本
[root@localhost mariadb]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld[root@localhost mariadb]# chmod +x /etc/rc.d/init.d/mysqld [root@localhost mariadb]# chkconfig --add mysqld[root@localhost mariadb]# chkconfig mysqld on[root@localhost mariadb]# cp support-files/my-large.cnf /etc/my.cnfcp:是否覆蓋"/etc/my.cnf"? y[root@localhost mariadb]# vim /etc/my.cnf
#vim /etc/my.cnf 編輯配置文件[mysqld]段填寫如下內(nèi)容
[mysqld]
datadir = /mydata/data 數(shù)據(jù)目錄
thread_concurrency = 4 設(shè)置線程數(shù)=核心數(shù)x2
或者:
thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = on
skip_name_resolve = on
4.啟動(dòng)數(shù)據(jù)庫
service mysqld start
ss -ntl | grep :3306
5.初始化數(shù)據(jù)庫
[root@localhost mariadb]# /lnmp/mariadb/bin/mysql_secure_installation
之后開始初始化
/lnmp/mariadb/bin/mysql_secure_installation:行379: find_mysql_client: 未找到命令NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] y ... Success!By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y ... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB![root@localhost mariadb]#
二、開始配置(nginx篇)
2.1 修改nginx.conf
[root@localhost nginx]# vim conf/nginx.conf[root@localhost nginx]# pwd/lnmp/nginx
2.2 找到如下內(nèi)容,并刪除紅色標(biāo)記的字符
# 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 $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root
2.3 輸出phpinfo文件
echo "<?php echo phpinfo(); ?>" > /lnmp/nginx/html/index.php
2.4 啟動(dòng)nginx
[root@localhost nginx]# /lnmp/nginx/sbin/nginx -s reload
小章總結(jié):
完成上面的修改就可以讓nginx來轉(zhuǎn)發(fā)php的動(dòng)態(tài)腳本請(qǐng)求。
不過目前還不能打開php文件,因?yàn)檫€沒有打開php-fpm。
我們繼續(xù)向下看。
三、開始配置(php-fpm)
3.1 copy默認(rèn)配置文件
[root@localhost nginx]# cd /source/php-7.1.0[root@localhost php-7.1.0]# cp php.ini-development /lnmp/php7/etc/php.ini[root@localhost php-7.1.0]# cd /lnmp/php7/etc/[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf[root@localhost etc]# cd /lnmp/php7/etc/php-fpm.d/[root@localhost php-fpm.d]# cp www.conf.default www.conf
3.2 運(yùn)行php-fpm
[root@localhost php-fpm.d]# /lnmp/php7/sbin/php-fpm -c /lnmp/php7/etc/php.ini
二、常用命令
nginx常用命令
啟動(dòng)nginx
# /lnmp/nginx/sbin/nginx
重啟nginx
# /lnmp/nginx/sbin/nginx -s reload
關(guān)閉nginx
# /lnmp/nginx/sbin/nginx -s stop
php-fpm常用命令
啟動(dòng)php-fpm
# /lnmp/php7/sbin/php-fpm -c /lnmp/php7/etc/php.ini
重啟php-fpm
# kill -SIGUSR2 `cat /lnmp/php7/var/run/php-fpm.pid`
關(guān)閉php-fpm
# kill -SIGINT `cat /lnmp/php7/var/run/php-fpm.pid`
信號(hào)解釋:
SIGINT, SIGTERM 立刻終止
SIGQUIT 平滑終止
SIGUSR1 重新打開日志文件
SIGUSR2 平滑重載所有worker進(jìn)程并重新載入配置和二進(jìn)制模塊
[root@localhost php7]# ll /source/php-7.1.0/sapi/fpm/config.m4 Makefile.frag php-fpm.service.inCREDITS php-fpm status.htmlfpm/ php-fpm.8 status.html.ininit.d.php-fpm php-fpm.8.in tests/init.d.php-fpm.in php-fpm.conf www.conf.libs/ php-fpm.conf.in www.conf.inLICENSE php-fpm.service [root@localhost php7]# cp /source/php-7.1.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm cp:是否覆蓋"/etc/init.d/php-fpm"? y[root@localhost php7]# chmod +x /etc/init.d/php-fpm [root@localhost php7]# service php-fpm restart
[root@localhost php7]# killall php-fpm
bash: killall: 未找到命令
[root@localhost php7]# yum install psmisc -y
Memcached 是一個(gè)高性能的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫負(fù)載。它通過在內(nèi)存中緩存數(shù)據(jù)和對(duì)象來減少讀取數(shù)據(jù)庫的次數(shù),從而提高動(dòng)態(tài)、數(shù)據(jù)庫驅(qū)動(dòng)網(wǎng)站的速度。
Memcached基于一個(gè)存儲(chǔ)鍵/值對(duì)的hashmap。其守護(hù)進(jìn)程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過memcached協(xié)議與守護(hù)進(jìn)程通信。
當(dāng)然memcached分為服務(wù)端和客戶端。服務(wù)端用來存放緩存,客戶端用來操作緩存。
客戶端有兩種常見的實(shí)現(xiàn)方式。
第一種是用php代碼根據(jù)服務(wù)端的通訊規(guī)則自己寫一個(gè)。
第二種是安裝php擴(kuò)展庫(php-memcached)。
二、memcached(服務(wù)端篇)
首先先安裝memcached依賴庫libevent。
2.1 安裝libevent
[官方網(wǎng)站] http://libevent.org/
命令流程:
[root@localhost source]# cd /source[root@localhost source]# wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz[root@localhost source]# tar -zxvf libevent-2.0.22-stable.tar.gz [root@localhost source]# cd libevent-2.0.22-stable[root@localhost libevent-2.0.22-stable]# ./configure --prefix=/package/libevent[root@localhost libevent-2.0.22-stable]# make &&make install
2.2 安裝memcached
[官方網(wǎng)站] http://memcached.org/
命令流程:
[root@localhost source]# wget http://memcached.org/files/memcached-1.4.34.tar.gz[root@localhost source]# tar -zxvf memcached-1.4.34.tar.gz [root@localhost source]# cd memcached-1.4.34[root@localhost memcached-1.4.34]# ./configure --prefix=/lnmp/memcached --with-libevent=/package/libevent/[root@localhost memcached-1.4.34]# make &&make install
本章總結(jié):
通過以上操作就很簡(jiǎn)單的把memcached服務(wù)端編譯好了。這時(shí)候就可以打開服務(wù)端進(jìn)行工作了。
[root@localhost memcached-1.4.34]# /lnmp/memcached/bin/memcached -d -m 64 -l 127.0.0.1 -p 11211 -u root -c 1024 –P /tmp/memcached.pid
啟動(dòng)參數(shù)說明:
-d 選項(xiàng)是啟動(dòng)一個(gè)守護(hù)進(jìn)程。
-m 分配給Memcache使用的內(nèi)存數(shù)量,單位是MB,默認(rèn)64MB。
-l 監(jiān)聽的IP地址。(默認(rèn):INADDR_ANY,所有地址)
-p 設(shè)置Memcache的TCP監(jiān)聽的端口,最好是1024以上的端口。
-u 運(yùn)行Memcache的用戶,如果當(dāng)前為root的話,需要使用此參數(shù)指定用戶。
-c 選項(xiàng)是最大運(yùn)行的并發(fā)連接數(shù),默認(rèn)是1024。
-P 設(shè)置保存Memcache的pid文件。
三、memcached(客戶端篇)
php-memcached
這種客戶端比較麻煩了,他是生成php的擴(kuò)展庫文件memcached.so。
首先先安裝memcached依賴庫zlib、libmemcached。
3.1 安裝libmemcached
[官方網(wǎng)站] http://libmemcached.org/
命令流程:
[root@localhost source]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz[root@localhost source]# tar -zxvf libmemcached-1.0.18.tar.gz [root@localhost source]# cd libmemcached-1.0.18[root@localhost libmemcached-1.0.18]# ./configure --prefix=/package/libmemcached[root@localhost libmemcached-1.0.18]# make &&make install
3.2 安裝zlib
[官方網(wǎng)站] http://zlib.net/
命令流程:
[root@localhost zlib-1.2.11]# wget http://zlib.net/zlib-1.2.8.tar.gz[root@localhost zlib-1.2.11]# tar -zxvf zlib-1.2.8.tar.gz[root@localhost zlib-1.2.11]# ./configure --prefix=/package/zlib[root@localhost zlib-1.2.11]# make &&make install
3.3 安裝m4
[官方網(wǎng)站] http://www.gnu.org/software/m4/m4.html
命令流程:
[root@localhost source]# wget http://ftp.gnu.org/gnu/m4/m4-1.4.17.tar.gz[root@localhost source]# tar -zxvf m4-1.4.17.tar.gz [root@localhost source]# cd m4-1.4.17[root@localhost m4-1.4.17]# ./configure --prefix=/package/m4[root@localhost m4-1.4.17]# make &&make install[root@localhost m4-1.4.17]# export PATH=/package/m4/bin:$PATH
3.4 安裝php-memcached
[官方網(wǎng)站] http://pecl.php.net/package/memcached
命令流程:
[root@localhost source]# wget https://github.com/php-memcached-dev/php-memcached/archive/php7.zip[root@localhost source]# unzipbash: unzip: 未找到命令[root@localhost source]# yum install -y unzip[root@localhost source]# unzip php7.zip [root@localhost source]# cd php-memcached-php7/[root@localhost php-memcached-php7]# yum install autoconf -y[root@localhost php-memcached-php7]# /lnmp/php7/bin/phpize [root@localhost php-memcached-php7]# ./configure --with-php-config=/lnmp/php7/bin/php-config --with-zlib-dir=/package/zlib --with-libmemcached-dir=/package/libmemcached --disable-memcached-sasl[root@localhost php-memcached-php7]# make &&make installInstalling shared extensions: /lnmp/php7/lib/php/extensions/no-debug-non-zts-20160303/
Centos7 編譯配置Redis
四 安裝redis
[官方網(wǎng)站] https://redis.io/download
命令流程:
[root@localhost php7]# pwd/lnmp/php7[root@localhost php7]# cd /source/[root@localhost source]# wget http://download.redis.io/releases/redis-3.2.6.tar.gz[root@localhost source]# tar -zxvf redis-3.2.6.tar.gz [root@localhost source]# make [root@localhost source]# make install[root@localhost source]# cp -rf redis-3.2.6 /lnmp/redis[root@localhost source]# /lnmp/redis/src/redis-server &[1] 106878設(shè)置防火墻(沒開啟防火墻的可以跳過)firewall-cmd --zone=public --add-port=6379/tcp --permanentfirewall-cmd --reload [root@localhost source]# firewall-cmd --zone=public --add-port=11211/tcp --permanentsuccess[root@localhost source]# firewall-cmd --zone=public --add-port=443/tcp --permanentsuccess[root@localhost source]# firewall-cmd --zone=public --add-port=3306/tcp --permanentsuccess[root@localhost source]# firewall-cmd --reloadsuccess
安裝PHP-redis模塊 php7
[root@localhost source]# wget https://github.com/phpredis/phpredis/archive/php7.zip -O php7redis.zip[root@localhost source]# unzip php7redis.zip Archive: php7redis.zip[root@localhost phpredis-php7]# phpize[root@localhost phpredis-php7]# ./configure [root@localhost phpredis-php7]# make &&make install[root@localhost phpredis-php7]# vim /lnmp/php7/etc/php.ini
聯(lián)系客服