centos服务器安装LNMP+composer+drush

Submitted by dravenk on Sat, 01/28/2017 - 16:03

在云服务器centos7上搭建LNMP环境;

  之前有写过一些笔记关于在一个初始化的镜像服务器上搭建LNMP环境,还包括composer、drush的安装、drupal运行环境,但都不太完整且解释说明过多,所以把内容整理了一下。只包括基本环境,不包括PHP.ini、Nginx.conf、防火墙等优化,这次直接贴命令和代码。下面操作均在root用户下写。


LNMP环境:

wget https://centos7.iuscommunity.org/ius-release.rpm  
rpm -ivh ius-release.rpm

警告:ius-release.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 9*****f: NOKEY
错误:依赖检测失败:
    epel-release = 7 被 ius-release-1.0-14.ius.centos7.noarch 需要

yum install epel-release
rpm -ivh ius-release.rpm
yum install nginx  //安装nginx
systemctl start nginx  //启动nginx
systemctl enable nginx //开机自启动
yum remove mariadb-libs -y  //删除旧版本的mariadb源
yum install mariadb101u-server mariadb101u //安装mariadb
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation //提示没设置过mariadb密码,是否设置root密码,点击Enter回车
yum install php70u-fpm php70u php70u-cli php70u-gd php70u-mysqlnd php70u-pdo php70u-pdo php70u-mcrypt php70u-mbstring php70u-json php70u-opcache  //安装PHP的各种扩展很多扩展基本都是必须的
systemctl start php-fpm
systemctl enable php-fpm

一份基本的Nginx.con配置文件:

vi /etc/nginx/conf.d/demo.conf

内容如下:

 server {
    listen       80;
    server_name  www.demo.com demo.com;
    root   /home/demo/www;
    index index.php index.html index.htm;
    location = /favicon.ico {
        try_files /home/lg/www/favicon.ico =404;
        #log_not_found off;
        #access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
    location ~ /\. {
        deny all;
    }

    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off;
        log_not_found off;
        expires max;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
    }

    error_page  404              /404.html;

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
    }
    }

systemctl reload nginx
nginx -t  //这只是测试一下nginx配置文件是否出错
nginx -s reload  //热重启nginx
systemctl reload php-fpm

 全局安装composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

全局安装composer方式2:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

添加环境变量

vi ~/.bash_profile
PATH=$PATH:$HOME/bin
改为:
PATH=$PATH:$HOME/bin:$HOME/.composer/vendor/bin
source ~/.bash_profile  //使变量立即生效


全局安装drush 

yum install git2u -y
git config --global user.name "longxianwen"
git config --global user.email "longxianwen@live.cn"
git config --global user.website "longxianwen.net"

git clone https://github.com/drush-ops/drush.git /usr/local/src/drush
cd /usr/local/src/drush
composer install

composer-install-error

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version  //查看版本

这个报错我一直以为只有phpunit没安装,后面才知道使php-xml没安装,学艺不精,惭愧。

yum install php70u-xml
composer install
ln -s /usr/local/src/drush/drush /usr/bin/drush  //全局安装drush

部分配置文件:

  • host: /etc/hosts
  • php.ini: /etc/php.ini
  • ssh: /etc/ssh/sshd_config
  • nginx: /etc/nginx/nginx.conf
  • php-fpm: /etc/php-fpm.d/www.conf
  • mariadb:/etc/my.cnf.d/mariadb-server.cnf