lnmp平臺搭設
只是在一臺服務器上搭設,為centos7.2環境
安裝步驟:
安裝編譯工具及庫文件
# yum -y install make apr* autoconf automake curl-devel gcc gcc-c++ openssl openssl-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* libtool* libxml2 libxml2-devel patch libcurl-devel bzip2-devel freetype-devel
因為centos7.2默認安裝了mariadb-libs,所以先要卸載掉
#rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs
註: 相關依賴包的作用
cmake:由於從MySQL5.5版本開始棄用了常規的configure編譯方法,所以需要CMake編譯器,用於設置mysql的編譯參數。如:安裝目錄、數據存放目錄、字符編碼、排序規則等。
Boost #從MySQL 5.7.5開始Boost庫是必需的,mysql源碼中用到了C++的Boost庫,要求必須安裝boost1.59.0或以上版本
GCC是Linux下的C語言編譯工具,mysql源碼編譯完全由C和C++編寫,要求必須安裝GCC
bison:Linux下C/C++語法分析器
ncurses:字符終端處理庫
# tar -xvf cmake-3.5.2.tar.gz
# cd cmake-3.5.2/
# ./bootstrap && gmake && gmake install
註意:cmake –version ---查看cmake版本
對ncurses-5.9.tar.gz包的操作
# tar -xvf ncurses-5.9.tar.gz
# cd ncurses-5.9/
# ./configure && make && make install
對bison-3.0.4.tar.gz包的操作
# tar -xvf bison-3.0.4.tar.gz
# cd bison-3.0.4/
# ./configure && make && make install
對boost_1_59_0.tar.gz的操作
# tar -xvf boost_1_59_0.tar.gz
# mv boost_1_59_0 /usr/local/boost
創建mysql用戶和用戶組及目錄
# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
創建mysql安裝目錄
#mkdir /usr/local/mysql ---創建目錄
#mkdir /usr/local/mysql/data ---數據庫目錄
編譯安裝mysql
# tar -xvf mysql-5.7.14.tar.gz
# cd mysql-5.7.14/
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql [MySQL安裝的根目錄]
-DMYSQL_DATADIR=/usr/local/mysql /data [MySQL數據庫文件存放目錄]
-DSYSCONFDIR=/etc [MySQL配置文件所在目錄]
-DWITH_MYISAM_STORAGE_ENGINE=1 [添加MYISAM引擎支持]
-DWITH_INNOBASE_STORAGE_ENGINE=1 [添加InnoDB引擎支持]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 [添加ARCHIVE引擎支持]
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock [指定mysql.sock位置]
-DWITH_PARTITION_STORAGE_ENGINE=1 [安裝支持數據庫分區]
-DEXTRA_CHARSETS=all [使MySQL支持所有的擴展字符]
-DDEFAULT_CHARSET=utf8 [設置MySQL的默認字符集為utf8]
-DDEFAULT_COLLATION=utf8_general_ci [設置默認字符集校對規則]
-DWITH-SYSTEMD=1 [可以使用systemd控制mysql服務]
-DWITH_BOOST=/usr/local/boost [指向boost庫所在目錄]
註2:為了加快編譯速度可以按下面的方式編譯安裝
# make -j $(grep processor /proc/cpuinfo | wc –l) && make install
-j參數表示根據CPU核數指定編譯時的線程數,可以加快編譯速度。默認為1個線程編譯。
註3:若要重新運行cmake配置,需要刪除CMakeCache.txt文件,如果服務器硬件不行,只能使用 make && make install 去編譯,我測試過,服務器會報make內部錯誤,其實就是資源不夠用了,這裏切記
# make clean
# rm -rf CMakeCache.txt
優化Mysql的執行路徑
# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
# source /etc/profile
設置權限並初始化MySQL系統授權表
# cd /usr/local/mysql
# chown -R mysql:mysql .
# bin/mysqld --initialize--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
註1:以root初始化操作時要加--user=mysql參數,生成一個隨機密碼(註意保存登錄時用)
註2:MySQL 5.7.6之前的版本執行這個腳本初始化系統數據庫
# /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 5.7.6之後版本初始系統數據庫腳本(本文使用此方式初始化)
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
但是很少一次就能完成的,這是我在初始化時出現的錯誤,大家可以參考下
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-04-26T08:18:26.037908Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-26T08:18:26.039282Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2018-04-26T08:18:26.039315Z 0 [ERROR] Aborting
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-04-26T08:19:26.814450Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-26T08:19:26.816048Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2018-04-26T08:19:26.816082Z 0 [ERROR] Aborting
我上完查了一下,說是/初始化data目錄下有數據文件被終止,於是我就把文件/data/下面的文件移動到了別處,再次初始化的
# mv ./* /data/
# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-04-26T08:20:48.352191Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-26T08:20:48.633613Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-04-26T08:20:48.674899Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-04-26T08:20:48.746230Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b9ae4c35-492a-11e8-9783-000c29b38466.
2018-04-26T08:20:48.748467Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-04-26T08:20:48.750974Z 1 [Note] A temporary password is generated for root@localhost: mZxo(zeof8Z.
註意:如果使用–initialize參數初始化系統數據庫之後,會生成root用戶的一個臨時密碼“mZxo(zeof8Z".
修改文件中配置選項,如下所示,添加如下配置項.
# cp support-files/my-default.cnf /etc/my.cnf
# vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
socket = /usr/local/mysql/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
配置mysql自動啟動
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig mysqld on
# systemctl start mysqld
啟動數據庫
[root@localhost share]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.14
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>
退出重新設置密碼
# mysqladmin -uroot -p password ‘123456‘
Enter password: 此處輸入原來的密碼
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
安裝nginx
解壓zlib和pcre,不需要編譯
# tar -xvf zlib-1.2.8.tar.gz
# tar -xvf pcre-8.39.tar.gz
創建用戶www,作為啟用服務時的程序用戶
# groupadd www
# useradd -g www www -s /sbin/nologin
解壓源碼包,編譯安裝,並啟動
# tar -xvf nginx-1.10.2.tar.gz
# cd nginx-1.10.2/
# ./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module
--with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
--with-pcre=/data/src/pcre-8.39 --with-zlib=/data/src/zlib-1.2.8 --with-http_ssl_module
--with-http_gzip_static_module --user=www --group=www
# make && make install
# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/
# nginx
# ps -ef |grep nginx
root 73290 1 0 17:25 ? 00:00:00 nginx: master process nginx
www 73291 73290 0 17:25 ? 00:00:00 nginx: worker process
root 73293 2142 0 17:25 pts/1 00:00:00 grep --color=auto nginx
本地測試即可,也可在瀏覽器上測試,不過要關閉防火墻或者放行80端口。
# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
瀏覽器上訪問
安裝php
# tar -xvf libmcrypt-2.5.7.tar.gz
# cd libmcrypt-2.5.7/
# ./configure && make && make install
創建鏈接庫文件
# ln -s /usr/local/mysql/lib/libmysqlclient.so.20.3.1 /usr/local/mysql/lib/libmysqlclient_r.so
安裝php模塊
# tar -xvf php-5.6.27.tar.gz
# cd php-5.6.27/
# ./configure --prefix=/usr/local/php5.6 --with-config-file-path=/etc --with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/usr/local/mysql/mysql.sock
--with-gd --with-iconv --with-libxml-dir=/usr --with-mhash --with-mcrypt
--with-config-file-scan-dir=/etc/php.d --with-bz2 --with-zlib --with-freetype-dir --with-png-dir
--with-jpeg-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization
--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
# make&& make install
# cp php.ini-production /etc/php.ini
編輯配置文件/etc/php.ini ,修改後的內容如下:
date.timezone = PRC //設置時區
expose_php = Off //禁止顯示php版本的信息
short_open_tag = On //支持php短標簽
post_max_size = 16M //上傳文件大小
max_execution_time = 300 //php腳本最大執行時間
max_input_time = 300 //以秒為單位對通過POST、GET以及PUT方式接收數據時間進行限制
always_populate_raw_post_data = -1
mbstring.func_overload = 0
創建php-fpm服務啟動腳本:
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
提供php-fpm配置文件並編輯:
# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
# vim /usr/local/php5.6/etc/php-fpm.conf
pid = run/php-fpm.pid
user = www
group = www
listen = 127.0.0.1:9000
pm.max_children = 300
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 10
啟動php-fpm服務
# systemctl start php-fpm
# ps -ef |grep php-fpm
root 84346 1 0 18:16 ? 00:00:00 php-fpm: master process (/usr/local/php5.6/etc/php-fpm.conf)
www 84347 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84348 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84349 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84350 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84351 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84352 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84353 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84354 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84355 84346 0 18:16 ? 00:00:00 php-fpm: pool www
www 84356 84346 0 18:16 ? 00:00:00 php-fpm: pool www
root 84358 2048 0 18:16 pts/0 00:00:00 grep --color=auto php-fpm
配置nginx支持php
# vim /usr/local/nginx1.10/conf/nginx.conf
user www;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_index index.php;
include fastcgi.conf;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /status {
stub_status on;
}
}
檢測nginx是否有語法錯誤
# nginx -t
nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful
重啟nginx
# killall -s HUP nginx
測試LNMP
進入nginx默認的網頁根目錄,創建.php的測試頁
# vim /usr/local/nginx1.10/html/test1.php
<?php
phpinfo()
?>
# vim /usr/local/nginx1.10/html/test2.php
<?php
$link=mysql_connect(‘localhost‘,‘root‘,‘123456‘);
if($link) echo "this is test page !!! It‘s ok";
mysql_close();
?>
訪問結果
總結, 不難,要心細。那麽現在lnmp環境現在結束了!!!
lnmp平臺搭設