为什么要写这一篇文章,不是因为自己有多厉害,只想做到两件事,能把自己的过程分享一下,其次就是记录下来,给自
己将来在尝试搭建一整套项目流程的时候,有迹可循。
因为自己很菜,这么久了还不会搭建一个正式的线上环境,所以借此契机,我想做一个全流程化的搭建过程记录,就从最
初的阿里购买服务器开始记录。首先,打开百度(要是这里都不会下面看都不要看,建议去看人类的起源,这个可能对你
来说太难了),找到阿里云的官网。
看到的是阿里云的官网,之前之后的版本我就不讨论了,技术是在更新的,我就总结当下。
选择“云服务器ECS”
到了这就是看个人能力和经济能力去选择需要的服务器了,我因为是小白就找了最菜最low的。
选择界面建议自定义,因为能找到最便宜的,毕竟学习嘛,省钱是第一要素(学生的话去认证一下支付宝的学生认证还能有学生优惠)。
这两个点需要强调一下,上面那个箭头是为了省钱,因为个人服务器,选择最便宜的够自己用就可以了,所以我选择了最低的,仔细点,可能马爹把这个选项产的很深,估计他们的产品是个人精,假装最低的是两核的,实际上有一核的。
下面的箭头我选择的是centOS6.9是因为貌似centOS7以上有不小的改动,我本身linux就很菜,不需要为难自己。而且我去了几家公司,都是用的centOS7以下的。
下面这里密码是登陆服务器的密码,设置的时候记得不要忘记了,不然自己想法子改吧。
买好了以后收到一条短信,上面会发给你的服务的IP地址,这时候就能登陆了。
怎么登陆自己百度,软件也是多种多样:
登陆上去以后会看到根目录的文件是这样的。
1.安装nginx:
关闭SELINUX
# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
命令行设置立即生效。
# setenforce 0
# groupadd -r nginx
# useradd -r -g nginx nginx
下载源码包解压编译。
# wget http://nginx.org/download/nginx-1.10.2.tar.gz
# tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
# yum groupinstall "Development tools"
# yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
# cd /usr/local/src/nginx-1.10.2
# ./configure \
当 “./configure \” 这行命令敲完之后,会出现单独一行有“>”符号的情况,把下面的复制,敲回车,即可。
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--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 \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
这里安装完成之后,继续下面的操作
# make && make install
# mkdir -pv /var/tmp/nginx/client
添加SysV启动脚本。
# vim /etc/init.d/nginx
脚本内容,复制之后 按下电脑键盘左上角的ESC 然后输入 :wq 进行保存
#!/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
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
killall -9 nginx
}
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
赋予脚本执行权限。
# chmod +x /etc/init.d/nginx
添加至服务管理列表,设置开机自启。
# chkconfig --add nginx
# chkconfig nginx on
启动服务。
# service nginx start
浏览器访问可看到默认欢迎页面。(又要问了,地址是什么?对吧?就是你的ip地址,如果没看到这里的话,去后台重置,
再来一遍吧)
2.安装MySql
准备编译环境。
# yum groupinstall "Server Platform Development" "Development tools" -y
# yum install cmake -y
准备mysql数据存放目录。
# mkdir /mnt/data
# groupadd -r mysql
# useradd -r -g mysql -s /sbin/nologin mysql
# id mysql
uid=497(mysql) gid=498(mysql) groups=498(mysql)
更改数据目录属主属组。
# chown -R mysql:mysql /mnt/data
这里官网没有提供下载mysql的方法,但是作为程序猿,我们可以找到一些“开源(bu yao qian)”
# wget http://101.110.118.70/dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.34.tar.gz
# tar xvf mysql-5.6.34.tar.gz -C /usr/local/src
# cd /usr/local/src/mysql-5.6.24
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
当 “cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \” 这行命令敲完之后,会出现单独一行有“>”符号的情况,把下面的复制,敲回车,即可。
-DMYSQL_DATADIR=/mnt/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
然后继续:
# make && make install
修改安装目录的属组为mysql。
# chown -R mysql:mysql /usr/local/mysql/
初始化数据库。
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/
ps:这里可能会出现问题 我这里有个解决方案,这里出现问题可能是路径的问题 加上一个 --basedir,如果没问题跳过
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/ --basedir=/usr/local/mysql/
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# cp support-files/my-default.cnf /etc/my.cnf
设置开机自动启动。
# chkconfig mysqld on
# chkconfig --add mysqld
修改配置文件中的安装路径及数据目录存放路径。
# echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
设置PATH环境变量。
# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh
启动服务。
# service mysqld start
# mysql -h 127.0.0.1
3.安装php-frm
安装依赖包。
# yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
又到了“开源(bu yao qian)”环节了,我也给大家找到了一个,是php7的源码包
# wget http://php.uberglobalmirror.com/distributions/php-7.0.18.tar.gz
# tar xvf php-7.0.18.tar.gz -C /usr/local/src
# cd /usr/local/src/php-7.0.18
# ./configure --prefix=/usr/local/php \
当 “./configure --prefix=/usr/local/php \” 这行命令敲完之后,会出现单独一行有“>”符号的情况,把下面的复制,
敲回车,即可。
--with-config-file-scan-dir=/etc/php.d \
--with-config-file-path=/etc \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--with-openssl \
-enable-xml \
--enable-sockets \
--enable-fpm \
--with-mcrypt \
--with-bz2
继续往下:
# make && make install
添加php和php-fpm配置文件。
# cp /usr/local/src/php-7.0.18/php.ini-production /etc/php.ini
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
ps:这里可能会报错,报错内容为如下:
WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/*.conf' from /usr/local/php/etc/php-fpm.conf at line 125.
这个warning就是说php-fpm.conf第125行的找不到对应的.conf文件,所以我们到这个文件夹下把www.conf.default复制为www.conf就可以了
添加php-fpm启动脚本。
# cp /usr/local/src/php-7.0.18/sapi/fpm/init.d.php-fpm /etc/init.d/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
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
# cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
编辑/etc/nginx/nginx.conf,在所支持的主页面格式中添加php格式的主页,类似如下:
编辑命令是不是又不知道了? vi /etc/nginx/nginx.conf
location/{
root /usr/local/nginx/html;
index index.php index.html index.htm;
}
取消以下内容前面的注释:
location~ \.php${
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
重新载入nginx的配置文件。
# service nginx reload
# cat index.php
<?php
phpinfo();
?>
ps:这篇文章有很多一部分是阿里的,我只是沿用并且总结了自己用的时候遇到的问题,再给出了解决的方法,而且最大一
部分是为了以后在搭建环境的时候复用。没帮助的话就别看了,本身就没啥卵用。^_^