本次實驗用Nginx+Mysql+PHP的源碼包來編譯安裝一個LNMP系統(tǒng),實驗之前先做一些準備工作.
前期準備工作:
- 下載對應(yīng)的源碼包"
可以將源碼包統(tǒng)一放在一個文件路徑下方便管理,這個選/usr/local/src,直接用wget命令下載源碼包即可,
Mysql源碼包地址:
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
PHP源碼包的下載地址:
http://cn2.php.net/distributions/php-5.6.30.tar.gz
Nginx源碼包的下載地址:
http://nginx.org/download/nginx-1.12.1.tar.gz
2.安裝環(huán)境及依賴關(guān)系:
這里先提供一些環(huán)境按鍵是需要的一下服務(wù),比如:gcc,prec-devel等等,在編譯./configure的時候配置不一樣所需要依賴的包有可能也不一樣,這里不一一列舉了,提供一個解決依賴問題的思路.
在./configure配置好選項后檢查時如果缺少了哪一個依賴包會有一個configure:error:please reinstall XXXX distribution 的語句,看到這個你就可以yum -yinstall XXXX或XXXX.devel,然后再重新./configure編譯一次,遇到依賴問題在安裝在編譯直到成功就ok了
這個就是編譯安裝時遇上依賴問題的一種排錯方法.
這里簡單列舉一些依賴文件,
Mysql:
perl-Module-Install.noarch
libaio*
PHP:
libcurl-devel
Nginx:
pcre-devel
下載好源碼包和依賴關(guān)系之后就可以開始配置了.
1. Mysql安裝配置
解壓縮源碼包:
tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
移動目錄并進入:
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql
創(chuàng)建數(shù)據(jù)目錄及用戶名:
mkdir /data/
useradd mysql
執(zhí)行初始化腳本:
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
copy配置文件并配置:
cp support-files/my-default.cnf /etc/my.cnf
--------------分割線--------------
在vim /etc/my.cnf配置如下:
[root@localhost mysql]# vim /etc/my.cnf
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir =/data/mysql #添加這個數(shù)據(jù)存放路徑
# port = .....
# server_id = .....
# socket = .....
socket = /tmp/mysql.sock #還有這個,其他暫時不用動
...省略...
完成以上操作就可以啟動Mysql服務(wù)了:
/etc/init.d/mysqld start
檢查服務(wù)是否啟動:
[root@localhost mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46168/nginx: master
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1548/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1165/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1163/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1396/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 5103/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 5103/sshd: root@pts
tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 5103/sshd: root@pts
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1165/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1163/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1396/master
tcp6 0 0 ::1:6010 :::* LISTEN 5103/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 5103/sshd: root@pts
tcp6 0 0 ::1:6012 :::* LISTEN 5103/sshd: root@pts
tcp6 0 0 :::3306 :::* LISTEN 5939/mysqld
#看到服務(wù)已經(jīng)啟動了
加入開機啟動:
chkconfig --add mysqld
chkconfig mysqld on
2. PHP安裝及配置
編譯之前在此提醒一下,準備工作中的依賴問題提醒,這里并沒有全部列出所有的依賴包,授人魚不如授人與漁,試驗環(huán)境及要安裝的選項不同請按需自行安裝缺少的依賴包.
解壓并進入目錄:
tar -zxvf php-5.6.30.tar.gz
cd php-5.6.30
創(chuàng)建 php-fpm用戶:
useradd -s /sbin/nologin php-fpm
./configure:這里的選項很多,報錯缺那個包就裝那個包就ok了.
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl
執(zhí)行編譯安裝:
make && make install
copy配置文件
cp php.ini-production /usr/local/php-fpm/etc/php.ini
創(chuàng)建vim /usr/local/php-fpm/etc/php-fpm.conf:
[root@localhost php-5.6.30]# vim /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock #監(jiān)聽ip和端口雕薪,端口默認為9000
listen.mode = 666 #用來定義php-fcgi.sock文件的權(quán)限
user = php-fpm
group = php-fpm
pm = dynamic #后面這些都是關(guān)于進程的信息
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
~
:wq
拷貝啟動腳本并修改權(quán)限:
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
設(shè)置開機啟動:
chkconfig --add php-fpm
chkconfig php-fpm on
檢查配置服務(wù):
/usr/local/php-fpm/sbin/php-fpm -t
啟動服務(wù):
service php-fpm start
檢查是否啟動:
[root@localhost conf]# ps aux | grep php
root 42351 0.0 0.2 123448 4736 ? Ss 15:39 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 42352 0.0 0.2 125532 4900 ? S 15:39 0:00 php-fpm: pool www
php-fpm 42353 0.0 0.3 125532 6476 ? S 15:39 0:00 php-fpm: pool www
php-fpm 42354 0.0 0.2 125532 4904 ? S 15:39 0:00 php-fpm: pool www
php-fpm 42355 0.0 0.2 125532 4904 ? S 15:39 0:00 php-fpm: pool www
php-fpm 42356 0.0 0.2 125532 4908 ? S 15:39 0:00 php-fpm: pool www
php-fpm 42357 0.0 0.2 125532 4908 ? S 15:39 0:00 php-fpm: pool www
...省略...
服務(wù)啟動成功了.
3. Nginx安裝配置
解壓縮并進入目錄:
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1/
執(zhí)行configure 檢查編譯環(huán)境:參數(shù)方面根據(jù)自己的需求去配置,依賴問題還是缺哪個包就裝哪個包
./configure --prefix=/usr/local/nginx
執(zhí)行編譯:
make && make install
創(chuàng)建啟動腳本: 在vim /etc/init.d/nginx中創(chuàng)建
#!/bin/bash
# chkconfig: - 30 21 #這個和下面的選項好正確,否則會報service nginx does not support chkconfig的錯誤,
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
修改權(quán)限:
chmod 755 /etc/init.d/nginx
加入服務(wù)列表并開機啟動:
chkconfig --add nginx
chkconfig nginx on
修改nginx.conf文件:文件路徑在/usr/local/nginx/conf下,備份原來conf文件并創(chuàng)建新的配置文件,格式如下;
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
檢查錯誤:
[root@localhost mysql]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
啟動Nginx:
service nginx start
檢查服務(wù)是否啟動:
[root@localhost mysql]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46168/nginx: master
到這里L(fēng)NMP服務(wù)已經(jīng)搭建好了,現(xiàn)在來檢查一下nginx是否能解析php,只要在在nginx的/usr/local/nginx/html目錄下創(chuàng)建一個php文件,然后通過瀏覽器訪問就能看到效果了.
[root@localhost php-5.6.30]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
~
:wq
如果php生效了會出現(xiàn)如下圖片: