本文中Linux
命令使用的大部分是絕對路徑(若沒有前面有相應的目錄切換命令)安裝時能夠不考慮路徑問題php
源碼存放目錄:\usr\src\
軟件安裝目錄:\usr\local\軟件名稱
html
這裏須要說明的是源碼存放源代碼的位置,安裝過程能夠指定,軟件運行時跑的是軟件安裝的代碼mysql
相應的最新的源碼包能夠在下列網址中找到ngnixhttp://nginx.org/download/mysqlhttps://www.mysql.com/downloads/phphttp://php.net/downloads.phpcmake(MySQL編譯工具)https://cmake.org/download/boost(MySQL編譯工具)http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
編譯工具及庫文件(yum
安裝)linux
yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-develyum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel
建立軟件存放目錄nginx
mkdir -p /usr/local/cmake
編譯安裝web
tar -xzvf cmake-3.8.0.tar.gzcd cmake-3.8.0/./configure --prefix=/usr/local/cmakemake && make installecho 'export PATH=$PATH:/usr/local/cmake/bin' >> /etc/profilesource /etc/profile
tar xzvf boost_1_59_0.tar.gzcd boost_1_59_0/./bootstrap.sh./b2 install
須要說明的是,這裏Boost
的安裝能夠不指定目錄(不進行相關目錄的建立),一些配置參數能夠經過./b2 --help
命令看到
這裏說明一下,默認存放位置是/usr/local/
下;庫文件存放在/usr/local/bin
;頭文件存放到/usr/local/include
sql
建立MySQL用戶和用戶組bootstrap
groupadd mysqluseradd -g mysql mysql -s /bin/false
建立軟件存放目錄vim
mkdir -p /usr/local/mysql
MySQL數據存放目錄centos
mkdir -p /var/mysql/data
建立日誌存放目錄
mkdir -p /usr/local/mysql/log
對數據存放目錄授予權限
chown -R mysql:mysql /var/mysql/
對日誌目錄授予權限
chown -R mysql:mysql /usr/local/mysql/log
tar -zxvf mysql-boost-5.7.18.tar.gzcd mysql-5.7.18cmake \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DMYSQL_DATADIR=/var/mysql/data\-DMYSQL_TCP_PORT=3306\-DENABLE_DOWNLOADS=1make && make install
選項 | 含義 |
---|---|
DCMAKE_INSTALL_PREFIX | 安裝目錄 |
DMYSQL_UNIX_ADDR | sock文件路徑 |
DDEFAULT_CHARSET | 默認字符集 |
DWITH_INNOBASE_STORAGE_ENGINE | 安裝INNODB |
DWITH_ARCHIVE_STORAGE_ENGINE | 存儲引擎可用值 |
DWITH_BLACKHOLE_STORAGE_ENGINE | 存儲引擎可用值 |
DMYSQL_DATADIR | 設置MySQL數據存放目錄 |
DMYSQL_TCP_PORT | MySQL監聽端口 |
其中MyISAM,MERGE,MEMORYMERGE,CSV是默認編譯生成的不須要明確指定
更多的參數含義能夠參考MySQL官網文章
須要注意的是5.7版本的軟件安裝目錄與以前版本不太同樣,具體介紹
目錄 | 目錄內容 |
---|---|
bin | MySQL服務端,客戶端可執行文件 |
docs | MySQL文檔格式信息 |
include | 包含的頭文件 |
lib | 庫文件 |
man | MySQL手冊信息 |
mysql-test | MySQL測試文件 |
share | 支持文件 |
support-files | MySQL啓動腳本 |
建立MySQL配置文件
cd /usr/local/mysqlvim my.cnf
將以下配置項寫入my.cnf中(這裏須要說明的是MySQL 5.7.18
以後安裝目錄中就沒有my.cnf)
[mysqld]basedir=/usr/local/mysqldatadir=/var/mysql/dataport=3306socket=/usr/local/mysql/tmp/mysql.socklog-error=/usr/local/mysql/log/mysqld.logtmpdir=/tmpmax_allowed_packet=32M#bind-address=127.0.0.1[client]port=3306socket=/usr/local/mysql/tmp/mysql.sockdefault-character-set=UTF8[manager]port=3306socket=/usr/local/mysql/tmp/mysql.sockpid-file=/usr/local/mysql/tmp/manager.piddefault-mysqld-path=/usr/local/mysql/bin/mysqld.bin
開啓MySQL和進行設置
cd /usr/local/mysql/bin./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/var/mysql/data./mysql_ssl_rsa_setup --datadir=/var/mysql/data./mysqld_safe --user=mysql &cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld/etc/init.d/mysqld start
須要注意的是在執行完./mysqld --initialize
命令後會分配一個默認的密碼在日誌文件中,查看密碼
cat /usr/local/mysql/log/mysqld.log
詳細選項參數說明可使用以下命令
./mysqld --verbose --help
也能夠查閱官網MySQL初始化文檔
修改root密碼
mysql -u root -p密碼mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
若是須要設置MySQL了能夠遠程鏈接,能夠進行以下設置
mysql -uroot -p密碼GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '密碼' WITH GRANT OPTION;FLUSH PRIVILEGES;
這裏值得注意的是配置遠程登陸首先須要註釋掉配置文件中bind-address=ip
這句話,才能夠經過設置表權限進行遠程登陸的控制
設置環境變量方便使用SQL
命令
echo 'export PATH = $PATH:/usr/local/mysql/bin'
make[2]: *** [unittest/gunit/CMakeFiles/merge_small_tests-t.dir/merge_small_tests.cc.o] Error 4make[1]: *** [unittest/gunit/CMakeFiles/merge_small_tests-t.dir/all] Error 2make: *** [all] Error 2
錯誤產生緣由:swap
分區不夠
解決辦法:
[root@VM_6_37_centos ~]# dd if=/dev/zero of=/swapfile bs=1k count=2048000[root@VM_6_37_centos ~]# mkswap /swapfile [root@VM_6_37_centos ~]# swapon /swapfile[root@web_1 ~]# dd if=/dev/zero of=/swapfile bs=1k count=2048000 [root@VM_6_37_centos ~]# mkswap /swapfile [root@VM_6_37_centos ~]# swapon /swapfile
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
錯誤產生緣由:沒有啓動MySQL
解決辦法:開啓MySQL
/usr/local/mysql/support-files/mysql.server start
mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
錯誤產生緣由:密碼錯誤
解決辦法:查看日誌文件,找到初始密碼進行登陸(日誌文件目錄在配置文件制定)
cat /usr/local/mysql/log/mysqld.log
這裏須要說明,安裝啓動MySQL遇到錯誤提示多是相同的,咱們能夠根據本身的狀況進行查明
groupadd wwwuseradd -g www www -s /bin/bash/false
cd /usr/src/tar -zxvf nginx-1.12.0.tar.gzcd nginx-1.12.0/./configure --prefix=/usr/local/ngnix --user=www --group=www --with-http_stub_status_module --with_http_ssl_module --with-http_gzip_static_module --with-threadsmake && make install
./configure
選項參數能夠經過./configure --help
查看
能夠查閱官網文檔
建立網站根目錄並修改用戶權限
mkdir /var/wwwchown www:www /var/www
進入配置文件目錄cd /usr/local/ngnix/conf/
cd /usr/local/ngnix/conf/ngnix.confvim ngnix.conf
將網站的根目錄修改成/var/www
vim /usr/local/ngnix/cong/ngnix.conf#在http的server中修改localtion的root屬性http{ server{ localtion / { root /var/www } }}
更多的配置項能夠參考官網文檔
啓動Ngnix
/usr/local/ngnix/sbin/ngnix
須要說明的是Ngnix
有相應的選項參數,能夠經過ngnix -h
進行查看
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -T : test configuration, dump it and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/ngnix/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
值得注意的是-s
選項的參數,有以下四種
名稱 | 含義 |
---|---|
stop | 快速關掉ngnix進程 |
quit | 優雅關掉ngnix進程即等待其它請求結束後在關閉ngnix進程 |
reload | 從新加載配置文件 |
reopen | 從新打開日誌文件 |
到此ngnix
安裝結束
cd /usr/srctar -zxvf php-7.1.4.tar.gzcd php-7.1.4./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlndmake && make install
值得注意的是:./configure
後面的選項能夠經過./configure --help
命令進行詳細查看,須要注意的是php-fpm
模塊時必須安裝的
從編譯後的源碼包中複製配置文件到安裝目錄
cp /usr/src/php-7.1.4/php.ini-production /usr/local/php/etc/php.ini
編輯php-fpm
的配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.confvim /usr/local/php/etc/php-fpm.conf#取消前面的分號pid = run/php-fpm.pidcp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf#改變用戶組及用戶user = wwwgroup = www
將php-fpm
啓動腳本加/etc/init.d/
中並設置執行權限
cp /usr/src/php-7.1.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod 755 /etc/init.d/php-fpm
啓動php-fpm
/etc/init.d/php-fpm start
編輯Ngnix
配置文件ngnix.conf
取消以下顯示內容的註釋
vim /usr/local/ngnix/conf/ngnix.confhttp{ server{ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { root /var/www;#修改網站根目錄 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#注意這行須要修改 include fastcgi_params; } }}
/etc/init.d/php-fpm restart/usr/local/ngnix/sbin/ngnix -s reload
到此安裝完畢
在瀏覽器中輸入相應的IP地址
MYSQL官方文檔
NGNIX官方文檔
PHP官方文檔
LNMP平臺搭建
若是以爲每次到ngnix
的sbin
目錄下進行服務器管理操做比較麻煩能夠將以下的ngnix
啓動腳本加到/etc/init.d/
目錄下
腳本代碼
#!/bin/sh## nginx - this script starts and stops the nginx daemin## chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# pidfile: /usr/local/nginx/logs/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { configtest || return $? stop start}reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo}force_reload() { restart}configtest() { $nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac
具體操做
vim /etc/init.d/ngnix#將上述代碼複製進去,以後進行權限設置chmod 755 /etc/init.d/ngnix