阿里云Centos 7.X 服务器LNMP环境搭建

阿里云Centos 7.X 服务器LNMP环境搭建_第1张图片
过程也很有意思

1 更新系统软件

$ yum update

2 安装nginx

2.1 更新nginx源

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

2.2 安装nginx

$ yum install nginx

2.3 启动nginx

$ service nginx start

2.4 结果显示

Redirecting to /bin/systemctl start nginx.service

2.5 访问测试

访问http://你的ip,如果成功安装会出来默认的欢迎页


阿里云Centos 7.X 服务器LNMP环境搭建_第2张图片

如果访问不了可以看看下面问题解决

3 安装Mysql5.7

3.1 更新mysql源

$ yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

3.2 安装mysql

$ yum install mysql-community-server

3.3 安装mysql开发包

$ yum install mysql-community-devel

3.4 启动mysql

$ service mysqld start

3.5 查看启动状态

$ service mysqld status

出现下面信息说明安装成功


阿里云Centos 7.X 服务器LNMP环境搭建_第3张图片

3.6 获取mysql默认密码

$ grep 'temporary password' /var/log/mysqld.log

红色下划线的为密码


3.7 登录数据库

$ mysql -uroot -p

输入上面的密码

3.8 修改密码

mysql> set password for root@localhost = password('MyNewPass4!'); 

密码一定要复杂,包含大小写字母数字和特殊字符

3.9 退出mysql

mysql> quit;

退出后可以走 3.7 ,重新验证密码

4 安装php

4.1 下载php7源码包

$ cd /root & wget -O php.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

如果需要安装其他版本的php,可以修改链接里的版本号,举个栗子:

$ cd /root & wget -O php.tar.gz http://cn2.php.net/get/php-5.6.30.tar.gz/from/this/mirror

4.2 解压源码包

$ tar -xvf php.tar.gz

4.3 进入目录

$ cd php-7.0.1   //具体目录名称可以通过  ls 命令查看

4.4 安装php依赖包

$ yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel libicu-devel gcc-c++

4.5 编译配置

$ ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--with-mysql=mysqlnd \
--enable-intl

这一步可能会产生一下错误,基本都是相关开发包没有安装导致,可看下面问题解决

4.6 编译与安装

$ make && make install

4.7 添加PHP命令到环境变量

$ vim /etc/profile

//在末尾加入
PATH=$PATH:/usr/local/php/bin
export PATH

使改动立即生效

$ source /etc/profile

查看环境变量

$ echo $PATH

查看php版本

$ php -v

4.8 配置php-fpm

$ cp php.ini-production /etc/php.ini
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
$ cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
$ chmod +x /etc/init.d/php-fpm

//这是php7配置,其他版本请忽略
$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf    

4.9 启动php-fpm

$ /etc/init.d/php-fpm start

其他命令:

//杀死php-fpm
$ killall php-fpm

//重启php-fpm
$ /etc/init.d/php-fpm restart

5 配置nginx和绑定域名

5.1 配置nginx

$ vim /etc/nginx/conf.d/your.website.com.conf

your.website.com改成自己的域名
把下面内容复制到your.website.com.conf里

server {
    listen       80;
    server_name  your.website.com; # 修改为网站域名
    root /home/www/your.website.com; # 修改为网站根目录

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {

    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    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;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

5.2 重启nginx

service nginx reload

5.3 创建测试页面

$ vim /home/www/your.website.com/index.php

把下面代码复制到index.php里

5.4 把你的域名解析到服务器上;

5.5 查看访问http://your.website.com

注 开机自启动

$ vim /etc/rc.local

//添加下线内容
service nginx start
service mysqld start
/etc/init.d/php-fpm start

$ chmod +x /etc/rc.d/rc.local

问题解决

Q1 nginx通过ip无法访问

主要原因有几点:

  • 阿里云安全组拦截了80端口访问
  • Firewalld防火墙拦截

安全组的解决方法:


阿里云Centos 7.X 服务器LNMP环境搭建_第4张图片

阿里云Centos 7.X 服务器LNMP环境搭建_第5张图片

阿里云Centos 7.X 服务器LNMP环境搭建_第6张图片

防火墙的解决方法:

$ firewall-cmd --permanent --add-service=http
$ firewall-cmd --permanent --zone=public --add-service=https
$ firewall-cmd --permanent --zone=public --add-service=ssh
$ firewall-cmd --permanent --zone=trusted --add-port=80/tcp
$ firewall-cmd --reload

Q2 php编译配置错误

Q2.1 configure: error: xml2-config not found. Please check your libxml2 installation.

解决

$ yum install libxml2 libxml2-devel

Q2.2 configure: error: Cannot find OpenSSL's

解决

$ yum install openssl openssl-devel

Q2.3 configure: error: Please reinstall the BZip2 distribution

解决:

$ yum install bzip2 bzip2-devel

Q2.4 configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/

解决:

$ yum install libcurl libcurl-devel

Q2.5 If configure fails try --with-webp-dir= configure: error: jpeglib.h not found.

解决:

$ yum install libjpeg libjpeg-devel

Q2.6 If configure fails try --with-webp-dir= checking for jpeg_read_header in -ljpeg... yes configure: error: png.h not found.

解决:

$ yum install libpng libpng-devel

Q2.7 If configure fails try --with-webp-dir= checking for jpeg_read_header in -ljpeg... yes checking for png_write_image in -lpng... yes If configure fails try --with-xpm-dir= configure: error: freetype-config not found.

解决:

$ yum install freetype freetype-devel

Q2.8 configure: error: Unable to locate gmp.h

解决:

$ yum install gmp gmp-devel

Q2.9 configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决:

$ yum install libmcrypt libmcrypt-devel

Q2.10 configure: error: Please reinstall readline - I cannot find readline.h

解决:

$ yum install readline readline-devel

Q2.11 configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解决:

$ yum install libxslt libxslt-devel

Q2.12 configure: error: Cannot find ldap.h

解决

$ yum install openldap openldap-devel  -y

Q2.13 configure: error: Cannot find ldap libraries in /usr/lib

解决

$ cp -frp /usr/lib64/libldap* /usr/lib/

你可能感兴趣的:(阿里云Centos 7.X 服务器LNMP环境搭建)