2016年12月19日

CentOS 7.2 Yum安装部署Nginx+PHP+Mysql/MariaDB+Nodejs+Django生产环境

作者 非鱼

内容取自网络,根据自己的实践总结整理。

1、更新系统应用。

yum update

2、安装开发工具包。

yum groupinstall "Development Tools"

3、安装MariaDB,启动,并设为开机自启动服务。

yum -y install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb

4、初始化数据库。

mysql_secure_installation

根据命令提示完成Mysql数据库和root用户的初始化操作。

5、配置Mysql默认字符集为UTF8。

vi /etc/my.cnf

在[mysqld]标签下添加

init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

文件/etc/my.cnf.d/client.cnf

vi /etc/my.cnf.d/client.cnf

在[client]中添加

default-character-set=utf8

文件/etc/my.cnf.d/mysql-clients.cnf

vi /etc/my.cnf.d/mysql-clients.cnf

在[mysql]中添加

default-character-set=utf8

全部配置完成,重启mariadb

systemctl restart mariadb

6、安装PHP7。

yum install epel-release
yum install wget unzip unrar
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7*.rpm
yum --enablerepo=remi,remi-test install php70-php-fpm
yum --enablerepo=remi,remi-test install php70-php-mysql php70-php-iconv php70-php-mbstring

7、安装Nginx。

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

8、修改nginx默认站点配置文件

vi  /etc/nginx/conf.d/default.conf
location / {
root   /usr/share/nginx/html;
index  index.php index.html index.htm;
}

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

9、修改PHP默认配置,解决Web站点权限问题。

vi /etc/opt/remi/php70/php-fpm.d/www.conf

修改user这一行

user=nginx

然后将PHP程序所在的目录chown -R nginx .

10、启动服务

systemctl start nginx
systemctl start php70-php-fpm
systemctl enable nginx
systemctl enable php70-php-fpm

在/usr/share/nginx/html目录中增加一个phpinfo的文件访问看看。

11、安装Nodejs。

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs

12、增加一个nginx站点配置文件。

server {
    server_name a.com;
    listen 80;

    root /wwwroot/a.com/static/;
    #使用以下方式让nginx代理静态文件,动态路由转发给nodejs
    if ( !-f $request_filename ){
       rewrite (.*) /server.js;
    }
    location = /server.js {
        # proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:9000$request_uri;
        proxy_redirect off;
    }
}

现在让你的nodejs app站点启动并监听9000端口,就可以通过nginx的80端口来访问了。

13、安装pm2。

npm install pm2 -g

然后使用pm2 start启动你的nodejs应用。

14、安装Django。
CentOS7已经自带了2.7版本的Python,可以直接使用。

yum install python-devel mysql-devel
pip install Django==1.10.4
pip install mysql-python
pip install uwsgi

然后配置uwsgi所需要的ini文件,就可以把django网站跑起来了。

15、https ssl证书。
现在阿里云和腾讯云都可以申请免费的ssl单域名证书,申请成功后会得到一个key文件和一个pem文件。
将两个文件复制到服务器上,不需要放在网站目录下。
然后在nginx站点conf复制一份,将顶部的监听端口从80改成443,并在文件结尾处添加:

ssl on;
ssl_certificate  pem文件绝对路径;
ssl_certificate_key  key文件绝对路径;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;

然后将原来的配置文件中除了域名和端口之外的配置全部删除,加上一行

rewrite ^(.*)$  https://$host$1 permanent;

这样所有的http访问就会自动跳转到https地址了。