centos7-ECS-LNMP-就这一篇就够了!!

搭建LNMP环境(CentOS 6)

最终软件版本

nginx-1.10.2
php-5.6.28
mysql-5.7.19
参考文档 
http://blog.sina.com.cn/s/blog_6272e6b20102wlc4.html
https://yq.aliyun.com/articles/161062
https://yq.aliyun.com/articles/65213

目录规划

/var/www/html/
            phpMyAdmin      <-----------------phpMyAdmin
            your-project    <-----------------项目
            your-project2

/usr/local/
            mysql           <-----------------自编译软件
            nginx

/opt/
    soft                    <-----------------软件资料
    data/mysql              <-----------------数据库文件

基本流程


啊啊啊

下载软件

nginx
# wget http://nginx.org/download/nginx-1.10.2.tar.gz

使用云服务器 ECS 搭建LNMP平台的操作步骤如下:

  • 准备编译环境
  • 安装nginx
  • 安装mysql
  • 安装php-fpm
  • 测试访问

步骤一:准备编译环境

1、系统版本说明

   # cat /etc/redhat-release 
    CentOS release 6.9 (Final)

2、关闭SELINUX

修改配置文件,重启服务后永久生效。

# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
命令行设置立即生效。

# setenforce 0
setenforce: SELinux is disabled

3、安全组设置

  • 在ECS安全组放行需访问的端口和访问白名单,下面的示例表示允许所有IP访问服务器的80端口。您可以根据实际情况放行允许访问的客户端IP。

步骤二:安装nginx

1、添加运行nginx服务进程的用户

创建群组
# groupadd www
创建一个用户,不允许登陆和不创主目录 
# useradd -s /sbin/nologin -g www -M www

2、下载源码包解压编译

# 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 \
--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=www \
--group=www \
--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

3、添加SysV启动脚本

# vim /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
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

4、赋予脚本执行权限

# chmod +x /etc/init.d/nginx

5、添加至服务管理列表,设置开机自启

# chkconfig --add nginx
# chkconfig  nginx on

6、启动服务

# service nginx start

7、浏览器访问可看到默认欢迎页面

或者查看nginx进程
# ps aux|grep nginx
root      5227  0.0  0.1  44976  1196 ?        Ss   11:37   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     5229  0.0  0.1  45412  1804 ?        S    11:37   0:00 nginx: worker process                   
root      5231  0.0  0.0 103320   884 pts/0    S+   11:37   0:00 grep nginx

8、经过优化的nginx.conf配置文件

user  www www;

worker_processes 1;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";

        #limit_zone  crawler  $binary_remote_addr  10m;

        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

server
    {
        listen       80;
        server_name www.cnhzz.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/htdocs;

            location ~ \.php$ {
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include        fastcgi_params;
            }

        location /status {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }

        location ~ .*\.(js|css)?$
            {
                expires      12h;
            }

        access_log  /home/wwwlogs/access.log  access;
    }
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

步骤三:安装mysql

1、准备编译环境

# yum groupinstall "Server Platform Development"  "Development tools" -y
# yum install cmake -y

2、准备mysql数据存放目录

# mkdir /opt/data
# groupadd -r mysql
# useradd -r -g mysql -s /sbin/nologin mysql
# id mysql

uid=498(mysql) gid=498(mysql) groups=498(mysql)

3、更改数据目录属主属组

# chown -R mysql:mysql /opt/data

4、解压编译在MySQL官网下载的稳定版源码包,这里使用的是5.7.19版本

  • MySQL5.6.37
# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.37.tar.gz
# tar xvf mysql-5.6.37.tar.gz -C  /usr/local/src
# cd /usr/local/src/mysql-5.6.37/
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/opt/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

5、修改安装目录的属组为mysql

修改/usr/local/mysql权限
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
  • 关于my.cnf配置文件:
