查看Linux版本
[root@iZwz96pb4qob9lpzadqipkZ etc]# lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.8 (Final)
Release: 6.8
Codename: Final
[root@iZwz96pb4qob9lpzadqipkZ etc]# cat /etc/redhat-release
CentOS release 6.8 (Final)
一、安装Nginx
准备工作,安装Nginx前先安装如下依赖:
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc.
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
1.gcc 安装:yum install gcc-c++
2.PCRE pcre-devel 安装:yum install -y pcre pcre-devel
3.zlib 安装: yum install -y zlib zlib-devel
4.OpenSSL 安装:yum install -y openssl openssl-devel
1.编译安装Nginx
[root@iZwz96pb4qob9lpzadqipkZ local]# cd /usr/local
[root@iZwz96pb4qob9lpzadqipkZ local]# wget https://nginx.org/download/nginx-1.13.4.tar.gz
1.下载
wget https://nginx.org/download/nginx-1.13.4.tar.gz
2.解压
tar -zxvf nginx-1.13.4.tar.gz
3.cd nginx-1.13.4
编译
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
可以看到
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/var/run/nginx/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/temp/nginx/client"
nginx http proxy temporary files: "/var/temp/nginx/proxy"
nginx http fastcgi temporary files: "/var/temp/nginx/fastcgi"
nginx http uwsgi temporary files: "/var/temp/nginx/uwsgi"
nginx http scgi temporary files: "/var/temp/nginx/scgi"
编译安装
4.make
5.make install
2.安装成功查看安装目录
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/var/log/nginx' \
|| mkdir -p '/var/log/nginx'
make[1]: Leaving directory `/usr/local/nginx-1.13.4'
[root@iZwz96pb4qob9lpzadqipkZ nginx-1.13.4]# cd ..
[root@iZwz96pb4qob9lpzadqipkZ local]# ls
aegis games lib64 nginx-1.13.4 share
bin include libexec nginx-1.13.4.tar.gz src
etc lib nginx sbin
[root@iZwz96pb4qob9lpzadqipkZ local]# cd nginx
[root@iZwz96pb4qob9lpzadqipkZ nginx]# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 25 00:08 conf
drwxr-xr-x 2 root root 4096 Aug 25 00:08 html
drwxr-xr-x 2 root root 4096 Aug 25 00:08 sbin
[root@iZwz96pb4qob9lpzadqipkZ nginx]#
3.运行Nginx,进入到sbin目录下执行./nginx,出现这个错误只需root下手动创建目录即可。
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
[root@iZwz96pb4qob9lpzadqipkZ sbin]# pwd
/usr/local/nginx/sbin
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
[root@iZwz96pb4qob9lpzadqipkZ sbin]# mkdir -p /var/temp/nginx/client
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
查看配置信息是否正确
[root@iZwz96pb4qob9lpzadqipkZ sbin]# /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
4.查看Nginx进程ps aux | grep nginx
[root@iZwz96pb4qob9lpzadqipkZ sbin]# mkdir -p /var/temp/nginx/client[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ps aux | grep nginx
root 4259 0.0 0.0 22060 660 ? Ss 00:22 0:00 nginx: master process ./nginx
nobody 4260 0.0 0.1 22504 1256 ? S 00:22 0:00 nginx: worker process
root 4262 0.0 0.0 105368 884 pts/0 S+ 00:25 0:00 grep nginx
5.输入IP测试nginx是否正常运行
6.创建Nginx启动脚本
首先添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx nginx
查看Nginx用户及用户组
cat /etc/passwd
cat /etc/group
参考官方脚本
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
先关闭Nginx
pkill -9 nginx
添加脚本,在init.d添加Nginx启动脚本
vi /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
改变nginx脚本权限
chmod 755 /etc/init.d/nginx
添加进service管理服务并设置开机启动
chkconfig –add nginx
chkconfig nginx on
服务启动测试
service nginx start
加入到环境变量
vim /etc/profile
尾行添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
保存关闭后运行 source /etc/profile 即会加入环境变量
二、安装mysql
一般分为以下几种安装方式:
- yum
- rpm包
- 常规编译安装
- cmake安装
- 二进制包安装
如何使用:
- 个人使用yum或rpm包安装
- 企业多使用常规编译和查马克及二进制包安装方式
centos的yum源安装的mysql版本太老,这里用编译安装mysql
1.安装编译代码需要的包
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
2.下载mysql
cd /usr/local/
1 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz
2 tar -zxvf mysql-5.7.19.tar.gz
3 cd mysql-5.7.19
3.编译
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/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 \
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
注意最后一行配置,是下载boost包的,如果没有boost包,编译会报错:
4.安装
make && make install
会出现以下错误,阿里云centos主机512M内存的, 会在make编译到45%时会报错, 这是内存不足所致。
c++: Internal error: Killed (program cc1plus)
Please submit a full bug report.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make[2]: *** [sql/CMakeFiles/sql.dir/item_geofunc.cc.o] Error 1
make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2
make: *** [all] Error 2
解决方法:
出错后重新运行配置,需要删除CMakeCache.txt文件
# make clean
# rm -f CMakeCache.txt
设置2G交换分区
dd if=/dev/zero of=/swapfile bs=1k count=2048000 --获取要增加的2G的SWAP文件块
# mkswap /swapfile -- 创建SWAP文件
# swapon /swapfile -- 激活SWAP文件
# swapon -s -- 查看SWAP信息是否正确
# echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab -- -- 添加到fstab文件中让系统引导时自动启动
注意, swapfile文件的路径在/var/下
重新使用cmake -DCMAKE_INSTALL_PREFIX…
[100%]Built target my_safe_process #此处make完成
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server
[root@iZwz96pb4qob9lpzadqipkZ mysql-5.7.19]#
# 此处make install完成
编译完后, 如果不想要交换分区了, 可以删除:
# swapoff /swapfile
# rm -fr /swapfile
创建mysql组和mysql用户
groupadd mysql
useradd -r -g mysql mysql
useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
useradd -g参数表示把mysql用户添加到mysql用户组中。
修改/usr/local/mysql权限
创建data目录
mkdir /usr/local/mysql/data
修改当前目录的所有者为mysql用户
chown -R mysql:mysql /usr/local/mysql
初始化配置:
===========================================
mysql5.7和之前版本不同,很多资料上都是这个命令:../scripts/mysql_install_db –user=mysql,而mysql5.7的mysql_install_db命令是在bin目录下 的并且建议 用 mysqld –initialize命令
mysql5.7之前版本初始化配置表命令:
script/mysql_install_db --user=mysql --basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data
–user 启动mysql的用户
–basedir mysql安装目录
–datadir mysql数据仓库目录
==========================================
cd /usr/local/mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@iZwz96pb4qob9lpzadqipkZ mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
2017-08-26T10:00:22.235755Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-26T10:00:24.305929Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-26T10:00:24.520554Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-26T10:00:24.630442Z 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: 61349e3b-8a45-11e7-929c-00163e04e9fa.
2017-08-26T10:00:24.633411Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-26T10:00:24.633981Z 1 [Note] A temporary password is generated for root@localhost: j,Dsw./Rs2k)
重要:
注意最后一行,这也是和之有版本不同的地方,它给了root一个初始密码,后面要登录的时候要用到这个密码。j,Dsw./Rs2k)
将mysql/目录下除了data/目录的所有文件,改回root用户所有,mysql用户只需作为mysql/data/目录下所有文件的所有者。
cd /usr/local/mysql
chown -R root:root ./
chown -R mysql:mysql ./data
修改/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
修改为datadir=/usr/local/mysql/data
创建目录
mkdir -p /var/lib/mysql/
chown -R mysql:mysql /var/lib/mysql
设置开机启动
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
把mysql注册为开机启动的服务
chkconfig --add mysql
查看是否添加成功
[root@iZwz96pb4qob9lpzadqipkZ init.d]# chkconfig --list mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改启动脚本vim /etc/init.d/mysql ,将basedir和datadir补全
basedir='/usr/local/mysql'
datadir='/usr/local/mysql/data'
启动mysql
service mysql start
修改MySQL密码
vi /etc/my.cnf 在[mysqld]下添加一行 skip-grant-tables
service mysql restart
mysql #现在可以免密码登陆
update mysql.user set authentication_string=password('test') where user='root' and Host = 'localhost';#特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段
flush privileges;
quit;
三、安装php7.1.8
此次采用编译安装php7.1.8
1.下载
1.cd /usr/local
2.wget http://hk1.php.net/get/php-7.1.8.tar.gz/from/this/mirror
3. tar -zxvf mirror
2.编译
编译之前需要安装一下依赖,否则会报错
# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel MySQL pcre-devel
# yum -y install curl-devel
# yum -y install libxslt-devel
编译
./configure --prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--with-jpeg-dir \
--disable-fileinfo
–with-jpeg-dir \ 安装这个防止Call to undefined function imagecreatefromjpeg()
–disable-fileinfo 防止小内存vps编译安装php时遇到virtual memory exhausted: Cannot allocate memory,很有可能会出现问题,因为编译过程是一个内存消耗较大的动作。
成功后会输出:
Thank you for using PHP.
3.make
make完成后会出现以下内容
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
directorygraphiterator.inc
pharcommand.inc
clicommand.inc
invertedregexiterator.inc
phar.inc
Build complete.
Don't forget to run 'make test'.
4.make install
完成后如下图所示
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/php-7.1.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
5.配置文件
# cd /usr/local/php-7.1.8
# cp php.ini-development /usr/local/php/lib/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cd /usr/local/php-7.1.8
# cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
添加php-fpm启动脚本
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --list php-fpm
chkconfig php-fpm on
启动服务
service php-fpm start
或/etc/init.d/php-fpm start
将php加入系统变量
sudo vi /etc/profile
export PATH=$PATH:/usr/local/php/bin #在文件后面添加这行
source /etc/profile #让修改生效
配置Nginx支持解析php,修改/usr/local/nginx/conf/nginx.conf,添加location ~ .php这段。
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}