Centos搭建LNMP環境

來源:互聯網
上載者:User
1、先做準備工作: ①、安裝LNMP環境所需的基礎組件:
yum -y install yum-fastestmirroryum -y install glibc-static libstdc++-static glibc-devel GeoIP GeoIP-devel sqlite-develyum -y install patch make flex bison tar pcre pcre-devel screen vsftpd ftp mailx jwhois postfix cronieyum -y install libtool libtool-libs kernel-devel autoconfyum -y install libjpeg libjpeg-devel libpng libpng-develyum -y install libtiff libtiff-devel gettext gettext-develyum -y install freetype freetype-devel libxml2 libxml2-devel zlib zlib-develyum -y install file glib2 glib2-devel bzip2 openldap-develyum -y install bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogsyum -y install e2fsprogs-devel krb5-devel libidn libidn-develyum -y install openssl openssl-devel vim-minimal unzipyum -y install libevent mcrypt mash libssh2 libssh2-devel libmetalink libmetalink-devel nghttp2yum -y install gd recode recode-devel libatomic_ops-develyum -y install libXpm libXpm-devel gmp gmp-devel gd-develyum -y updateexport PHP_AUTOCONF=/usr/bin/autoconfexport PHP_AUTOHEADER=/usr/bin/autoheader
②、CentOS設定防火牆由iptables改為firewall,firewall使用方法:
#安裝firewalldyum -y install firewalld#開啟firewalldsystemctl start firewalld#開機啟動firewalldsystemctl enable firewalld#關閉firewalldsystemctl stop firewalld#取消開機啟動firewalldsystemctl disable firewalld#為public域開放tcp協議的8080連接埠firewall-cmd --zone=public --add-port=8080/tcp --permanent#為public域添加http服務firewall-cmd --zone=public --add-service=http --permanent#重啟firewall服務firewall-cmd --reload
2、安裝MySQL:

PS.官方有詳細的安裝說明,詳情請戳:MySQL安裝方法詳解 ①、添加MySQL Yum Repository:

前往MySQL Yum Repository下載頁,頁面地址:MySQL Yum Repository下載頁,根據伺服器版本資訊及所需的MySQL版本資訊,下載對應的MySQL Yum Repository,本伺服器是centos7 64位作業系統,想要使用MySQL5.7,可用就是:http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm,命令如下:

cd ~ && wget http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpmsudo yum -y localinstall mysql57-community-release-el7-9.noarch.rpm
②、安裝MySQL:
sudo yum -y install mysql-devel mysql-community-server
③、啟動MySQL:
sudo service mysqld start
④、修改root密碼,建立使用者:

PS.MySQL5.7在啟動時會建立一個隨機密碼,查看設定的root密碼:

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

找到顯示結果中的密碼提示:

 [Note] A temporary password is generated for root@localhost: xxxxx

:之後的就是系統分配的隨機密碼,根據隨機密碼登入MySQL:

mysql -uroot -pxxxxx

登入MySQL之後使用命令列修改root密碼:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

設定密碼的時候一定要設定一個強密碼,如果不是強密碼,會有如下提示:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

關於密碼強度判斷及強密碼的規則,官方有以下說明:

NoteMySQL's validate_password plugin is installed by default. This will require that passwords contain at least one upper case letter, one lower case letter, one digit, and one special character, and that the total password length is at least 8 characters.

為了方便使用,我們經常會建立任意串連的root使用者:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;

重新整理許可權使命令生效:

flush privileges;

退出MySQL:

exit

接下來修改系統配置, ⑤、如果系統的防火牆開著(or behind a hardware firewall or NAT)你必須放開mysql使用的TCP連接埠,預設是3306;

#為public域開放tcp協議的3306連接埠firewall-cmd --zone=public --add-port=3306/tcp --permanent#為public域添加mysql服務firewall-cmd --zone=public --add-service=mysql --permanent#重啟firewall服務firewall-cmd --reload
⑥、修改設定,去掉MySQL中大小寫敏感:

用root帳號登入後,在/etc/my.cnf中設定不區分大小寫:

[mysqld]lower_case_table_names=1

PS.lower_case_table_names參數詳解: 0:區分大小寫,1:不區分大小寫 ⑦、更新MySQL:

yum -y update mysql-community-server
⑧、開機啟動MySQL:
sudo chkconfig mysqld on
3、安裝php7: ①、在/usr/local中建立檔案夾php7:
mkdir -p /usr/local/php7mkdir ~/php7 && cd ~/php7
②、下載並解壓php7安裝包:

去https://secure.php.net選擇php7版本,目前發行版是7.1.0,找到php7.1.0的下載包地址進行下載:
國內建議使用http://cn2.php.net/distributions/php-7.1.0.tar.gz,
國外建議使用https://secure.php.net/distributions/php-7.1.0.tar.gz,

wget https://secure.php.net/distributions/php-7.1.0.tar.gztar -zxvf php-7.1.0.tar.gzcd php-7.1.0/
③、配置編譯php7:

Ⅰ、在使用伺服器時我會開啟並使用SSL,所以編譯安裝php時會在環境中加入SSL模組,因為openssl1.0.1版本會有心臟滴血漏洞,所以這裡需要更換OpenSSL版本 ①、下載openssl1.0.2版本:

