Can
Be Better

Centos6 安装Nginx+php+mysql

Nginx

首先安装相关依赖

yum -y install make zlib zlib-devel gcc-c++ libtool pcre-devel

下载并安装Nginx

Nginx官网下载

cd /usr/local/

wget http://nginx.org/download/nginx-1.13.2.tar.gz

解压缩压缩包

tar -zxvf nginx-1.13.2.tar.gz

cd nginx-1.13.2

./configure

编译结果

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: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

常见问题

安装PCRE:  yum -y install pcre-devel, 安装完成后再执行./configure

安装Nginx

make && make install

启动nginx

cd /usr/local/nginx/sbin

执行./nginx

验证nginx是否启动成功

执行curl http://localhost 出现下面信息表示启动成功

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Nginx常用命令

cd /usr/local/nginx/sbin
启动:./nginx
停止:./nginx -s stop
重启:./nginx -s reopen    nginx -s reload
执行./nginx -h 可以看到命令的帮助信息

yum安装

修改yum 源#

进入/etc/yum.repos.d/目录创建一个nginx.repo 文件

cd /etc/yum.repos.d/

vim nginx.repo

写入源内容#

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

更新yum源#

yum update

使用yum 安装nginx#

yum install nginx -y

安装好之后看看版本

[root@localhost] # nginx -v
nginx version: nginx/1.10.1

打开nginx自启动

chkconfig nginx on

mysql 5.7

更新及安装mysql的yum 源#

官网下载源码包#

wget http://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm

rpm 安装mysql的yum源#

rpm -Uvh mysql57-community-release-el6-7.noarch.rpm

打开 mysql-community.repo 看关于mysql的内空,确定mysql57的enable是打开的

vim /etc/yum.repos.d/mysql-community.repo

安装mysql服务#

yum install mysql-community-server

注意版本号!

完成后启动服务#

service mysqld start

启动后,查看安装后自动生成的密码#

grep "password" /var/log/mysqld.log

第一行root@localhostu6erhYQNju&P 这个就是数据库的root密码了

修改初始化密码#

执行命令:

mysql_secure_installation

登录验证#

mysql的配制文件默认在 /etc/my.cnf

更多文档请请问官方网站: Installing MySQL on Linux Using the MySQL Yum Repository

打开mysql自启动

 chkconfig mysqld on

安装PHP7.0#

处理有php 及 php扩展:#

如果之前已经安装过php的话

yum remove php* php-common

安装php7的yum源#

与上面安装nginx、mysql的方式一样,先更新yum源然后再安装yum源

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

修改yum源#

vi /etc/yum.repos.d/remi.repo

[remi]段中的enabled=0改为enabled=1

vim /etc/yum.repos.d/remi-php70.repo

与remi.repo类似,将[remi-php70]段中的enabled=0改为enabled=1。

扫行命令查看版本如果显示的是7.x的话 那就没问题,当然也可以直接使用yum install php70 进行安装

[root@localhost tmp]# yum list php

yum 安装php7#

执行命令以下命令安装php及一些相当的扩展:

yum install php php-fpm php-cli php-pdo php-mysql php-gd php-bcmath php-xml php-mbstring php-mcrypt php-redis

安装好之后php -vphp -m 查看版本及安装的扩展

php的php.ini配制一般在/etc/php.ini

简单的修改一些配制:

vim /etc/php.ini

date.timezone = Asia/Shanghai
upload_max_filesize = 20M
post_max_size = 20M
display_errors = Off // 生产环境半掉就好了

# 使HTTP Header中不显示PHP信息把
expose_php = On 
修改为
expose_php = Off

重启php: service php-fpm restart

打开php自启动

 chkconfig php-fpm on

配置nginx与php#

安装好nginx之后,nginx默认的网站根目录应该是在/usr/share/nginx/html/

虚拟主机的配制在 /etc/nginx/conf.d 如果要配制新的域名在这里就可以了。

默认有一个default.conf的配制,如果不需要的话可以删除。

创建一个新的配制#

比如我创建一个 photo.ltq.com 的网站;

/etc/nginx/conf.d/ 创建文件photo.ltq.com.conf 内容如下

server {
    listen       80;
    server_name  photo.ltq.com;

    client_max_body_size 10m;

    root /home/dudulu/website/com-ltq-photo/web;

    access_log  /var/log/nginx/photo.ltq.com.access.log  main;
    error_log /var/log/nginx/photo.ltq.com.error.log;

    location / {
        try_files $uri /app.php$is_args$args;
        index  index.html index.htm index.php app.php;
    }

    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  app.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include        fastcgi_params;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  app.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include        fastcgi_params;
        internal;
    }
}

因为我一般使用symfony框架写东西,所以我就按symfony的标准配制了。如果不是请参考其他资料。

创建日志文件及分组

touch /var/log/nginx/photo.ltq.com.ccess.log
chown -R nginx.nginx /var/log/nginx/photo.ltq.com.ccess.log

安装composer#

官网下载composer包就好了

安装composer#

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php --install-dir=/usr/bin --filename=composer

php -r "unlink('composer-setup.php');"

测试composer#

执行composer -v 看看,如果有显示它的相关选项那就表示安装成功了!

设置国空镜像#

有两种方式启用本镜像服务:

  • 系统全局配置: 即将配置信息添加到 Composer 的全局配置文件 config.json 中
  • 单个项目配置: 将配置信息添加到某个项目的 composer.json 文件中

修改 composer 的全局配置文件#

控制台(Linux、Mac 用户)并执行如下命令:

composer config -g repo.packagist composer https://packagist.phpcomposer.com

修改当前项目的 composer.json 配置文件#

进入你的项目,找到composer.json文件并打开,添加以下信息:

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}

OK,一切搞定!试一下 composer install 来体验飞一般的速度吧!

未经允许不得转载:最长的路 » Centos6 安装Nginx+php+mysql

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址