搭建LNMP經驗分享
花了一個禮拜總算是搭建了lnmp集群(也就是傳統的web網站架構),下面就來分享一下,搭建的過程,以及遇到的各種坑。
A.首先先搭建nginx服務:
1.先安裝pcre(偽靜態)庫
yum install pcre pcre-devel -y
2.在普通用戶的家目錄下面創建一個tools目錄(這個目錄是用來放軟件的)
mkdir -p /home/oldboy/tools
3.進入到nginx官方網站http://nginx.org/,下載nginx
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
4.解壓軟件包
tar xf nginx-1.6.3.tar.gz
5.創建
useradd www -s /sbin/nologin -M
6.安裝openssl
yum install openssl openssl-devel -y
7.配置文件的安裝
./configure --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.6.3/
假如配置的時候出現以下問題:./configure --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.6.3/
checking for OS
+ Linux 2.6.32-573.el6.i686 i686
checking for C compiler ... not found
./configure: error: C compiler cc is not found
是因為沒有安裝yum -y install gcc
8.編譯安裝nginx
make
make install
9.為/application/nginx-1.6.3這個目錄創建一個軟鏈接
ln -s /application/nginx-1.6.3/ /application/nginx
10.啟動並檢查安裝結果
/application/nginx/sbin/nginx 啟動
ps -ef|grep "nginx"
netstat -lntup|grep 80
lsof -i :80
/application/nginx/sbin/nginx -t 檢查nginx語法是否出錯
/application/nginx/sbin/nginx -V 查看版本已經安裝的詳細情況
[[email protected]_185_169_centos application]# /application/nginx/sbin/nginx
nginx: [alert] could not open error log file: open() "/application/nginx-1.6.3//logs/error.log" failed (2: No such file or directory)
2017/04/18 21:00:12 [emerg] 5735#0: open() "/application/nginx-1.6.3//logs/access.log" failed (2: No such file or directory)
當啟動nginx的時候,出現以上報錯時,沒有logs和裏面的日誌文件,需要重新創建一個
B.安裝mysql數據庫
1.下載軟件
wget -q http:
//mirrors
.sohu.com
/mysql/MySQL-5
.6
/mysql-5
.6.35-linux-glibc2.5-x86_64.
tar
.gz
2.解壓移動
tar xf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mkdir -p /application
mv mysql-5.6.35-linux-glibc2.5-x86_64 /application/mysql-5.6.35
3.創建用戶並授權
useradd -s /sbin/nologinx -M mysql
chown -R mysql.mysql /application/mysql-5.6.35/
4.初始化數據庫
cd /application/mysql-5.6.35/
cp support-files/my-default.cnf /etc/my.cnf
/scripts/mysql_install_db --basedir=/application/mysql-5.6.35/ --datadir=/application/mysql-5.6.35/data/ --user=mysql
5.並更改mysql默認配置裏面的默認路徑(因為筆者使用的是自己的路徑)
sed -i ‘s#/usr/local/mysql#/application/mysql-5.6.35#g‘ /application/mysql-5.6.35//bin/mysqld_safe
6.啟動登陸並且加入PATH路徑
/application/mysql-5.6.35//bin/mysqld_safe &
lsof -i :3306
PATH="/application/mysql-5.6.35/bin/:$PATH"
echo "PATH="/application/mysql-5.6.35/bin/:$PATH"" >>/etc/profile
7.簡化mysql啟動
sed -i ‘s#/usr/local#/application#g‘ support-files/mysql.server
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
8.並加入開機自啟動
chkconfig --add mysqld
chkconfig --list mysqld
9.登陸並測試
[[email protected] mysql-5.6.35]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.00 sec)
mysql> create database oldboy;
Query OK, 1 row affected (0.00 sec)
mysql> create database oldgirl;
Query OK, 1 row affected (0.00 sec)
mysql> show databases like ‘old%‘;
+-----------------+
| Database (old%) |
+-----------------+
| oldboy |
| oldgirl |
+-----------------+
2 rows in set (0.00 sec)
C.PHP安裝
1.安裝PHP基礎庫
yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel libpng-devel gd-devel libcurl-devel libxslt-devel
2.查看PHP基礎庫是否安裝完成
rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel libpng-devel gd-devel libcurl-devel libxslt-devel
libxml2-devel-2.7.6-21.el6_8.1.x86_64
gd-devel-2.0.35-11.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
libxslt-devel-1.1.26-2.el6_3.1.x86_64
libcurl-devel-7.19.7-53.el6_9.x86_64
libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64
freetype-devel-2.3.11-17.el6.x86_64
libpng-devel-1.2.49-2.el6_7.x86_64
3.yum安裝的時候,這個包libiconv-devel是沒有的,所有需要另外安裝
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar xf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
4.下載epel源,並安裝PHP相關擴展庫
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install -y libmcrypt-devel
yum install -y mhash
yum install -y mcrypt
5.確認PHP擴展庫是否安裝正確
rpm -qa mcrypt mhash libmcrypt-devel
6.編譯安裝PHP5.5.32(這個是有mysql數據庫的情況下)
cd /home/oldboy/tools/
#上傳php包
tar xf php-5.5.32.tar.gz
cd php-5.5.32
./configure \
--prefix=/application/php5.5.32 \
--with-mysql=/application/mysql/ \
--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-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--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-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no
這個是沒有mysql的情況下
./configure \
--prefix=/application/php5.5.32 \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=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-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--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-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no
7.防止等會編譯安裝的時候出錯
ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
touch ext/phar/phar.phar
8.開始編譯安裝並創建軟鏈接,以及啟動php
make
make install
9.創建軟鏈接
ln -s /application/php5.5.32/ /application/php
10.配置PHP解析文件
cd /home/oldboy/tools/
cp php.ini-production /application/php/lib/php.ini
11.配置php-fpm配置文件
cd /application/php/etc/
cp php-fpm.conf.default php-fpm.conf
12.啟動php
/application/php/sbin/php-fpm
13.檢查進程和端口
ps -fe |grep php-fpm
lsof -i:9000
至此LNMP環境搭建完成,謝謝大家
搭建LNMP經驗分享