CentOS7 通过yum源安装LNMP

最近刚换了服务器,听说nginx挺牛逼的,所以就配置了个LNMP环境

什么是lnmp

LNMP便是指Liunx+Nginx+MySQL+PHP-FPM架构,高效稳定的Nginx配合PHP-FPM这一强大的FastCGI进程管理,适合中小型PHP应用

安装笔记

操作系统:centos 7.0 x64位

一、基本配置

1.配置网卡
vi /etc/sysconfig/network-scripts/ifcfg-eth0

修改ONBOOT=no为ONBOOT=yes
修改BOOTPROTO=dhcp为BOOTPROTO=static
加入ip信息(已经存在的话就跳过)

1
2
3
IPADDR=10.x.x.x
NETMASK=255.255.255.0
GATEWAY=10.x.x.x

 

2.修改主机名

vi /etc/sysconfig/network

1
2

NETWORKING=yes

 

 

3.修改DNS解析
vi /etc/resolv.conf

1
2
3
4
5

# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver xx.xx.xx.xx

 

 

4.将主机名与ip的对应加入/etc/hosts

1
2
3

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

 

 

5.升级yum update

二、导入epel源

CentOS6和7用:rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
CentOS 5请使用:rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

执行yum makecache
如果报metalink下载失败,将/etc/yum.repos.d/epel.repo和/etc/yum.repos.d/epel-testing.repo中mirrorlist全部由https://改为http://

三、安装nginx

nginx可以用epel源中的包安装,但更新并不及时,建议使用nginx官方源。Debian党请参考nginx在Linux上的安装方法

导入nginx stable源:

Centos7请使用:rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Centos6请使用:rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
CentOS 5请使用:http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm

yum install nginx

四、安装php与mysql

php官方不打包rpm、deb二进制安装包,在其官网上推荐了两个第三方源,个人比较喜欢remi源。Debian党请自行设法解决。

1.导入ius源(注意ius源依赖epel源)

CentOS7请使用:rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-13.ius.centos7.noarch.rpm

Centos6请使用:rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/i386/ius-release-1.0-11.ius.centos6.noarch.rpm
CentOS 5请使用:http://dl.iuscommunity.org/pub/ius/stable/CentOS/5/i386/ius-release-1.0-11.ius.centos5.noarch.rpm

用ius源安装php与mysql(这里的mysql并非采用ius源,而是updates源中的5.1版本)
yum install php54-fpm php54-mysql php54-mcrypt php54-mbstring
yum install mysql-server

2.导入remi源(注意remi源依赖epel源)

CentOS7请使用:rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

CentOS6请使用:rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
CentOS 5请使用:http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

用remi源安装php与mysql(remi中的php-mysql依赖它打包的mysql mysql-lib mysql-server,索性一起装了吧,mysql的版本为5.5)
yum –enablerepo=remi install php-fpm php-mysql php-mcrypt php-mbstring mysql-server php-gd php-pecl-apc

常用php组件,酌情安装
php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-pecl-memcache
php-eaccelerator php-pecl-apc

我采用了remi源安装了php 5.4与mysql 5.5

五、配置php

添加www用户用于执行nginx以及php-fpm子进程
useradd -d /home/wwwroot -m -s /sbin/nologin www

1.php配置vi /etc/php.ini酌情修改

2.php-fpm配置
fpm采用unix-sock方式跑,适合小流量,高效;高负载建议用TCP端口,或者直接用LNAMP架构,或者上HHVM
cd /etc/php-fpm.d
mv www.conf www.conf.default
vi www.conf

www.conf参考内容(注意酌情修改fpm子进程数量,一个fpm进程大概20M):

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
[www]

listen = 127.0.0.1:9000
;listen = /tmp/php-cgi.sock
; Default Value: -1
;listen.backlog = -1

listen.allowed_clients = 127.0.0.1

listen.owner = www
listen.group = www
;listen.mode = 0660

user = www
group = www

pm = dynamic

pm.max_children = 50

pm.start_servers = 6

pm.min_spare_servers = 4

pm.max_spare_servers = 20

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
; accepted conn - the number of request accepted by the pool;
; pool - the name of the pool;
; process manager - static or dynamic;
; idle processes - the number of idle processes;
; active processes - the number of active processes;
; total processes - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
; accepted conn: 12073
; pool: www
; process manager: static
; idle processes: 35
; active processes: 65
; total processes: 100
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
; http://www.foo.bar/status
; http://www.foo.bar/status?json
; http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
; anything, but it may not be a good idea to use the .php extension or it
; may conflict with a real PHP file.
; Default Value: not set
;pm.status_path = /status

