groupadd app
useradd -g app app
usermod -c "for web env" app
passwd app # 设置用户密码
visudo
app ALL=(ALL) ALL
ZMODEM
;sudo yum install -y lrzsz
rz # 上传
sz 文件名 # 下载
/home/app/pkg
目录下/app/env # 用于放置安装的Web环境
/app/www # 用于放置Web项目
/app/log # 用于放置Web项目的日志
(参考:nginx php-fpm安装配置)
sudo yum -y install gcc
sudo yum -y install libxml2
sudo yum -y install libxml2-devel
sudo yum -y install bzip2 bzip2-devel
sudo yum -y install curl curl-devel
sudo yum -y install libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install
使用app用户安装该依赖时,出现
mkdir: cannot create directory `/usr/local/lib/libmcrypt': Permission denied
权限不够,使用root
用户安装即可(su root)。
tar zvxf php-5.6.31.tar.gz
cd php-5.6.31
./configure --prefix=/app/env/php --with-config-file-path=/app/env/php/lib --enable-fpm --with-mcrypt --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir
make all install
在php5.6`./configure`过程中,可能出现如下错误:
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
参考:http://blog.csdn.net/u010098331/article/details/51981297
解决方式如下:
vi /etc/ld.so.conf
添加 /usr/local/lib
内容如下:
include ld.so.conf.d/*.conf
/usr/local/lib
保存成功后,再执行ldconfig命令。
配置:
cd /app/env/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf
修改(这里设置的app用户在前面创建了):
;user = nobody
;group = nobody
user = app
group = app
(参考:Nginx 之一:编译安装nginx 1.8.1及配置)
sudo yum -y install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/app/env/nginx --sbin-path=/app/env/nginx/sbin/nginx --conf-path=/app/env/nginx/conf/nginx.conf --error-log-path=/app/log/nginx/error.log --http-log-path=/app/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=app --group=app --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
make
make install
其中设置了运行nginx的用户和用户组均为app。
启动nginx(可以使用app用户启动)
sudo /app/env/nginx/sbin/nginx
判断nginx是否启动成功
ps aux|grep nginx
(参考:关于nginx配置成功后,无法访问的问题)
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
http://192.168.196.133/
vim /app/env/nginx/conf/nginx.conf
# app define
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
重启php-fpm和nginx
# 暴力关闭
killall php-fpm
/app/env/nginx/sbin/nginx -s stop
# 启动
/app/env/php/sbin/php-fpm --fpm-config /app/env/php/etc/php-fpm.conf
/app/env/nginx/sbin/nginx
sudo chown -R app:app /app/env/nginx/html/
cd /app/env/nginx/html
vim phpinfo.php
echo phpinfo();
?>
http://192.168.196.133/phpinfo.php
(参考:Nginx 启动脚本/重启脚本)
cd /etc/init.d/
vim nginx
#!/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"
nginx="/app/env/nginx/sbin/nginx"
prog=$(basename $nginx)
# NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_CONF_FILE="/app/env/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
chmod +x /etc/init.d/nginx
chkconfig nginx on
查看服务是否开启成功
chkconfig --list nginx
# nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
service nginx start
service nginx stop
service nginx restart
service nginx reload
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload
(参考:
)
从源码中复制一份php.ini,如下:
cp /home/app/pkg/php-5.4.7/php.ini-production /app/env/php/lib/php.ini
vim /app/env/php/etc/php-fpm.conf
开启 Pid 文件的配置
pid = run/php-fpm.pid
同样地,进行类似操作,其中/etc/init.d/php-fpm
如下:
#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php/etc/php.ini
# Source function library.
. /etc/rc.d/init.d/functions
#PHP_PATH=/usr/local
PHP_PATH=/app/env
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/php/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
CONFIG_INI_FILE=$PHP_PATH/php/lib/php.ini
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
rh_start() {
$DAEMON -y $CONFIGFILE -c $CONFIG_INI_FILE || echo -n " already running"
}
rh_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
rh_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
rh_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
rh_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
rh_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
rh_stop
sleep 1
rh_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
查看服务是否开启成功
chkconfig --list php-fpm
# php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload
(参考:nginx php-fpm 输出php错误日志)
修改一下日志目录的所有者:
sudo chown -R app:app /app/log
在此前nginx的安装过程中,已经设置了nginx的访问日志记录在/app/log/nginx/access.log
,而nginx的错误记录则记录在/app/log/nginx/error.log
。
这里我们进行一项测试,先打开nginx的错误日志:
tail -f /app/log/nginx/error.log
然后在/app/env/nginx/html
中:
vim test.php
echo 1/0;
访问test.php之后,我们会发现在nginx的错误日志会出现如下Warning:
2017/09/30 21:33:29 [error] 1297#0: *4 FastCGI sent in stderr: "PHP message: PHP Warning: Division by zero in /app/env/nginx/html/test.php on line 2" while reading response header from upstream, client: 192.168.196.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.196.133"
修改test.php为:
echo 1/0;
test
访问test.php之后,我们会发现在nginx的错误日志会出现如下错误:
2017/09/30 21:41:58 [error] 1297#0: *9 FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error, unexpected end of file in /app/env/nginx/html/test.php on line 4" while reading response header from upstream, client: 192.168.196.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.196.133"
从这里我们可以发现,默认情况下,php自身并没有记录错误日志,当发生报错时,会通过cgi接口把日志抛给nginx,nginx这时进行了记录。这样不太合理。我们要求nginx的日志文件记录nginx的,php的错误也能被对应的PHP日志文件记录。同时php-fpm进程的错误,应该也由对应的日志文件记录。
catch_workers_output = yes
error_log = /app/log/php/php-fpm.log
log_errors = On
; set error log path
error_log = /app/log/php/php_err.log
; set error_reporting
error_reporting = E_ALL
(参考:CentOS 6.4下编译安装MySQL 5.6.14)
rpm -qa | grep mysql
rpm -e mysql //普通删除模式
rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
* 查看系统中/etc/
是否存在my.cnf
,在/etc
目录下会存在一个my.cnf
,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak
,否则,该文件会干扰源码安装的MySQL
的正确配置,造成无法启动。*
mv /etc/my.cnf /etc/my.cnf.bak
yum -y install gcc-c++ automake cmake bison-devel ncurses-devel
tar zxvf mysql-5.6.15.tar.gz
cd mysql-5.6.15
编译安装
cmake \
-DCMAKE_INSTALL_PREFIX=/app/env/mysql \
-DMYSQL_DATADIR=/app/env/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
/app/env/mysql
权限,将所有组和所有人设置为app,如下:chown -R app:app /app/env/mysql
cd /app/env/mysql
scripts/mysql_install_db --basedir=/app/env/mysql --datadir=/app/env/mysql/data --user=app
添加服务,拷贝服务脚本到init.d目录,并设置开机启动,如下:(这里跟设置php-fpm和nginx开机自启差不多,不同的是这里MySQL通过命令生成运行的脚本)
cp support-files/mysql.server /etc/init.d/mysql
chkconfig mysql on
chkconfig --list mysql
cd /app/env/mysql
vim my.cnf
在my.cnf文件最后补充,配置如下:
user=app
默认运行MySQL服务的用户为app用户,如果不做上述修改,将/app/env/mysql/data/localhost.localdomain.err
中(文件名视具体环境的而定)出现如下的报错信息:
[ERROR] Fatal error: Can't change to run as user 'mysql' ; Please check that the user exists!
/var/lib/mysql
目录所有者 在运行MySQL前,可能需要修改/var/lib/mysql
目录的所有者OR权限,因为运行MySQL的用户权限是app用户,即MySQL可能操作不了该目录,需要对该目录的所有者OR权限进行修改。这里对所有者进行修改,如下:
chown -R app:app /var/lib/mysql
如果不做上述修改,将/app/env/mysql/data/localhost.localdomain.err
中(文件名视具体环境的而定)出现如下的报错信息:
[ERROR] Can't start server : Bind on unix socket: Permission denied
[ERROR] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?
service mysql start
# service mysql stop
# service mysql restart
这个时候我们并不能直接运行mysql命令,要进入mysql,需要执行如下命令:
/app/env/mysql/bin/mysql
为方便起见,设置环境变量即可直接调用mysql命令,如下:
(参考:Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别)
- 修改/etc/profile
文件,在文件末尾添加:(需要root权限)
# 设置之前,我们需要先设置PATH,否则不能直接调用mysql
PATH=/app/env/mysql/bin:$PATH
export PATH
/etc/profile
配置后并不能立即生效,需要用户重新登录,或者执行如下命令:source /etc/profile
mysql -uroot
mysql> SET PASSWORD = PASSWORD('123456');
(参考:Mysql初始化root密码和允许远程访问)
默认情况下,MySQL不允许用户使用root用户进行远程访问,允许进行授权设置,如下:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
'root'@'%'
表示将root用户授权给任何一台主机(也可以针对某个IP段或IP进行设置),授权登录的密码是123456
。该密码可以与本机MySQL root用户登录的密码不一样。
mysql -uroot -p
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
防火墙的3306端口默认没有开启,若要远程访问,需要开启这个端口。(道理跟““外网访问nginx服务器”一样)操作如下:
vim /etc/sysconfig/iptables
在-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT
,下添加:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
重启防火墙:
service iptables restart