阿里云Centos 7.X 服務器LNMP環境搭建

過程也很有意思

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,如果成功安裝會出來默認的歡迎頁


如果訪問不了可以看看下面問題解決

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

出現下面信息說明安裝成功


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里

<?php
phpinfo();

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防火墻攔截

安全組的解決方法:




防火墻的解決方法:

$ 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 <evp.h>

解決

$ 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 <curl-dir>/include/curl/

解決:

$ yum install libcurl libcurl-devel

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

解決:

$ yum install libjpeg libjpeg-devel

Q2.6 If configure fails try --with-webp-dir=<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=<DIR> checking for jpeg_read_header in -ljpeg... yes checking for png_write_image in -lpng... yes If configure fails try --with-xpm-dir=<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/
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容