centos下編譯lnmp環境

Object發表於2017-07-12

uclund 部署架設環境

cp /etc/resolv.conf /etc/resolv.conf.bak

yum update -y
yum -y install gcc gcc-c++ autoconf cmake libjpeg libg libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-deves-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-idn-devel openssl openssl-devel openldap openldap-devel nss_ldclients openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN ncurses-devel bison bison-devel bzip2-devel libxslt-devel zip unzip
reboot

//網路卡繫結多個內網和外網 ucloud.cn下

cd /etc/sysconfig/network-scripts

cp ifcfg-eth0 ifcfg-eth0:1

vim ifcfg-eth0

DEVICE=eth0
IPADDR=$Publick_IP
NETMASK=255.255.255.255
BOOTPROTO=none
PEERDNS=yes
USERCTL=no
NM_CONTROLLED=no
ONBOOT=yes

vim ifcfg-eth0:1 其實這部不用操作 預設就是原來的內網配置

DEVICE=eth0:1
HWADDR=原配置中的mac地址
IPADDR=原配置的內網ip地址
NETMASK=255.255.0.0
BOOTPROTO=none
PEERDNS=yes
USERCTL=no
NM_CONTROLLED=no
ONBOOT=yes
DNS1=原配置
DNS2=原配置
DNS3=原配置

vim route-eth0

10.0.0.0/8 via 10.9.0.1
default via 10.9.0.1 src $Public_IP

service network restart

vim /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

新增iptables規則:

iptables -t nat -A PREROUTING ! -s 10.0.0.0/8 -d 內網ip/32 -j DNAT –to-destination $Publick_IP

iptables -t nat -A POSTROUTING -s 外網ip/32 -j SNAT –to-source 內網ip

umount /dev/vdb
fdisk -l
fdisk /dev/vdb
n
p
1
[enter]
[enter]
w

去掉vdb1的自動載入

vim /etc/fstab
mkdir /web
mkfs.ext3 /dev/vdb1
vim /etc/fstab

/dev/vdb1    /web    ext3    defaults     0 0

mount -a

安裝mysql-5.6.26

建立mysql使用者和組
groupadd mysql
useradd -g mysql -s /usr/sbin/nologin mysql
mkdir -p /web/mysql/data
mkdir -p /web/mysql/logs
mkdir -p /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /web/mysql

編譯安裝mysql
tar zxvf mysql-5.6.26.tar.gz
cd mysql-5.6.26

cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/web/mysql/data
-DSYSCONFDIR=/etc
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DMYSQL_TCP_PORT=3306
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_READLINE=1-DWITH_SSL=system
-DWITH_EMBEDDED_SERVER=1
-DENABLED_LOCAL_INFILE=1
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DWITH_DEBUG=0

make && make install

============可能遇到的編譯錯誤===========
出現函式gets錯誤的
解決辦法:
rm -f CMakeCache.txt

yum -y install ncurses-devel

修改配置檔案
cp ./support-files/my-medium.cnf /etc/my.cnf
vi /etc/my.cnf

 輸入以下內容(可以先清空預設內容):

[mysqld]
datadir=/web/mysql/data
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/web/mysql/logs/mysqld.log
pid-file=/web/mysql/mysqld.pid
user=mysql
tmpdir=/tmp

or
cp /web/lanmp/my.cnf /etc/my.cnf

初始化資料庫
/usr/local/mysql/scripts/mysql_install_db
–user=mysql
–basedir=/usr/local/mysql
–datadir=/web/mysql/data

配置開機自啟動
cp ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig –add mysqld
chkconfig mysqld on

新增MySQL的軟連結以適應init指令碼
ln -sv /usr/local/mysql/bin/mysql /usr/sbin/mysql
ln -sv /usr/local/mysql/bin/mysqladmin /usr/sbin/mysqladmin
ln -sv /usr/local/mysql/bin/mysqldump /usr/sbin/mysqldump

啟動mysql
service mysqld start  # 或 /etc/init.d/mysqld start

修改root密碼:

mysqladmin -uroot password `新密碼`

mysql -uroot -p
輸入新密碼
create user `duigou`@`%` identified by `密碼`
create user `duigou`@`localhost` identified by `密碼`
create database duigou default character set utf8mb4 collate utf8mb4_general_ci;
grant all privileges on duigou.* to `duigou`@`%` identified by `密碼`;
grant all privileges on duigou.* to `duigou`@`localhost` identified by `密碼`;

======================================================================================
或者重新配置一個超級管理員並刪除root
GRANT ALL PRIVILEGES ON . TO `admin`@`localhost` IDENTIFIED BY `12345678`;
GRANT ALL PRIVILEGES ON . TO `admin`@`127.0.0.1` IDENTIFIED BY `12345678`;

DELETE FROM user WHERE user!=`admin`; #刪除原先預設的使用者,僅保留新建的admin使用者

第三步:安裝nginx-1.8.0

新增www使用者和組、建立網站虛擬目錄
groupadd www
useradd -g www -s /usr/sbin/nologin www
mkdir -p /web/htdocs/www
chmod +w /web/htdocs/www
chown -R www:www /web/htdocs/www
mkdir -p /web/htdocs/logs
chmod +w /web/htdocs/logs
chown -R www:www /web/htdocs/logs

