1. 程式人生 > >Chapter one:安裝配置LNMP

Chapter one:安裝配置LNMP

lnmp

  • 源碼安裝配置LNMP


更新時間:2016-08-02

系統環境:CentOS 6.5

軟件環境:nginx 1.8.1、mysql 5.6.22、php 5.6.19

本機IP :192.168.1.88、192.168.1.36


  • 關閉防火墻和安全機制

#service iptables stop

#setenforce 0

#getenforce

【nginx】

1. 創建用戶

#groupadd nginx

#useradd -M -s /sbin/nologin nginx -g nginx

2. 編譯安裝

#yum -y install pcre-devel zlib-devel gcc

# tar zvxf nginx-1.8.1.tar.gz -C /usr/src/

# cd /usr/src/nginx-1.8.1/

# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_flv_module --with-http_gzip_static_module

#make && make install

3. 創建軟連接
#ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

4.檢查配置文件

# nginx -t

5. 啟動nginx服務

#nginx

#netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17295/nginx

  • 瀏覽器訪問本機IP

技術分享

nginx配置優化

worker_processes auto //nginx
要開啟的進程數,一般等於cpu的總核數 (設置為“auto”將嘗試自動檢測它)

worker_rlimit_nofile 102400 //
這個指令是指當一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那麽均勻,所以最好與ulimit -n的值保持一致。

worker_connections 5000 //
每個進程允許的最多連接數,理論上每臺nginx服務器的最大連接數為worker_processes*worker_connections

keepalive_timeout 60 //keepalive
超時時間

access_log /var/log/nginx/access.log //
設置nginx是否將存儲訪問日誌

error_log /var/log/nginx/error.log //
告訴nginx只能記錄嚴重的錯誤



【mysql】

1.創建用戶

#useradd mysql
2.編譯安裝
# yum -y install ncurses ncurses-devel gcc libtool gcc-c++ make cmake
#tar zvxf mysql-5.6.22.tar.gz -C /usr/src

#cd /usr/src/mysql-5.6.22/

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DSYSCONFDIR=/etc \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DENABLE_DOWNLOADS=1 \

-DWITH_READLINE=1 \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DMYSQL_TCP_PORT=3306 \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=all \

-DMYSQL_USER=mysql

#make && make install

3. 更改屬主屬組

#chown -R mysql:mysql /usr/local/mysql/

初始化:

#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

拷貝文件:

# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

添加啟動mysqld服務:

[[email protected] ~]# chkconfig --add mysqld

[[email protected] ~]# chkconfig mysqld on

[[email protected] ~]# service mysqld start

Starting MySQL.. [確定]

軟連接:

[[email protected] ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/


4. 設置mysql密碼

#mysqladmin -uroot password ‘newpass‘
修改密碼報錯:
mysqladmin: connect to server at ‘localhost‘ failed
error: ‘Access denied for user [email protected] (using password: YES)‘
進數據庫更改密碼:
  • 1.service mysqld stop

  • 2. mysqld_safe --skip-grant-tables &

  • 3. mysql -uroot -p 回車進入

  • >use mysql;
    > update user set password=PASSWORD("newpass")where user="root";

    > flush privileges;
    更新權限
    > quit
    退出

  • 4.service mysqld restart

  • 5. mysql -uroot -pnewpass 進入


【PHP】

1. 編譯安裝

# yum -y install openssl openssl-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

# tar zvxf php-5.6.19.tar.gz -C /usr/src

# cd /usr/src/php-5.6.19/

# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5 --enable-fpm --with-fpm-user=php --with-fpm-group=php --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 --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 --enable-opcache

報錯:

1. configure: error: mcrypt.h not found. Please reinstall libmcrypt.

#tar zvxf libmcrypt-2.5.7.tar.gz -C /usr/src/

#cd /usr/src/libmcrypt-2.5.7/

#./configure --prefix=/usr/local/libmcrypt && make && make install

2. 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 # 添加該行

# ldconfig //使之生效

繼續編譯安裝:
# make && make install


2. 配置php-fpm模塊

[[email protected] php-5.6.19]# cp php.ini-development /usr/local/php5/php.ini

[[email protected] ~]# cd /usr/local/php5/etc/

[[email protected] etc]# ls

pear.conf php-fpm.conf.default

[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf

[[email protected] etc]# useradd -M -s /sbin/nologin php

編輯配置文件

# vim php-fpm.conf

25 pid = run/php-fpm.pid

131 user = php

132 group =php

236 pm.max_children = 50

241 pm.start_servers = 20

246 pm.min_spare_servers = 5

251 pm.max_spare_servers = 35

啟動php-fpm

[[email protected] etc]# /usr/local/php5/sbin/php-fpm

[[email protected] etc]# netstat -anpt | grep php-fpm

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 45716/php-fpm


3.將php-fpm模塊加入nginx配置文件

#vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name www.baidu.com;

charset utf-8;

access_log logs/host.access.log main;

location / {

root html/baidu;

index index.html index.htm index.php;

}

location ~ \.php$ {

root html/baidu;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

重啟nginx

[[email protected] etc]# killall nginx

[[email protected] etc]# netstat -anpt | grep nginx

[[email protected] etc]# nginx

[[email protected] etc]# netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45787/nginx


4. 測試

域名映射

# vim /etc/hosts

192.168.1.36 www.baidu.com

創建網頁目錄

#mkdir /usr/local/nginx/html/baidu

測試

# echo Baidu > /usr/local/nginx/html/baidu/index.html

# elinks --dump 192.168.1.36

Baidu


測試nginxPHP連通性

[[email protected] baidu]# pwd

/usr/local/nginx/html/baidu

[[email protected] baidu]# cat 2.php

<?php

phpinfo();

?>

技術分享

測試數據庫和PHP連通性


#vim /usr/local/nginx/html/baidu/test.php

<?php

$link=mysql_connect(‘localhost‘,‘root‘,‘123456‘); //連接mysql數據庫

if($link) echo "<h1>恭喜你,大功告成!!</h1>"; //連接成功則返回信息

mysql_close(); //關閉數據庫連接

?>


[[email protected] html]# elinks --dump192.168.1.36/test.php

恭喜你,大功告成!!

技術分享


在網上查詢的,未實驗過。
mysql_connect這個模塊將在未來棄用,請你使用mysqli或者PDO來替代。

解決方法1:

禁止PHP報錯

display_errors = On

改為

display_errors = Off

鑒於這個服務器都是給用戶用的,有時候他們需要報錯,不能這做,讓他們改程序吧,看方案2.

解決方法2:

常用的php語法連接MySQL如下

<?php


$link = mysql_connect(‘localhost‘, ‘user‘, ‘password‘);

mysql_select_db(‘dbname‘, $link);


改成mysqi

<?php

$link = mysqli_connect(‘localhost‘, ‘user‘, ‘password‘, ‘dbname‘);

常用mysql建表SQL如下

<?php

// 老的

mysql_query(‘CREATE TEMPORARY TABLE `table`‘, $link);

// 新的

mysqli_query($link, ‘CREATE TEMPORARY TABLE `table`‘);

解決方法三:
在php程序代碼裏面設置報警級別

<?php

error_reporting(E_ALL ^ E_DEPRECATED);



本文出自 “一個Linux小白-學習運維” 博客,請務必保留此出處http://zhaojia.blog.51cto.com/12886290/1923697

Chapter one:安裝配置LNMP