在启动MySQL服务时,会按照一定次序搜索my.cnf,
先在/etc目录下找,
找不到则会搜索”$basedir/my.cnf” 
就是安装目录下 /usr/local/mysql/my.cnf,
这是新版MySQL的配置文件的默认位置! 
注意:在CentOS 6.x版操作系统的最小安装完成后,
在/etc目录下会存在一个my.cnf,
需要将此文件更名为其他的名字。 如:/etc/my.cnf.bak,
否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。 
由于我们已经卸载了最小安装完成后的mysq库所以,就没必要操作了。
进入support-files目录

6、初始化数据库

# cd support-files/
如果还有my.cnf请备份
# mv /etc/my.cnf /etc/my.cnf.bak
如果愿意也可以复制配置文件到etc下
# cp my-default.cnf /etc/my.cnf
初始化数据库

执行初始化配置脚本,创建系统自带的数据库和表,注意配置文件的路径
# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/opt/data --user=mysql

7、拷贝配置文件和启动脚本。

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld

8、设置开机自动启动。

# chkconfig mysqld  on 
# chkconfig --add mysqld

9、修改配置文件中的安装路径及数据目录存放路径。

# echo -e "basedir = /usr/local/mysql\ndatadir = /opt/data\n" >> /etc/my.cnf

10、设置PATH环境变量。

# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh      
# source /etc/profile.d/mysql.sh

11、启动服务。

# service mysqld start 
或者
# /etc/init.d/mysqld start
# mysql -h 127.0.0.1

12、MySQL配置

  • MySQL5.6.x启动成功后,root默认没有密码,我们需要设置root密码。 设置之前,我们需要先设置PATH,要不,不能直接调用mysql
修改/etc/profile文件
# vim /etc/profile
在文件末尾添加
PATH=/usr/local/mysql/bin:$PATH
export PATH
让配置立即生效
# source /etc/profile
登陆测试,默认是没有密码,直接回车就可进入
# mysql -uroot -p
设置mysql密码
/usr/local/mysql/bin/mysqladmin -uroot -p password '你的密码'
注:输入的密码为默认密码 空
登陆进命令行模式
mysql -uroot -p
查看用户
select user,host from mysql.user;
删除不必要的用户
drop user ""@localhost;
drop user ""@iz2ze5ntizeb71218sigbcz; 
drop user root@iz2ze5ntizeb71218sigbcz; 
drop user root@'::1';
赋予账号远程访问的权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '你的密码' WITH GRANT OPTION; 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'ssz2080' WITH GRANT OPTION;
关于删除MySQL的默认root用户参考:http://blog.chinaunix.net/uid-16844903-id-3377690.html
其它一些信息查询: 检查mysql版本

mysql -uroot -p"密码" -e "select version();"

验证mysql安装路径
ls -ld /usr/local/mysql/

步骤四:安装php-fpm

Nginx本身不能处理PHP,作为web服务器,当它接收到请求后,不支持对外部程序的直接调用或者解析,必须通过FastCGI进行调用。如果是PHP请求,则交给PHP解释器处理,并把结果返回给客户端。PHP-FPM是支持解析php的一个FastCGI进程管理器。提供了更好管理PHP进程的方式,可以有效控制内存和进程、可以平滑重载PHP配置。

1、安装依赖包。

# yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel

2、解压官网下载的源码包,编译安装。

# wget http://cn.php.net/distributions/php-5.6.31.tar.gz
# tar xvf php-5.6.31.tar.gz -C /usr/local/src
# cd /usr/local/src/php-5.6.31
# ./configure --prefix=/usr/local/php \
--with-config-file-scan-dir=/etc/php.d \
--with-config-file-path=/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--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 \
--disable-fileinfo

# make && make install

3、添加php和php-fpm配置文件。

# cp /usr/local/src/php-5.6.31/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

4、添加php-fpm启动脚本。

# cp /usr/local/src/php-5.6.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

5、添加php-fpm至服务列表并设置开机自启。

# chkconfig --add php-fpm     
# chkconfig --list php-fpm     
# chkconfig php-fpm on

6、启动服务。

按照标准,给php-fpm创建一个指定的用户和组
创建群组
# groupadd www
创建一个用户,不允许登陆和不创主目录 
# useradd -s /sbin/nologin -g www -M www
# service php-fpm start
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值