安裝LNMP開發環境

首發於 樊浩柏科學院

本文主要介紹 LNMP 開發環境的安裝,同時列出了 PhpStorm 的安裝過程。源碼包統一放置於/usr/src,軟件統一安裝於/usr/local
預覽圖php

CentOS安裝

安裝

前往 CentOS 的 官方地址 下載 CentOS 6.9 Min 版本鏡像文件。html

新建虛擬機並安裝,可是注意在 新建虛擬機嚮導 設置裏,要選擇 稍後安裝操做系統,並在完成虛擬機安裝嚮導後,經過 編輯虛擬機設置 指定系統鏡像(CD/DVD項)的地址。java

安裝完成 Min 版後,默認未開啓網卡,採用以下方法開啓網卡。mysql

$ vi /etc/sysconfig/network-scripts/ifcfg-eth0

配置信息以下:linux

DEVICE=eth0
HWADDR=00:0C:29:7A:CF:56
TYPE=Ethernet
UUID=216048ec-c974-427e-8b57-5a4c9fe6733e
# 是否開機自啓,將no更改成yes
ONBOOT=no
NM_CONTROLLED=yes
BOOTPROTO=dhcp

配置後重啓便可,不然沒法鏈接到網絡。nginx

配置

1)安裝 epel 解決第三方擴展依賴的問題c++

$ yum install epel-release

2)更新系統git

$ yum -y update

3)安裝必備工具github

# 查看流量
$ yum -y install iptraf
# 經常使用工具
$ yum -y install wget curl lrzsz vim unzip zip
# GCC
$ yum -y install gcc gcc-c++
# cmake
$ yum -y install cmake

4)同步時鐘web

$ yum -y install ntpdate ntp
$ ntpdate time.windows.com && hwclock -w

將如下內容寫入計劃任務:

03 01 * * * /usr/sbin/ntpdate -u time.windows.com  >/var/log/ntpdate.log &

5)修改DNS設置

$ vim /etc/resolv.conf
# 增長主DNS
nameserver 8.8.8.8
# 增長次DNS
nameserver 8.8.4.4

6)修改語言

查看系統語言:

# 若是是zh_CN則爲中文,是en_US則爲英文
$ echo $LANG
# 查看系統語言包
$ locale
# 下載中文語言包
$ yum -y groupinstall chinese-support

經過編輯/etc/sysconfig/i18n配置文件修改。

7)桌面

爲了方便後續開發,在這裏安裝了 GNOME 桌面。

$ yum groupinstall "X Window System"  "Desktop" "Desktop Platform" "Fonts" "Chinese Support [zh]"
# 修改默認啓動模式
$ vim /etc/inittab
id:5:initdefault:

PHP

下載並解壓源碼

PHP 官方地址 下載源碼包。

$ cd /usr/src
$ wget http://am1.php.net/distributions/php-5.6.30.tar.gz
$ tar zxvf php-5.6.30.tar.gz
$ cd php-5.6.30

安裝依賴

能夠經過./configure腳本檢查依賴安裝狀況。

$ yum -y install gd-devel pcre-devel libxml2-devel libjpeg-devel libpng-devel libevent-devel libtool* autoconf* freetype* libstd* gcc44* ncurse* bison* openssl* libcurl* libcurl* libmcrypt*

使用 yum 安裝 libmcrypt 時可能會找不到安裝源,這裏提供了 libmcrypt 的源碼安裝方式。

$ wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
$ tar zxf libmcrypt-2.5.7.tar.gz
$ cd libmcrypt-2.5.7
$ ./configure
$ make && make install

編譯安裝

./configure --prefix=/usr/local/php \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg \
--enable-inline-optimization \
--with-curl \
--with-mcrypt \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-ftp \
--without-pear \
--enable-opcache

$ make && make install

若是在編譯過程當中出現以下錯誤:

configure:error:Don't know how to define struct flock on this system,set-enable-opcache=no

採用以下辦法解決:

$ vim /etc/ld.so.conf.d/local.conf
# 文件追加以下內容
/usr/local/lib
# 運行後從新編譯PHP便可
$ ldconfig

配置

1)php.ini

複製並修改php.ini配置文件。

$ cp ./php.ini-development /usr/local/php/lib/php.ini

開發環境 建議配置修改成以下:

short_open_tag = On
output_buffering = 4096
max_execution_time = 60
# 錯誤顯示和級別
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
# 把PHP報錯記錄到syslog中或指定的文件中(注意目錄權限)
error_log = syslog
# 設置默認時區
date.timezone = PRC

2) php-fpm.conf

複製並修改php-fpm.conf配置文件。

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

建議配置修改成以下:

[global]
# 重要,必定要記錄notice, 及時發現異常狀況
log_level = notice
# 重要,每一個子進程能打開的文件描述符數量上限(系統默認是1024),必定要儘可能提升
rlimit_files = 60000
[www]
# 重要,設置fpm運行狀態查看地址
pm.status_path = /status
# fpm存活狀態檢測地址
ping.path = /ping
# 用於檢測fpm進程是否運行正常
ping.response = pong
# 子進程處理指定的請求數
pm.max_requests = 5000
# 定義慢速日誌文件位置
slowlog = var/log/slow.log
# 慢請求時間上下限,超過此值就記錄到日誌文件中
request_slowlog_timeout = 1s
# 單個腳本運行超時時間,fpm模式下,php.ini中的max_execution_time配置無效,必須指定
request_terminate_timeout = 30s
# 捕獲php程序的報錯,併發送給客戶端(nginx), 若是不配置這項,經過nginx訪問時php報錯,沒法在nginx響應中看到錯誤內容,只會產生50X錯誤
catch_workers_output = yes

系統服務

$ cp /usr/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
$ chmod a+x  /etc/init.d/php-fpm
# 添加到init.d服務中
$ chkconfig --add php-fpm
# 設置服務自動運行
$ chkconfig php-fpm on

啓動

$ ln -s /usr/local/php/bin/php /usr/bin/php
$ ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-pfm
# 配置語法檢測
$ php-pfm -t
$ service php-fpm start

MySQL

用戶

# mysql運行用戶
$ groupadd mysql
$ useradd mysql -g mysql -M
$ mkdir -p /usr/local/mysql
$ mkdir -p /usr/local/mysql/data
$ chown -R mysql:mysql /usr/local/mysql
$ vi /etc/profile
# profile追加以下內容
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
$ source /etc/profile

下載並解壓源碼

從 MySQL 官網下載源碼包,解壓。

$ tar zxvf mysql-5.6.33.tar.gz
$ cd mysql-5.6.33

安裝依賴

$ yum -y install ncurses-devel perl ncurses-devel bison-devel

編譯安裝

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \
$ make && make install

配置

執行初始化:

$ cd /usr/local/mysql
$ ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

修改配置文件:

[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character_set_server=utf8
init_connect='SET NAMES utf8'
# 限制爲本地訪問
bind=127.0.0.1
... ...
[client]
default-character-set=utf8

系統服務

$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chkconfig mysqld on

啓動

$ service mysqld start

權限

$ mysql -u root
mysql> SET PASSWORD = PASSWORD('**********');
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"127.0.0.1"  WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Nginx

準備

# 用戶
$ groupadd www
$ useradd -g www www -s /bin/false -M
# 依賴
$ yum -y install pcre* opensll*

下載並解壓

官網 下載源碼包,並解壓。

$ wget http://nginx.org/download/nginx-1.7.8.tar.gz
$ tar zxvf nginx-1.7.8.tar.gz
$ cd nginx-1.7.8

編譯安裝

$ ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

$ make && make install

配置

nginx.conf文件配置:

user www www;
worker_processes  4;
error_log  logs/error.log  notice;
... ...
http {
  ... ...
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
  ... ...
  server {
    listen 80 default;
    server_name _;
    return 403;
  }
  # 各站配置文件
  include     vhost/*.conf;
  ... ...
}

各站配置文件:

server {
  listen       80;
  server_name  localhost;
  access_log  logs/host.access.log  main;
  location / {
    root   /home/www;
    index  index.html index.htm;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }
}

系統服務

$ vi /etc/init.d/nginx

# 追加入以下內容
#!/bin/bash
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
    start)
        echo -n "Starting Nginx"
        $nginx -c $conf
        echo " done"
    ;;
    stop)
        echo -n "Stopping Nginx"
        killall -9 nginx
        echo " done"
    ;;
    test)
        $nginx -t -c $conf
    ;;
    reload)
        echo -n "Reloading Nginx"
        ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
        echo " done"
    ;;
    restart)
        echo -n "Restart Nginx"
        $0 stop
        sleep 1
        $0 start
        echo " done"
    ;;
    show)
        ps -aux|grep nginx
    ;;
    *)
        echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
    ;;
esac
# 保存並退出

$ cd /etc/init.d
$ chmod +x nginx
$ chkconfig --add nginx
$ chkconfig --level 2345 nginx on
$ chkconfig nginx on

啓動

# 檢測配置
$ service nginx test
$ service nginx start

Phpstorm

依賴

Phpstorm 運行環境依賴 JAVA,這裏使用 yum 安裝 JAVA 環境。

$ yum -y install java

下載並解壓

phpstorm 官網 下載最新安裝包—— Linux版。

$ tar zxvf PhpStorm-10.0.4.tar.gz
$ mv ./PhpStorm-10.0.4 /usr/local/phpstorm

安裝

注意:如下安裝操做是從虛擬機終端操做,且肯定虛擬機是運行於圖形界面模式(init 5),而不是經過 Xshell 端操做。

在虛擬機終端:

$ cd /usr/local/phpstorm/bin
$ ./phpstorm.sh

隨後系統會彈出 phpstorm 的安裝界面,正常安裝便可。

註冊

經過 IDEA註冊碼 便可獲取正版註冊碼。

本地代碼實時同步開發環境

我是在 Win 下使用 PhpStrom 開發代碼,而開發環境爲 VM 下的 LNMP。因此怎樣實時同步本地代碼到開發環境,以便開發和調試呢?採用 VM 提供的 文件共享 便可很好的解決這個問題。

關閉虛擬機後,在路徑爲 編輯虛擬機設置 >>選項>>共享文件夾 的面板上配置需共享文件夾路徑,即本地代碼文件夾,並選中 老是啓用 選項,啓動虛擬機便可。

共享文件夾默認掛載路徑爲/mnt/hgfs,以下:

$ cd /mnt/hgfs
$ ls
# 個人本地代碼文件夾
Code

若是配置共享文件夾路徑後,默認掛載路徑沒有共享文件夾,能夠試試下面的解決辦法:

$ cd /usr/src
$ git clone https://github.com/rasa/vmware-tools-patches.git
$ cd vmware-tools-patches
$ ./patched-open-vm-tools.sh

啓用Win10的Linux子系統

以前的方案是使用虛擬機,到後面是 Docker。其實 Win10 已經支持 Linux 子系統,我此次已經嚐鮮了,我裝的子系統爲 Ubuntu。

一路過來並不順利,在這裏記錄我所遇到的問題以及相應的解決方案。

子系統隨WIN自啓

啓動 Win 後,都須要手動 Bash 才能啓動 Ubuntu,極爲不方便。可使用 vbs 腳原本啓動 Ubuntu,並添加開機啓動項。

首先,建立名爲ubuntu-up.vbs的 vbs 腳本,內容爲:

set ws=wscript.createobject("wscript.shell")
ws.run "C:\Windows\System32\bash.exe",0

而後,在"運行"中鍵入shell:startup,會打開"啓動"列表,將ubuntu-up.vbs保存至此,便可實現 Ubuntu 開機啓動。

子系統配置啓動服務

這裏以 SSH 爲例配置啓動服務。Ubuntu 子系統默認未安裝 SSH,可使用sudo apt-get install openssh-server完成安裝。

爲了不端口衝突,建議 Port 配置成非 22 端口。

先嚐試配置自啓動服務:

$ sudo chmod +x /etc/init.d/ssh
$ cd /etc/init.d/
$ sudo update-rc.d ssh defaults

重啓系統後,發現 sshd 服務並無啓動。查了好久,最後仍是使用 vbs 腳原本實現了。

子系統隨 Win 自啓 啓動腳本ubuntu-up.vbs中,增長以下內容:

ws.run "C:\Windows\System32\bash.exe -c 'sudo /usr/sbin/service ssh --full-restart'",0
注意這裏使用到了 sudo 命令,因此須要配置免密碼使用,見 免密碼使用 sudo 部分。

再次啓動後,sshd 服務就能正常啓動了。

更新 [»]()

相關文章
相關標籤/搜索