不少人在練習部署LNMP環境的時候,大都數是部署在同一個虛擬機上面的。可是實際工做中,咱們通常都是分離部署的。php
今天我就用3臺虛擬機,部署下LNMP環境。以供參考!html
網絡拓撲圖:mysql
首先準備3臺虛擬機:linux
nginx:192.168.1.214nginx
php:192.168.1.202sql
mysql:192.168.1.217數據庫
首先安裝ngix(192.168.1.214):vim
#解決依賴 yum install -y gcc,openssl-devel,pcre-devel,zilb-devel pcre-devel #關閉防火牆和修改selinux service iptables stop chkconfig iptables off sed -i's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
#添加一個nginx用戶 [root@nginx ~]# groupadd -g 108 -r nginx [root@nginx ~]# useradd -u 108 -r -g 108 nginx [root@nginx ~]# tar zxvf nginx-1.6.0.tar.gz [root@nginx ~]# cd nginx-1.6.0 [root@nginx ~]# ./configure --prefix=/usr/local/ --sbin-path=/usr/sbin/nginx/ --conf-path=/etc/nginx/nginx.conf/ --error-log-path=/var/log/nginx/error.log/ --http-log-path=/var/log/nginx/access.log/ --pid-path=/var/run/nginx/nginx.pid/ --lock-path=/var/lock/nginx.lock/ --user=nginx/ --group=nginx/ --with-http_ssl_module/ --with-http_flv_module/ --with-http_stub_status_module/ --with-http_gzip_static_module/ --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/ --with-pcre make && make install
nginx啓動腳本:api
[root@nginx ~]# cat /etc/init.d/nginx #!/bin/sh # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs 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 sleep 1 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 2 esac
添加執行權限:瀏覽器
[root@nginx ~]# chmod +x /etc/init.d/nginx #添加腳本到開機啓動項 [root@nginx ~]# chkconfig --add nginx [root@nginx ~]# chkconfig nginx on [root@nginx ~]# chkconfig nginx --list nginx 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 #啓動nginx [root@nginx ~]# service nginx start 正在啓動 nginx: [肯定] #查看端口是否正常 [root@nginx ~]# netstat -ntlp | grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3889/nginx # Nginx安裝與配置完畢
#安裝MySQL(192.168.1.217)
#注意:這裏是直接解壓MySQL的
#添加MySQL用戶 [root@localhost ~]#groupadd -r mysql [root@localhost ~]#useradd -r -g mysql -s [root@localhost ~]#/sbin/nologin mysql
#解壓到指定路徑 [root@localhost ~]#tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/ [root@localhost ~]#cd /usr/local/ [root@localhost ~]#ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64/ /usr/local/mysql `/usr/local/mysql' -> `/usr/local/mysql-5.5.33-linux2.6-x86_64/' [root@localhost ~]#cd mysql [root@localhost ~]#chown -R root.mysql *
#初始化數據庫;並指定其用戶和data目錄 scripts/mysql_install_db --user=mysql--datadir=/Mysql/data ls /Mysql/data/ lost+found mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index performance_schema test
#沒有報錯並生成以上文件便可 cp support-files/my-large.cnf /etc/my.cnf 配置一個配置文件 vi /etc/my.cnf 更改已下配置選項;具體選項根據本身須要配置 # The MySQL server [mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 4 datadir = /Mysql/data 新增一行
#提供一個服務腳本 cp support-files/mysql.server /etc/rc.d/init.d/mysqld chmod +x /etc/rc.d/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off service mysqld start Starting MySQL... [ OK ]
#用mysql命令啓動成功後錯誤信息 vi /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH ln -sv /usr/local/mysql/include/ /usr/include/mysql `/usr/include/mysql' ->`/usr/local/mysql/include/' vi /etc/man.config
定位到MANPATH MANPATH /usr/man MANPATH /usr/share/man MANPATH /usr/local/man MANPATH /usr/local/share/man MANPATH /usr/X11R6/man MANPATH /usr/local/mysql/man #添加一行 echo /usr/local/mysql/lib/ > /etc/ld.so.conf.d/mysql.conf
#如果想要編譯安裝MySQL能夠參考我以前的文章:
http://liangey.blog.51cto.com/9097868/1626945
安裝PHP
yum install zlib libxml libjpegfreetype gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devellibpng-devel gd-devel curl-devel libmcrypt-devel libxslt* bzip2* -y
#添加nginx用戶 useradd nginx #安裝libiconv-1.14庫 tar zxvf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv echo $? make echo $? make install echo $?
#安裝libmcrypt-2.5.8.tar.gz庫 tar zxvflibmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8 ./configure make make install
#安裝mhash-0.9.9.9 tar zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure make make install
#安裝mcrypt tar zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 /sbin/ldconfig ./configureLD_LIBRARY_PATH=/usr/local/lib make make install
tar zxvf php-5.3.27.tar.gz cd php-5.3.27 ./configure--prefix=/application/php5.3.27 --with-mysqli=mysqlnd / --with-pdo-mysql=mysqlnd/ --with-iconv-dir=/usr/local/libiconv / --with-freetype-dir / --with-jpeg-dir/ --with-png-dir / --with-zlib / --with-libxml-dir=/usr/ --enable-xml / --disable-rpath/ --enable-safe-mode/ --enable-bcmath/ --enable-shmop / --enable-sysvsem/ --enable-inline-optimization / --with-curl / --with-curlwrappers / --enable-mbregex/ --enable-fpm / --enable-mbstring / --with-mcrypt / --with-gd / --enable-gd-native-ttf/ --with-openssl / --with-mhash / --enable-pcntl / --enable-sockets/ --with-xmlrpc/ --enable-zip / --enable-soap/ --enable-short-tags / --enable-zend-multibyte / --enable-static / --with-xsl/ --enable-ftp make && make install
#若make出錯 /home/tools/php-5.3.27/sapi/cli/php:error while loading shared libraries: libmysqlclient.so.18: cannot open sharedobject file: No such file or directory make: *** [ext/phar/phar.php] 錯誤 127
#解決方法 ln -s/application/mysql/lib/libmysqlclient.so.18 /usr/lib64/ make make install ln -s /application/php5.3.27//application/php
#配置PHP cp php.ini-production /application/php/lib/php.ini cd /application/php/etc/ vim php-fpm.conf #修改以下: user = nginx group = nginx listen = 192.168.1.202:9000
#啓動php-fpm /etc/init.d/php-fpm start netstat -lntup|grep 9000 tcp 0 0 192.168.1.202:9000 0.0.0.0:* LISTEN 1422/php-fpm
#整合Nginx和PHP
#這裏是在Nginx(192.168.1.214)下操做:
vim /etc/nginx/nginx.conf location / { root /www; #更改目錄 index index.php index.html index.htm; #添加一個index.php } location ~ \.php$ { root /www;#更改目錄 fastcgi_pass 192.168.1.202:9000; #這裏爲PHP服務器的地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }
vim /etc/nginx/fastcgi_params #添加如下這行: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#建立一個/www目錄,而且建立一個test.php文件 mkdir /www chown nginx:nginx /www/ cd /www vim test.php <?php phpinfo(); ?>
#注意:這裏先不要重啓nginx
#如下操做是在PHP:192.168.1.202中進行
mdkir /www chown -R nginx:nginx /www/ cd /www/ vim test.php <?php phpinfo(); ?>
#重啓php-fpm /etc/init.d/php-fpm restart
#在nginx中重啓nginx服務 /etc/init.d/nginx restart
在瀏覽器中輸入:192.168.1.214/test.php
出現如下頁面,證實nginx和php整合成功了!
#最後是建立一個wordpress站點
建立站點,首先咱們的nginx必需要跟MySQL是互通的。如今咱們先在MySQL下受權nginx這臺服務器可以訪問數據庫: #這裏是在MySQL(192.168.1.217)下操做的: mysql -uroot -proot 登錄數據庫 #建立一個數據庫 mysql> create database wp; mysql> use wp; mysql> grant all privileges on wp.* to 'wpuser'@'192.168.1.%' identified by '123456'; mysql> flush privileges;
#在nginx的/www目錄下面解壓wordpress-4.1-zh_CN.tar.gz [root@localhost www]#tar zxvf wordpress-4.1-zh_CN.tar.gz [root@localhost www]#cp -r wordpress/* .
#在php的/www目錄下面解壓 wordpress-4.1-zh_CN.tar.gz [root@localhost www]#tar zxvf wordpress-4.1-zh_CN.tar.gz [root@localhost www]#cp -r wordpress/* .
#重啓nginx服務和php-fpm服務 /etc/init.d/nginx restart /etc/init.d/php-fpm restart
#瀏覽器中輸入:192.168.1.214,會出現設置的頁面
好了,關於LNMP的分離部署就完成了。若在部署中出現問題,能夠發送到我郵291406980@qq.com