cd ~ && wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz
②、解壓安裝包:
tar -zxvf openssl-1.0.2-latest.tar.gz
③、編譯安裝openssl-1.0.2:
cd  openssl-1.0.2j./config shared zlibmake dependmake && make install
④、修改曆史的OpenSSL檔案設定備份:
mv /usr/bin/openssl /usr/bin/openssl.oldmv /usr/include/openssl /usr/include/openssl.old
⑤、設定軟串連使其使用新的OpenSSL版本 剛剛安裝的OpenSSL預設安裝在/usr/local/ssl下:
ln -s /usr/local/ssl/bin/openssl /usr/bin/opensslln -s /usr/local/ssl/include/openssl /usr/include/openssl
⑥、更新動態連結程式庫資料:
echo "/usr/local/ssl/lib/" >> /etc/ld.so.confexport LD_LIBRARY_PATH=/usr/local/ssl/lib/ldconfig -v

查看openssl版本openssl version:

OpenSSL 1.0.2j  26 Sep 2016

執行命令查看openssl依賴庫版本是否為1.0.2j:

strings /usr/local/ssl/lib/libssl.so |grep OpenSSL

Ⅱ、在編譯配置php時需要安裝mcrypt,由於著作權原因CentOS源沒有mcrypt,所以需要手動安裝,安裝方法有兩個:
①、使用第三方源,這種方法的好處是方便易於管理,壞處就是不安全:

cd ~ && wget http://www.atomicorp.com/installers/atomicsh ./atomic

在安裝時會顯示協議說明,需要輸入yes通過,然後直接斷行符號安裝源,安裝成功之後安裝mcrypt:

yum -y install libmcrypt  libmcrypt-devel

直接安裝得到的版本是2.5.8
②、下載源碼編譯安裝(推薦):

cd ~ && wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gztar -zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8/./configure --prefix=/usr/localmake && make install

PS. 使用這兩種方法安裝的區別在於編譯php時的--with-mcrypt路徑問題: 使用yum源安裝,路徑就是--with-mcrypt=/usr/include; 使用源碼編譯安裝,路徑就是--with-mcrypt=/usr/local

Ⅲ、修改curl的安裝,使其支援openssl:
PS.如果不支援openssl,CURL無法訪問https,在php中同樣也無法使用CURL訪問https:
查看當前CURL版本:

curl -V

可以看到結果如下:

curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz

很明顯可以看到是NSS模式,不支援openssl,接下來重新安裝curl: ①、下載、解壓縮CURL安裝包:

去官網查看curl最新版本已經是7.51.0,並且支援http2,所以建議重新編譯CURL並且使之支援http2,為了讓 curl 支援 HTTP2 我們需要安裝 nghttp2(http2 的 C 語言庫),nghttp2的地址:nghttp2,git倉庫:nghttp2的git倉庫,使用source方式安裝nghttp2:

cd ~ && git clone https://github.com/nghttp2/nghttp2.gitcd nghttp2autoreconf -iautomakeautoconf./configuremake && make install
②、安裝CURL7.51.0:
cd ~ && wget https://curl.haxx.se/download/curl-7.51.0.tar.gztar -zxvf curl-7.51.0.tar.gzcd curl-7.51.0/./configure --prefix=/usr/local/curl --without-nss --with-ssl=/usr/local/ssl --with-nghttp2=/usr/local --with-gssapi --with-libmetalink --with-libssh2 --enable-tls-srp --enable-sspimake && make installecho '/usr/local/lib' > /etc/ld.so.conf.d/local.confldconfigmv /usr/bin/curl /usr/bin/curl.oldln -s /usr/local/curl/bin/curl /usr/bin/

現在查看CURL版本curl -V,顯示結果如下:

curl 7.51.0 (x86_64-pc-linux-gnu) libcurl/7.51.0 OpenSSL/1.0.2j zlib/1.2.7 libssh2/1.4.3 nghttp2/1.18.0-DEVProtocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftpFeatures: IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets Metalink

IV:開始編譯安裝php:

cd ~/php7/php-7.1.0

PS.記憶體1G以下請在結尾加上:–disable-fileinfo,

./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc --enable-rpath --enable-bcmath --enable-calendar --enable-debug --enable-exif --enable-fileinfo --enable-filter --enable-fpm --enable-ftp --enable-gd-jis-conv --enable-gd-native-ttf --enable-hash --enable-inline-optimization --enable-json --enable-libxml --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-mysqlnd --enable-opcache --enable-opcache-file --enable-pcntl --enable-pdo --enable-session --enable-shared --enable-shmop --enable-simplexml --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-xml --enable-zip --enable-ctype --with-bz2 --with-curl=/usr/local/curl --with-fpm-user=www --with-fpm-group=www --with-freetype-dir=/usr/include/freetype2/freetype --with-gd --with-gettext --with-gmp --with-iconv --with-iconv-dir=/usr --with-jpeg-dir=/usr --with-mcrypt=/usr/local --with-mhash --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=mysqlnd --with-openssl=/usr/local/ssl --with-pdo-mysql=mysqlnd --with-pear --with-png-dir=/usr --with-xmlrpc --with-zlib --with-libxml-dir=/usr 

安裝:

make && make installmake test

安裝完成之後將php的程式引用至/usr/bin中:

ln -s /usr/local/php7/bin/* /usr/bin/ln -s /usr/local/php7/sbin/* /usr/sbin/

查看安裝的php的版本:

php -v

顯示結果如下:

PHP 7.1.0 (cli) (built: Dec  2 2016 19:07:57) ( ZTS DEBUG )Copyright (c) 1997-2016 The PHP GroupZend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

現在php7已經安裝完成,開始設定PHP7的設定檔php.ini,設定php.ini:

cp php.ini-production /usr/local/php7/etc/php.iniln -s /usr/local/php7/etc/php.ini /etc/php.inicp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.confln -s /usr/local/php7/etc/php-fpm
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.