安裝Nginx所需的pcre庫
tar zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make && make install
ln -s /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1
cd ..

安裝nginx-1.8.0
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure
–user=www
–group=www
–prefix=/usr/local/nginx
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_image_filter_module

make && make install

建立Nginx配置檔案和建立fcgi.conf檔案
cp -f /web/lanmp/nginx.conf.uclound /usr/local/nginx/conf/nginx.conf
cp -f /web/lanmp/fastcgi.conf /usr/local/nginx/conf/fastcgi.conf

啟動nginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx 直接啟動

配置開機自啟動
cp /web/lanmp/initd/nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chkconfig nginx on
service nginx start|stop|restart|reload

第四步:安裝php5.5.13

cd ..
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure –prefix=/usr/local
make && make install

=====================================================================
編譯libiconv解決./stdio.h:1010:1: 錯誤: ‘gets’未宣告(不在函式內)
cd srclib/
sed -i -e `/gets is a security/d` ./stdio.in.h
cd ../
make

make install

cd ..
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7/
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure –enable-ltdl-install
make && make install

cd ..
cd ..
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install

對共享庫做符號連結

ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

cd ..
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install

=====================================================================
遇到問題:configure: error: * libmcrypt was not found
繼續查詢,還是變數的問題
解決方法如下:執行

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

安裝php:
cd ..
tar zxvf php-5.6.11.tar.gz
cd php-5.6.11

./configure
–prefix=/usr/local/php
–with-config-file-path=/usr/local/php/etc
–with-mysql=/usr/local/mysql
–with-mysqli=/usr/local/mysql/bin/mysql_config
–enable-fpm
–enable-calendar
–enable-dba
–enable-wddx
–enable-opcache
–with-fpm-user=php-fpm
–with-fpm-group=php-fpm
–with-mysql=mysqlnd
–with-mysql-sock=/tmp/mysql.sock
–with-libxml-dir=/usr
–with-xsl
–with-gd
–with-bz2
–with-kerberos
–with-jpeg-dir
–with-png-dir
–with-zlib
–with-freetype-dir
–with-iconv-dir
–with-zlib-dir
–with-mcrypt
–enable-soap
–enable-gd-native-ttf
–enable-ftp
–enable-sockets
–enable-zip
–enable-posix
–enable-pcntl
–enable-mbstring
–enable-fpm –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf
–with-openssl –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –enable-soap
–enable-mbregex
–enable-sysvsem
–enable-sockets
–enable-shmop
–enable-bcmath
–enable-xml
–enable-exif
–disable-ipv6
–with-pear
–with-curl
–with-openssl
–with-pdo-mysql

–enable-magic-quotes
–disable-fileinfo

注:如果記憶體較大 可以去掉–disable-fileinfo

======================================================
configure: error: Please reinstall the BZip2 distribution
yum install bzip2-devel.x86_64 -y

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

yum install libxslt-devel -y

make ZEND_EXTRA_LIBS=`-liconv`

make
如果直接make出錯make: * [sapi/cli/php] Error 1

make ZEND_EXTRA_LIBS=`-liconv`

make install

php.ini設定

cp /web/lanmp/php.ini /usr/local/php/etc/php.ini
cp /web/lanmp/php-fpm.conf /usr/local/php/etc/php-fpm.conf

fpm加入自啟動
cp /web/lanmp/initd/php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
service php-fpm start|stop|restart|reload

vi /etc/rc.local

輸入

ulimit -SHn 65535

以上簡單部署 下面是增加部分的擴充套件和優化

編譯PHP擴充套件模組memcache、pdo_mysql、imagick
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
出錯出現configure: error: mysql_query missing!? 解決方法 ./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql
make出錯
make && make install
cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql
ln -s /usr/local/mysql/include/* /usr/local/include/
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

tar zxvf imagick-3.2.0RC1.tgz
cd imagick-3.2.0RC1
/usr/local/php/bin/phpize
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure –with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

修改php.ini配置檔案
vi /usr/local/php/etc/php.ini

查詢; extension_dir = “/” 將前面的;去掉並修改為

extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/”

並加入

extension=memcache.so
extension=pdo_mysql.so
extension=imagick.so

執行下面的命令使配置檔案立即生效:
kill -USR2 cat /usr/local/php/var/run/php-fpm.pid

其他(可選):

優化linux核心引數

vi /etc/sysctl.conf

在末尾增加以下內容:

Add

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_len = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 120

net.ipv4.ip_local_port_range = 1024 65535

使配置立即生效:
/sbin/sysctl -p

安裝opcache(因為PHP 5.5已經整合Zend Opcache,可以替代eaccelerator)
tar zxvf zendopcache-7.0.3.tgz
cd zendopcache-7.0.3
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

在php.ini中加入下面配置:

[opcache]
zend_extension=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so”
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
?
1
2 # 使php.ini配置檔案立即生效
kill -USR2 cat /usr/local/php/var/run/php-fpm.pid

常用命令:

修改完php.ini後執行:

kill -USR2 cat /usr/local/php/var/run/php-fpm.pid

修改完nginx.conf後執行

/usr/local/nginx/sbin/nginx -s reload

重啟mysql服務執行:

service mysqld (start|stop|restart)

相關文章