Linux + Nginx + Mysql + PHP 编译安装

目录
  1. 首先准备安装包
  2. yum安装必要包
  3. 安装各个库
  4. 一、安装MySQL
  5. 二、安装PHP
  6. 三、安装NGINX
  7. nginx启动脚本
  8. nginx设置自启动
  9. 环境说明

首先准备安装包

1
2
3
4
5
6
7
8
9
10
11
nginx-1.0.15.tar.gz
php-5.3.16.tar.gz
libaio-0.3.93-4.i386.rpm
MySQL-server-5.5.14-1.rhel5.x64.rpm
MySQL-client-5.5.14-1.rhel5.x64.rpm
MySQL-shared-5.5.14-1.rhel5.x64.rpm
MySQL-devel-5.5.14-1.rhel5.x64.rpm
libmcrypt-2.5.7.tar.gz
mhash-0.9.9.9.tar.gz
mcrypt-2.6.8.tar.gz
pcre-8.33.tar.gz

yum安装必要包

1
yum -y install gcc-c++ openssl-devel libjpeg-devel libpng-devel libXpm-devel ImageMagick ImageMagick-devel freetype freetype-devel curl-devel

安装各个库

依次是libmcrypt,mhash, mcrypt 和pcre

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../

tar zxvf pcre-8.33.tar.gz
cd pcre-8.30
./configure
make
make install
cd ../

一、安装MySQL

1
2
3
4
5
6
7
8
9
rpm -ivh libaio-0.3.93-4.x64.rpm
rpm -ivh MySQL-server-5.5.14-1.rhel5.x64.rpm
rpm -ivh MySQL-client-5.5.14-1.rhel5.x64.rpm
rpm -ivh MySQL-shared-5.5.14-1.rhel5.x64.rpm
rpm -ivh MySQL-devel-5.5.14-1.rhel5.x64.rpm

service mysql start
//修改密码
mysql_secure_installation

二、安装PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
tar zxvf php-5.3.16.tar.gz
cd php-5.3.16
mkdir -p /usr/local/php
// 很多的模块
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/bin/ --with-libdir=lib64 --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --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-zip --enable-soap --with-pdo-mysql=/usr/bin/mysql --with-mysql-sock=/var/lib/mysql/mysql.sock
// 简单的模块
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/bin/ --with-libdir=lib64 --enable-xml --enable-zip --enable-fpm --enable-mbstring --with-gd --with-zlib --with-curl --with-pdo-mysql=/usr/bin/mysql --with-mysql-sock=/var/lib/mysql/mysql.sock

// 如果有这个错误
configure: error: Cannot find libmysqlclient under /usr.
Note that the MySQL client library is not bundled anymore!
// 就加上
--with-libdir=lib64

make
make install

cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #因为php5.3开始自带fpm,使用自带的管理脚本
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on #设置开机自启动
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

// 然后修改php-fpm.conf,将pid改成以下,并将user和group改成www
pid = run/php-fpm.pid
user = www
group = www

三、安装NGINX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
groupadd www
useradd -s /sbin/nologin -g www www #www用户不能login
mkdir -p /home/www #此路径是用于存放各域名的root路径
chmod +w /home/www
mkdir -p /home/wwwlogs
chmod 755 /home/wwwlogs
chown -R www:www /home/www

tar zxvf nginx-1.0.15.tar.gz
cd nginx-1.0.15/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install
mkdir /usr/local/nginx/conf/vhosts/ #用于配置各域名

// 修改Nginx的配置文件
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx_bak.conf
vi /usr/local/nginx/conf/nginx.conf


user www;
worker_processes 8;

#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;
index index.php index.html index.htm;
root D:/xampp/htdocs/enterprise;

# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 24h;
}

if ($request_filename !~* (\.xml|\.rar|\.html|\.htm|\.php|\.swf|\.css|\.js|\.gif|\.png|\.jpg|\.jpeg|robots\.txt|index\.php|\.jnlp|\.jar|\.eot|\.woff|\.ttf|\.svg)) {
rewrite ^/(.*)$ /index.php/$1 last;
}

location ~ .*\.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

}

nginx启动脚本

因为默认是没有nginx管理脚本的,在此先新建一个,运行vi /etc/init.d/nginx,输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

nginx设置自启动

1
2
chmod +x /etc/init.d/nginx
chkconfig nginx on

环境说明

直接输入以下命令,会得到相应的结果

1
2
3
4
5
service nginx restart 重启nginx
service mysql restart 重启mysql
service php-fpm restart 重启php-fpm
service php-fpm stop 停止php-fpm
service php-fpm start 启动php-fpm

各站点的根目录是/home/www/

1
2
3
php: /usr/local/php/etc/
nginx: /usr/local/nginx/conf/
find / -name "my.ini"

启动nginx的时候遇到 error while loading shared libraries: libpcre.so.1:

答:增加以下软连接,注意,如果是64位系统,对应目录是/lib64

1
2
3
# cd /lib64
cd /lib
ln -s /usr/local/lib/libpcre.so.1 /lib64

Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令:

1
lsof -i tcp:80

列出所有端口

1
netstat -ntlp

查看进程

1
ps -ef | grep nginx