; The ping URI to call the monitoring page of FPM. If this value is not set, no
; URI will be recognized as a ping page. This could be used to test from outside
; that FPM is alive and responding, or to
; - create a graph of FPM availability (rrd or such);
; - remove a server from a group if it is not responding (load balancing);
; - trigger alerts for the operating team (24/7).
; Note: The value must start with a leading slash (/). The value can be
; anything, but it may not be a good idea to use the .php extension or it
; may conflict with a real PHP file.
; Default Value: not set
;ping.path = /ping

; This directive may be used to customize the response of a ping request. The
; response is formatted as text/plain with a 200 response code.
; Default Value: pong
;ping.response = pong

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /var/log/php-fpm/www-slow.log

; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024

; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: chrooting is a great security feature and should be used whenever
; possible. However, all PHP paths will be relative to the chroot
; (error_log, sessions.save_path, ...).
; Default Value: not set
;chroot =

; Chdir to this directory at the start. This value must be an absolute path.
; Default Value: current directory or / when chroot
;chdir = /var/www

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
;catch_workers_output = yes

; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; exectute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
;security.limit_extensions = .php .php3 .php4 .php5

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
; php_value/php_flag - you can set classic ini defines which can
; be overwritten from PHP call 'ini_set'.
; php_admin_value/php_admin_flag - these directives won't be overwritten by
; PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Default Value: nothing is defined by default except the values in php.ini and
; specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 128M

; Set session path to a directory owned by process user
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

 

 

centos7用户执行:service php-fpm start
centos5和6用户执行:/etc/init.d/php-fpm start

六、配置nginx

cd /etc/nginx
mv nginx.conf nginx.conf.default
cd conf.d
mv default.conf default.conf.default

cd ../

vi nginx.conf

nginx.conf参考内容:

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#user nobody;
user www www;
worker_processes 1;
#include=/etc/nginx/conf.d/*.conf
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
include /etc/nginx/conf.d/*.conf;

default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
#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 650;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /home/wwwroot/html;
index index.php index.html index.htm;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

#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;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

 

 

vi conf.d/default.conf
default.conf参考内容:

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

server {
listen 80;
server_name test.com www.test.com;

root /home/wwwroot/test/public_html;
index index.html index.htm index.php default.html default.htm default.php;
error_log /home/wwwroot/wwwlogs/test.log;
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

location ~ .*\.(php|php5)?$
{
#try_files $uri =404;
#fastcgi_pass /tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
#include fastcgi_params;
}

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

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

#access_log /home/wwwroot/wwwlogs/phpmyadmin.log main;
}

 

 

vi fastcgi_params
加入此行

1
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 

cd /home/wwwroot
mkdir wwwlogs default website
chown -R www.www *
centos7用户执行:service nginx start
centos5和6用户执行:/etc/init.d/nginx start

七、配置mysql

默认是装的mariadb.

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB。 MariaDB由MySQL的创始人Michael Widenius(英语:Michael Widenius)主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL AB卖给了SUN,此后,随着SUN被甲骨文收购,MySQL的所有权也落入Oracle的手中。MariaDB名称来自Michael Widenius的女儿Maria的名字。

mysql的配置直接用了my-small.cnf(占有内存最少的),因我买的服务器只有512M内存,吊丝配置,内存很珍贵,不然容易挂。依内存大小与PHP应用的数据库级别选择,各种配置见/usr/share/doc/mariadb-galera-server-5.5.37/
如果没有/usr/share/doc/mariadb-galera-server-5.5.37/ 目录的话就找/usr/share/doc/mysql-server-5.5.38/

mv /etc/my.cnf /etc/my.cnf.default
cp /usr/share/doc/mariadb-galera-server-5.5.37/my-small.cnf /etc/my.cnf
centos7用户执行:service mysqld start
centos5和6用户执行:/etc/init.d/mysqld start

如果启动失败,查看/var/lib/mysql下的那个.err
可能是InnoDB: Warning: io_setup() failed with EAGAIN. Will make 5 attempts before giving up.
在/etc/my.cnf加入innodb_use_native_aio = 0禁用Linux Native AIO
或者在/etc/my.cnf中加入

1
2
default-storage-engine = MyISAM
skip-innodb

以禁用innodb引擎

修改root密码/usr/bin/mysqladmin -u root password ‘rootpasswordhere’

八、安装phpMyAdmin,便于数据库管理

phpmyadmin新版本可能不支持,安装旧版
cd /home/wwwroot/website
wget http://soft.vpser.net/datebase/phpmyadmin/phpMyAdmin-3.4.8-all-languages.tar.gz
tar zxvf phpMyAdmin-3.4.8-all-languages.tar.gz

至此,LNMP架构配置完毕,最后设置开机自启动,
centos7用户执行

1
2
3
systemctl enable nginx.service
systemctl enable php-fpm.service
systemctl enable mysqld.service

 
centos5和6用户执行

1
2
3
chkconfig nginx on
chkconfig php-fpm on
chkconfig mysqld on

 

坚持原创技术分享,您的支持将鼓励我继续创作!